我有一个简单的椭圆形(红色),我想要旋转这个椭圆形。 以下代码只返回扁平化的椭圆形(蓝色),但它并没有旋转: import tkinter as tk import numpy as np import math
def rotate(points, angle):
new_points = list(points)
rad = angle * (math.pi/180)
cos_val = math.cos(rad)
sin_val = math.sin(rad)
for coords in new_points:
x_val = coords[0]
y_val = coords[1]
coords[0] = x_val * cos_val - y_val * sin_val
coords[1] = x_val * sin_val + y_val * cos_val
return new_points
window = tk.Tk()
window.geometry("500x300")
canvas = tk.Canvas(window, height=300, width=500)
canvas.grid(row=0, column=0, sticky='w')
# draw ellipse
size=80
x0,y0=100,100
width,height=0.25*size,0.5*size
x1,y1=x0+width,y0+height
xc=x0+width/2
yc=y0+height/2
ellipse = canvas.create_oval([x0, y0, x1,y1],fill='blue')
#draw rotated ellipse
coord1=canvas.coords(ellipse)
points=[[coord1[0],coord1[1]],[coord1[2],coord1[3]]]
point2=rotate(points,30)
coord2 = [item for sublist in point2 for item in sublist]
ellipse2 = canvas.create_oval(coord2,fill='red')
window.mainloop ()
这是结果: 红色椭圆应该被旋转30度,但实际上它只是被压扁了。
问题:如何在tkinter画布中旋转椭圆?
注:
- 我使用的是Python 3.6 - 我查看了类似问题的stackoverflow,但没有正确的答案。 - 不同于我们可以简单地旋转每个顶点的多边形,椭圆没有顶点。