1

I want to rotate an oval around it's center. Currently I can display the static object witohut rotation, and I can't seem to find what to do wit it.

EDIT: I made a rotation function based on complex number multiplication, which seemed to work on lower polygons but the ellipse kinda looks the same.

A potato-level noob C.S. student.

also, the code:

from tkinter import*
import cmath, math
import time


def poly_rot(tup, deg):
    rad=deg*0.0174533
    crot=cmath.exp(rad*1j)
    rotpol=[]
    i=0
    while i < len(tup):
        x=tup[i]
        y=tup[i+1]
        z=crot*complex(x,y)
        rotpol.append(400+z.real)
        rotpol.append(400+z.imag)
        i=i+2
    return rotpol


def poly_oval(x0,y0, x1,y1, steps=60, rotation=0):
    """return an oval as coordinates suitable for create_polygon"""
    # x0,y0,x1,y1 are as create_oval
    # rotation is in degrees anti-clockwise, convert to radians
    rotation = rotation * math.pi / 180.0
    # major and minor axes
    a = (x1 - x0) / 2.0
    b = (y1 - y0) / 2.0
    # center
    xc = x0 + a
    yc = y0 + b
    point_list = []
    # create the oval as a list of points
    for i in range(steps):
        # Calculate the angle for this step
        # 360 degrees == 2 pi radians
        theta = (math.pi * 2) * (float(i) / steps)
        x1 = a * math.cos(theta)
        y1 = b * math.sin(theta)
        # rotate x, y
        x = (x1 * math.cos(rotation)) + (y1 * math.sin(rotation))
        y = (y1 * math.cos(rotation)) - (x1 * math.sin(rotation))

        point_list.append(round(x + xc))
        point_list.append(round(y + yc))
    return point_list




inp= input("We need an ellipse. One to fit in a 800*800 window. Give the size of it's axes separated by ','-s (ex: lx,ly)!\n")
inpu= inp.split(',')
lx=int(inpu[0])
ly=int(inpu[1])
x0=400-lx//2
x1=400+lx//2
y0=400-ly//2
y1=400+ly//2
deg= float(input("Let's rotate this ellipse! But how much, in degrees, should we?"))

pre=poly_oval(x0, y0, x1, y1)
post=poly_rot(poly_oval(x0, y0, x1, y1), deg)



root =Tk()
w = Canvas(root, width=801, height=801)
w.config(background="dark green")
w.pack()
w.create_polygon(tuple(post), fill="yellow", outline="yellow" )




root.mainloop()
  • Once you have your oval as a polygon object (Only polygons can be rotated), you want a function to rotate it by a set amount, and then call that function without blocking the `mainloop`: `for i in range(10): root.after(i*100, rotate_oval() )`. – SneakyTurtle Nov 05 '17 at 13:28
  • Will it overwrite the previous image? – Bokor Albert Nov 05 '17 at 18:41
  • It would depend on what `rotate_oval` does. It could alter the co-ordinates of your oval via the `coords` Canvas method instead of creating a new object, which wouldn't 'overwrite' the previous object. – SneakyTurtle Nov 05 '17 at 18:59
  • Currently it uses the coordinates as numbers not in direct association with the object. How shoud I formulate a `coords` rewriting? – Bokor Albert Nov 05 '17 at 19:38
  • If your oval object is called `my_oval` for example, then you can get the current coordinates of `my_oval` with `w.coords(my_oval)`, perform the calculations you need to obtain the new coordinates, and then perform `w.coords(my_oval, *new_coords)` where new_coords is a list or tuple containing the new coordinates. – SneakyTurtle Nov 05 '17 at 20:35

0 Answers0