# trying to rotate a triangle with complex numbers in tkinter python

the first method is mine, the other two are from here http://effbot.org/zone/tkinter-complex-canvas.htm

so i call the rotate method that calls the second one that calls the first one. the first one gets the xy coordinates from the angle that is passed, and then work with them to translate the triangle.

self.x and self.y are the coordinates on the canvas of the triangle, the middle of the lower line of the triangle

i guess there's another way to do this, much simpler.

and i have found this btw, but it doesn't really help

How do I rotate a polygon in python on a Tkinter Canvas?

## Answers

Your post implied difficulty with understanding how to rotate triangles using complex numbers. After reading your comment in reply to my answer I have edited my code sample to demonstrate a way to get the angle from keyboard input. I have no experience with Tkinter so perhaps someone can help with a more state of the art approach. Gleaned from Tkinter: Events and Bindings and The Tkinter Entry Widget

When you enter the keypress event handler, the Entry widget's text, retrieved with text.get(), doesn't include the latest keypress character.

The angle entered is in degrees and can be negative.

from Tkinter import * import tkSimpleDialog as tks import cmath,math root = Tk() c = Canvas(root,width=200, height=200) c.pack() # keypress event def key(event): text.focus_force() ch=event.char # handle backspace if ch=='\x08': if len(text.get())>1 : entry_text=text.get()[:-1] if entry_text=='-': entry_text='0' else: entry_text='0' else: entry_text=text.get()+ch # we want an integer try: angle_degrees=int(entry_text) cangle = cmath.exp(angle_degrees*1j*math.pi/180) offset = complex(center[0], center[1]) newxy = [] for x, y in triangle: v = cangle * (complex(x, y) - offset) + offset newxy.append(v.real) newxy.append(v.imag) c.coords(polygon_item, *newxy) except ValueError: print "not integer" text = Entry(root) text.bind("<Key>", key) text.pack() text.focus_force() # a triangle triangle = [(50, 50), (150, 50), (150, 150)] polygon_item = c.create_polygon(triangle) center = 100, 100 mainloop()

roitation of anything is best done by expressing every point (x,y) as a complex number x + iy Then Rotate each point by multiplying by the comples number cos(angle) + i sin(angle)