3
import gtk,gtk.gdk,math,pango #Numeric,
10
w.draw_line(g, int(x), int(y), int(x), int(y))
12
def draw_handle(w, h, name = ""):
15
w.draw_line(g, int(x-3), int(y), int(x+3), int(y))
16
w.draw_line(g, int(x), int(y-3), int(x), int(y+3))
17
templayout.set_text(name)
18
w.draw_layout(g, x, y, templayout)
20
def draw_ray(w, h, d):
23
w.draw_line(g, int(h.x), int(h.y), int(3*d.x-2*h.x), int(3*d.y-2*h.y))
25
def intersect(n0, d0, n1, d1):
26
denominator = n0.x*n1.y - n0.y*n1.x
27
X = n1.y * d0 - n0.y * d1
31
Y = n0.x * d1 - n1.x * d0
33
return handle(X / denominator, Y / denominator)
35
def seg(a0, a1, b0, b1):
36
n0 = handle(a1.y - a0.y, -a1.x + a0.x)
37
d0 = n0.x*a0.x + n0.y *a0.y
38
n1 = handle(b1.y - b0.y, -b1.x + b0.x)
39
d1 = n1.x*b0.x + n1.y *b0.y
41
return intersect(n0, d0, n1, d1)
45
w.draw_line(g, h[0].x, h[0].y, h[1].x, h[1].y)
46
w.draw_line(g, h[3].x, h[3].y, h[4].x, h[4].y)
47
w.draw_line(g, h[3].x, h[3].y, h[2].x, h[2].y)
48
w.draw_line(g, h[2].x, h[2].y, h[1].x, h[1].y)
50
c = seg(h[0], h[1], h[3], h[4])
55
w.draw_line(g, h[i].x, h[i].y, h[(i+1)%6].x, h[(i+1)%6].y)
61
for i in range(0, 101):
65
nx = (1-t)*h[0].x + t*h[3].x
66
ny = (1-t)*h[0].y + t*h[3].y
67
#w.draw_line(g, 2*cx-nx, 2*cy-ny, nx, ny)
68
c1 = seg(handle(2*cx-nx, 2*cy-ny), handle(nx, ny), h[0], h[2])
70
c2 = seg(handle(2*cx-nx, 2*cy-ny), handle(nx, ny), h[4], h[2])
72
#draw_ray(w, h[3], c1)
73
#draw_ray(w, h[1], c2)
74
six = seg(c1, h[3], c2, h[1])
77
w.draw_line(g, ox, oy, six.x, six.y)
81
r = math.hypot(h[0].x - cx, h[0].y - cy)
82
s = math.atan2(h[0].y - h[3].y, h[0].x - h[3].x)
83
e = math.atan2(h[1].y - h[4].y, h[1].x - h[4].x)
84
for i in range(0, 101):
86
nx, ny = r*math.cos(t)+cx, r*math.sin(t)+cy
87
sx, sy = r*math.cos(t+math.pi)+cx, r*math.sin(t+math.pi)+cy
88
w.draw_line(g, sx, sy, nx, ny)
92
def __init__(self, x, y):
96
return "handle(%f, %f)" % (self.x, self.y)
98
handles = [handle(145.000000, 50.000000), handle(43.000000, 69.000000), handle(26.000000, 135.000000), handle(48.000000, 189.000000), handle(248.000000, 188.000000)]
100
selected_handle = None
103
g = gtk.gdk.GC(da.window)
109
draw_handle(da.window, h, str(i))
111
draw_elip(da.window, handles)
113
def mouse_event(w, e):
114
global selected_handle
117
if math.hypot(e.x-h.x, e.y - h.y) < 5:
118
selected_handle = (h, (e.x-h.x, e.y-h.y))
120
def mouse_release_event(w, e):
121
global selected_handle
122
selected_handle = None
124
def mouse_motion_event(w, e):
125
global selected_handle
127
h, (ox, oy) = selected_handle
128
if(e.state & gtk.gdk.BUTTON1_MASK):
135
win.set_default_size(400,400)
138
da = gtk.DrawingArea()
139
templayout = da.create_pango_layout("")
140
da.connect("expose_event", display)
141
da.add_events(gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK | gtk.gdk.KEY_PRESS_MASK | gtk.gdk.POINTER_MOTION_MASK)
142
da.connect("button-press-event", mouse_event)
143
da.connect("button-release-event", mouse_release_event)
144
da.connect("motion-notify-event", mouse_motion_event)
145
#da.connect("key_press_event", key_event)
148
win.connect("destroy", gtk.main_quit)