(Tkinter, py3) How to make a rotation animation? - python-3.x

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()

Related

How to scale a list of 2D coordinates to a desired size

I have a list of 2D coordinates that draw a shape, e.g. [{12, 14}, {22, 44}, {59, 33}, ...]
I'd like to be able to take this shape, and center it in a canvas of arbitrary size (let's say 400x400) and have it take as much space as possible.
I've figured out how to normalize the list so it's in the 0-1 range, but ended up being stuck there when trying to then scale it up to the desired size.
Any help would be appreciated!
Find minimal and maximal values for X and Y coordinates xmin, xmax, ymin, ymax
Calculate point cloud width and height, and middle coordinates
cw = xmax - xmin
ch = ymax - ymin
mx = (xmax + xmin) / 2
my = (ymax + ymin) / 2
Now find coefficient
if cw * canvas.height >= ch * canvas.width
coeff = canvas.width / cw
else
coeff = canvas.height / ch
Now get canvas center
centerx = canvas.width / 2
centery = canvas.height / 2
and apply the next transformation to every point (x,y):
screenx = centerx + coeff * (x - mx)
screeny = centery + coeff * (y - my)

How change the spiral movement in 2D space?

I have two points in 2D space as we can see in the figure to move from the blue point to the red points we can use the equation (1). Where b is a constant used to limit the shape of the logarithmic spiral, l is a random number in [−1,1], which is used to
control the indentation effect of the movement, D indicates the distance between blue points and the current point
I need another movement that can move from blue points to the red points like in the figure
You can use sinusoidal model.
For start point (X0, Y0) and end point (X1,Y1) we have vector end-start, determine it's length - distance between points L, and angle of vector Fi (using atan2).
Then generate sinusoidal curve for some standard situation - for example, along OX axis, with magnitude A, N periods for distance 2 * Pi * N:
Scaled sinusoid in intermediate point with parameter t, where t is in range 0..1 (t=0 corresponds to start point (X0,Y0))
X(t) = t * L
Y(t) = A * Sin(2 * N * Pi * t)
Then shift and rotate sinusoid using X and Y calculated above
X_result = X0 + X * Cos(Fi) - Y * Sin(Fi)
Y_result = Y0 + X * Sin(Fi) + Y * Cos(Fi)
Example Python code:
import math
x0 = 100
y0 = 100
x1 = 400
y1 = 200
nperiods = 4
amp = 120
nsteps = 20
leng = math.hypot(x1 - x0, y1 - y0)
an = math.atan2(y1 - y0, x1 - x0)
arg = 2 * nperiods* math.pi
points = []
for t in range(1, nsteps + 1):
r = t / nsteps
xx = r * leng
yy = amp * math.sin(arg * r)
rx = x0 + xx * math.cos(an) - yy * math.sin(an)
ry = y0 + xx * math.sin(an) + yy * math.cos(an)
points.append([rx, ry])
print(points)
Draw points:

How can I calculate the centre between two centre of rectangles in python 3?

I have two rectangles and I want to measure the distance between them. I have centre coordinates for both rectangle.
R1 = (908,1018) ## (x,y,width,height)=(595,11,627,2015)
R2 = (891,1019) ## (x,y,width,height)= (670,871,442,297)
I know, we can calculate it with help of Pythagoras Theory. But how can I do it with python in a simple way.
Like you said, Pythagoras works fine here. Try something like this
x1, y1 = R1
x2, y2 = R2
dx = x2 - x1
dy = y2 - y1
d = math.sqrt(dx * dx + dy * dy)
print(d)

Why's the red polygon goes out of plane, then returns to its specified place again?

I want to put the red polygon in place of the empty one. But it goes above it first before returning again to it. Can someone help me with that?
Why's the red polygon goes out of plane, then returns to its specified place again?
def Rotating(Rotating_angle, polygon_points): # Drawing the rotated figure
my_points = (re.findall("\(\-?\d*\.?\d*\,\-?\d*\.?\d*\)", polygon_points))
sleep_time = .5
my_new_points = [] # Scale_points
for point in my_points:
new_point = str(point).replace(")", "").replace("(", "").split(",")
# creating a list with all coordinates components
all_coordinates_components = []
all_coordinates_components.append(abs(eval(new_point[0])))
all_coordinates_components.append(abs(eval(new_point[1])))
point = (scale * eval(new_point[0]), scale * eval(new_point[1]))
my_new_points.append(point)
rotated_points = []
for point in my_new_points:
new_point = str(point).replace(")", "").replace("(", "").split(",")
theta = Rotating_angle
X = (eval(new_point[0]) * cos(theta * pi / 180)) - (eval(new_point[1]) * sin(theta * pi / 180))
Y = (eval(new_point[0]) * sin(theta * pi / 180)) + (eval(new_point[1]) * cos(theta * pi / 180))
# length = sqrt((X) ** 2 + (Y) ** 2)
point = (X, Y)
rotated_points.append(point)
# draw steps
time.sleep(sleep_time)
draw_rotation_steps(my_new_points, theta) # draw steps ((((( 3 )))))
# drawing rotated polygon
draw_polygon(rotated_points) # draw rotated polygon ((((( 4 )))))
s = Shape('compound')
poly1 = (my_new_points)
s.addcomponent(poly1, fill="red")
register_shape('myshape', s)
shape('myshape')
polygon = Turtle(visible=False)
polygon.setheading(90)
polygon.speed('slowest')
polygon.penup()
polygon.shape('myshape')
polygon.st()
polygon.circle(0, theta)
pen_dot = Turtle(visible=False)
pen_dot.speed('fastest')
for point in rotated_points:
pen_dot.penup()
pen_dot.goto(point)
pen_dot.pendown()
pen_dot.dot(5, 'blue violet')
I can't reproduce the behaviour you describe. But your code is riddled with issues that should be addressed so perhaps fixing those might also fix the positioning issue:
These loops are nested, but they shouldn't be:
my_new_points = []
for point in my_points:
...
rotated_points = []
for point in my_new_points:
They both should be at the same level.
You shouldn't use eval(). In this situation, use float():
point = (scale * eval(new_point[0]), scale * eval(new_point[1]))
Here, you've already converted the points but you turn them back into strings and reconvert them:
new_point = str(point).replace(")", "").replace("(", "").split(",")
X = (eval(new_point[0]) * cos(theta * pi / 180)) - (eval(new_point[1]) * sin(theta * pi / 180))
when you can simply do:
X = point[0] * cos(theta * pi / 180) - point[1] * sin(theta * pi / 180)
You don't have to put the pen down for the .dot() method to work:
pen_dot.goto(point)
pen_dot.pendown()
pen_dot.dot(5, 'blue violet')
so you can move the penup() out of the loop.
Below is my rework of your example code. I've added just enough code to make it runnable and removed anything that had nothing to do with the problem. See if this gives you any ideas of how to simplify and fix your own code:
import re
from math import sin, cos, pi
from turtle import *
scale = 20
def draw_rotation_steps(points, theta):
''' Not supplied by OP '''
pass
def draw_polygon(rotated_points):
''' Simple replacement since not supplied by OP '''
hideturtle()
penup()
goto(rotated_points[-1])
pendown()
for point in rotated_points:
goto(point)
def Rotating(theta, polygon_points): # Drawing the rotated figure
my_points = re.findall(r"\(\-?\d*\.?\d*\,\-?\d*\.?\d*\)", polygon_points)
my_new_points = [] # Scaled points
for point in my_points:
X, Y = point.replace(")", "").replace("(", "").split(",")
point = (scale * float(X), scale * float(Y))
my_new_points.append(point)
rotated_points = []
for point in my_new_points:
X = point[0] * cos(theta * pi / 180) - point[1] * sin(theta * pi / 180)
Y = point[0] * sin(theta * pi / 180) + point[1] * cos(theta * pi / 180)
point = (X, Y)
rotated_points.append(point)
# draw steps
draw_rotation_steps(my_new_points, theta) # draw steps
# drawing rotated polygon
draw_polygon(rotated_points) # draw rotated polygon
s = Shape('compound')
s.addcomponent(my_new_points, fill="red")
register_shape('myshape', s)
polygon = Turtle('myshape', visible=False)
polygon.setheading(90)
polygon.showturtle()
polygon.circle(0, theta, steps=100) # added steps to visually slow it down
pen_dot = Turtle(visible=False)
pen_dot.penup()
for point in rotated_points:
pen_dot.goto(point)
pen_dot.dot(5, 'blue violet')
Rotating(180, "(-8,-6) (-6,-3) (-3,-4)")
mainloop()

Drawing regular Polygon

A regular polygon with N vertices. The lower side of the polygon is parallel to x axis. Given two point (x1, y1) and (x2, y2) if we draw a line through these points then the line would be parallel to the x axis. That means the lower side of the polygon is given. How to find the other n - 2 points. Each point could have floating value but it is grantee that x1, y1, x2, y2 is integer.
As example if N = 5 and (x1, y1) = (0, 0) and (x2, y2) = (5, 0)
I have to find these remaining 3 points (6.545085, 4.755283), (2.500000, 7.694209), (-1.545085 4.755283)
I am trying with vector rotation, but can't figure out any solution. How can I calculate ?
Given points x1, y1, x2, y2, number N.
Middle point of this edge is
xm = (x1 + x2) / 2
ym = y1
The center of polygon has coordinates
xc = xm
yc = y1 + Abs(x1 - x2)/2 * Ctg(Pi/N)
Radius of circumcircle is (Edit: 0.5 coefficient was missed)
R = 0.5 * Abs(x1 - x2) / Sin(Pi/N)
Kth (k=3..N) vertice of polygon has coordinates
xk = xc + R * Cos(-Pi/2 + Pi/N + (k - 2) * 2 * Pi/ N)
yk = yc + R * Sin(-Pi/2 + Pi/N + (k - 2) * 2 * Pi/ N)

Resources