• drawing lines through circles in python
    1 replies, posted
I recently started programming in python, and i could really use some help. So basically im trying to make a program that could draw a circle in a certain location with a certain radius, and then draw a line from the center to the edge, a number of pixels around the circumference. I have this working code: [code] from tkinter import ttk from tkinter import * from tkinter.ttk import * import math class Application(Frame): def calc_coords(self, r, l): a = (l/(2*math.pi*r))*360 Y = math.cos(math.radians(a))*r X = math.sin(math.radians(a))*r Y = round(Y,2) X = round(X,2) return (X,Y) def makecanvas(self): self.grid_forget() global can can = Canvas(self.master, width=500, height=250) can.create_oval(145, 45, 255, 155, width=2) a = self.calc_coords(55, 1) can.create_line(200,100,a[1]+200,a[0]+100, width = 2, capstyle = ROUND) can.grid() def createWidgets(self): self.inst = Button(self) self.inst["text"] = "GO!" self.inst["width"] = "15" self.inst["command"] = self.makecanvas self.inst.grid(row=3, column=1, pady=15, sticky=N) def __init__(self, master=None): Frame.__init__(self, master) self.pack() self.createWidgets() root = Tk() app = Application(master=root) app.mainloop() root.destroy() [/code] which works. With a 1 px offset it draws the line at 90 degrees. Now, i trided changing up the code so that the whole thing would be in one function like so: [code] from tkinter import ttk from tkinter import * from tkinter.ttk import * import math class Application(Frame): def makecanvas(self): self.grid_forget() global can can = Canvas(self.master, width=500, height=250) a = self.calc_coords(55, 1, 200, 100) def calc_coords(self, r, l, x, y): a = (l/(2*math.pi*r))*360 Y = math.cos(math.radians(a))*r X = math.sin(math.radians(a))*r Y = round(Y,2) X = round(X,2) can.create_oval(x-r, y-r, x+r, y+r, width=2) can.create_line(x,y,X+x,Y+y, width = 2, capstyle = ROUND) return (X,Y) def createWidgets(self): self.inst = Button(self) self.inst["text"] = "GO!" self.inst["width"] = "15" self.inst["command"] = self.makecanvas self.inst.grid(row=3, column=1, pady=15, sticky=N) def __init__(self, master=None): Frame.__init__(self, master) self.pack() self.createWidgets() root = Tk() app = Application(master=root) app.mainloop() root.destroy() [/code] It uses the exact same variables, and should produce the exact same result. But instead of the line being at 90 degrees, it is at 180. Wtf? And in the second example, the line rotates counterclockwise compared to clockwise in the first example.
any ideas? anyone?
Sorry, you need to Log In to post a reply to this thread.