trying to rotate a triangle with complex numbers in tkinter python

the first method is mine, the other two are from here

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?


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)

# keypress event
def key(event):

    # handle backspace
    if ch=='\x08':
        if len(text.get())>1 :
            if entry_text=='-': entry_text='0'

    # we want an integer
        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
        c.coords(polygon_item, *newxy)
    except ValueError:
        print "not integer"

text = Entry(root)
text.bind("<Key>", key)


# a triangle
triangle = [(50, 50), (150, 50), (150, 150)]

polygon_item = c.create_polygon(triangle)
center = 100, 100


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)

Need Your Help

Two way data-binding in slider in AngularJS

javascript jquery angularjs

I have a use case where I need the slider-directive to update configuration &amp; model value dynamically through a controller. For this I am trying to a create a two way data binding. However, I a...