ich habe ein Programm geschrieben, dass einen Pfeil erzeugt, die Pfeilform abhängig von einem Pfeilfaktor self._r_divisor ist und man den Pfeil hin- und herbewegen kann.
Kann man dieses Problem mit einem sich drehenden Pfeil auch anders lösen?
Code: Alles auswählen
try:
import tkinter as tk
except:
import Tkinter as tk
import numpy as np
def polar_to_cartesian(radius, phi, x_0=0, y_0=0):
x = radius * np.cos(phi) + x_0
y = radius * np.sin(phi) + y_0
return x, y
def cartesian_to_polar(x, y):
phi = np.arctan(y/x)
radius = y/(np.sin(phi))
print ("myphi=%f, myrad=%f" %(phi, radius))
return radius, phi
def degree_to_radiant(degree):
radiant = (degree/180.)*np.pi
return radiant
def radiant_to_degree(phi):
degree=(phi/np.pi)*180.
return degree
class Arrow():
def __init__(self, canvas, x_0=100, y_0=100, radius=100, color='green'):
self._canvas = canvas
self._x_0 = x_0
self._y_0 = y_0
self._radius = radius
self._color = color
self._phi = 0
self._r_divisor=4.665
self._arrow_width = self._radius/self._r_divisor
# self._canvas = tk.Canvas()
self._canvas.create_oval(self._x_0-self._radius, self._y_0-self._radius, self._x_0+self._radius, self._y_0+self._radius)
self._create_arrow()
def _create_arrow(self):
self._x_1, self._y_1 = polar_to_cartesian(self._radius, self._phi, x_0=self._x_0, y_0=self._y_0)
self._x_2 = self._x_1-(self._radius/self._r_divisor)
self._y_2 = self._y_1+(self._radius/(2*self._r_divisor))
self._x_3 = self._x_1-(self._radius/self._r_divisor)
self._y_3 = self._y_1-(self._radius/(2*self._r_divisor))
self._arrowhead = self._canvas.create_polygon(self._x_1, self._y_1, # P1
self._x_2, self._y_2, # P2
self._x_3, self._y_3, # P3
fill='blue')
self._arrow = self._canvas.create_line(self._x_0,
self._y_0,
self._x_1,
self._y_1)
print ("x_0 ---> %d y_0 ---> %d" %(self._x_0, self._y_0))
print ("x_1 ---> %d y_1 ---> %d" %(self._x_1, self._y_1))
print ("x_2 ---> %d y_2 ---> %d" %(self._x_2, self._y_2))
print ("x_3 ---> %d y_3 ---> %d" %(self._x_3, self._y_3))
self._radius_arrow, self._phi_arrow = cartesian_to_polar(self._x_2-self._x_0, self._y_2-self._y_0)
print ("Radius ---> %d phi ---> %d" %(self._radius_arrow, self._phi_arrow))
def _calculate_coords(self):
self._x_1, self._y_1 = polar_to_cartesian(self._radius, self._phi, x_0=self._x_0, y_0=self._y_0)
self._x_2, self._y_2 = polar_to_cartesian(self._radius_arrow, self._phi_arrow+self._phi, x_0=self._x_0, y_0=self._y_0)
self._x_3, self._y_3 = polar_to_cartesian(self._radius_arrow, -self._phi_arrow+self._phi, x_0=self._x_0, y_0=self._y_0)
print ("x_0 ---> %d y_0 ---> %d" %(self._x_0, self._y_0))
print ("x_1 ---> %d y_1 ---> %d" %(self._x_1, self._y_1))
print ("x_2 ---> %d y_2 ---> %d" %(self._x_2, self._y_2))
print ("x_3 ---> %d y_3 ---> %d" %(self._x_3, self._y_3))
def set_phi(self, phi=0):
self._phi=phi
self._calculate_coords()
self._canvas.coords(self._arrow, self._x_0, self._y_0, self._x_1, self._y_1)
self._canvas.coords(self._arrowhead, self._x_1, self._y_1, self._x_2, self._y_2, self._x_3, self._y_3)
def set_degree(self, angle):
self.set_phi(degree_to_radiant(angle))
def get_phi(self):
return self._phi
def main ():
print ("in main...")
root = tk.Tk()
canvas_frame = tk.Frame(root)
canvas = tk.Canvas(canvas_frame)
canvas.pack()
myarrow = Arrow(canvas, x_0 = 120, y_0=120)
def do_exit():
root.quit()
root.destroy()
def do_move_arrow_pos():
phi = myarrow.get_phi()
degree = radiant_to_degree(phi)
degree += 10
phi = degree_to_radiant(degree)
myarrow.set_phi(phi)
def do_move_arrow_neg():
phi = myarrow.get_phi()
degree = radiant_to_degree(phi)
degree -= 10
phi = degree_to_radiant(degree)
myarrow.set_phi(phi)
button_frame = tk.Frame(root)
beenden_button = tk.Button(button_frame, text="Beenden", width=40, height=3, command=do_exit)
move_arrow_button_pos = tk.Button(button_frame, text="--->", width=40, height=3, command=do_move_arrow_pos)
move_arrow_button_neg = tk.Button(button_frame, text="<---", width=40, height=3, command=do_move_arrow_neg)
move_arrow_button_pos.pack()
move_arrow_button_neg.pack()
beenden_button.pack()
canvas_frame.grid(row=0, column=1)
button_frame.grid(row=0, column=2)
root.mainloop()
if __name__ == '__main__':
main ()
print ("Programm ende ...")