diff options
Diffstat (limited to 'src/python')
-rw-r--r-- | src/python/cy2geom_example.py | 10 | ||||
-rw-r--r-- | src/python/elip.py | 155 | ||||
-rw-r--r-- | src/python/exact-arc-length-quad-bez.py | 16 | ||||
-rw-r--r-- | src/python/test_py2geom.py | 41 |
4 files changed, 222 insertions, 0 deletions
diff --git a/src/python/cy2geom_example.py b/src/python/cy2geom_example.py new file mode 100644 index 0000000..aca7dca --- /dev/null +++ b/src/python/cy2geom_example.py @@ -0,0 +1,10 @@ +#!/usr/bin/python + +import cy2geom +from cy2geom import * + +a = Point(1,2) +b = Point(31,2) +print a, b +print Point.dot(a,b) +print Point.unit_vector(a) diff --git a/src/python/elip.py b/src/python/elip.py new file mode 100644 index 0000000..9809db8 --- /dev/null +++ b/src/python/elip.py @@ -0,0 +1,155 @@ +#!/usr/bin/python + +import gtk,gtk.gdk,math,pango #Numeric, + +templayout = None + +def draw_spot(w, h): + x,y = h.x, h.y + g = gtk.gdk.GC(w) + w.draw_line(g, int(x), int(y), int(x), int(y)) + +def draw_handle(w, h, name = ""): + x,y = h.x, h.y + g = gtk.gdk.GC(w) + w.draw_line(g, int(x-3), int(y), int(x+3), int(y)) + w.draw_line(g, int(x), int(y-3), int(x), int(y+3)) + templayout.set_text(name) + w.draw_layout(g, x, y, templayout) + +def draw_ray(w, h, d): + x,y = h.x, h.y + g = gtk.gdk.GC(w) + w.draw_line(g, int(h.x), int(h.y), int(3*d.x-2*h.x), int(3*d.y-2*h.y)) + +def intersect(n0, d0, n1, d1): + denominator = n0.x*n1.y - n0.y*n1.x + X = n1.y * d0 - n0.y * d1 + if denominator == 0: + return None; + + Y = n0.x * d1 - n1.x * d0 + + return handle(X / denominator, Y / denominator) + +def seg(a0, a1, b0, b1): + n0 = handle(a1.y - a0.y, -a1.x + a0.x) + d0 = n0.x*a0.x + n0.y *a0.y + n1 = handle(b1.y - b0.y, -b1.x + b0.x) + d1 = n1.x*b0.x + n1.y *b0.y + + return intersect(n0, d0, n1, d1) + +def draw_elip(w, h): + g = gtk.gdk.GC(w) + w.draw_line(g, h[0].x, h[0].y, h[1].x, h[1].y) + w.draw_line(g, h[3].x, h[3].y, h[4].x, h[4].y) + w.draw_line(g, h[3].x, h[3].y, h[2].x, h[2].y) + w.draw_line(g, h[2].x, h[2].y, h[1].x, h[1].y) + + c = seg(h[0], h[1], h[3], h[4]) + draw_handle(w, c) + + if 0: + for i in range(6): + w.draw_line(g, h[i].x, h[i].y, h[(i+1)%6].x, h[(i+1)%6].y) + + + cx,cy = c.x, c.y + + ox, oy = None, None + for i in range(0, 101): + t = i/100.0 + + + nx = (1-t)*h[0].x + t*h[3].x + ny = (1-t)*h[0].y + t*h[3].y + #w.draw_line(g, 2*cx-nx, 2*cy-ny, nx, ny) + c1 = seg(handle(2*cx-nx, 2*cy-ny), handle(nx, ny), h[0], h[2]) + #draw_handle(w, c1) + c2 = seg(handle(2*cx-nx, 2*cy-ny), handle(nx, ny), h[4], h[2]) + #draw_handle(w, c2) + #draw_ray(w, h[3], c1) + #draw_ray(w, h[1], c2) + six = seg(c1, h[3], c2, h[1]) + #draw_spot(w, six) + if ox: + w.draw_line(g, ox, oy, six.x, six.y) + ox, oy = six.x, six.y + return + + r = math.hypot(h[0].x - cx, h[0].y - cy) + s = math.atan2(h[0].y - h[3].y, h[0].x - h[3].x) + e = math.atan2(h[1].y - h[4].y, h[1].x - h[4].x) + for i in range(0, 101): + t = (e-s)*i/100.0 + s + nx, ny = r*math.cos(t)+cx, r*math.sin(t)+cy + sx, sy = r*math.cos(t+math.pi)+cx, r*math.sin(t+math.pi)+cy + w.draw_line(g, sx, sy, nx, ny) + + +class handle: + def __init__(self, x, y): + self.x = x + self.y = y + def __repr__(self): + return "handle(%f, %f)" % (self.x, self.y) + +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)] + +selected_handle = None + +def display(da, ev): + g = gtk.gdk.GC(da.window) + i = 0 + + + + for h in handles: + draw_handle(da.window, h, str(i)) + i += 1 + draw_elip(da.window, handles) + +def mouse_event(w, e): + global selected_handle + if e.button == 1: + for h in handles: + if math.hypot(e.x-h.x, e.y - h.y) < 5: + selected_handle = (h, (e.x-h.x, e.y-h.y)) + +def mouse_release_event(w, e): + global selected_handle + selected_handle = None + +def mouse_motion_event(w, e): + global selected_handle + if selected_handle: + h, (ox, oy) = selected_handle + if(e.state & gtk.gdk.BUTTON1_MASK): + h.x = e.x - ox + h.y = e.y - oy + w.queue_draw() + + +win = gtk.Window() +win.set_default_size(400,400) +vb = gtk.VBox(False) + +da = gtk.DrawingArea() +templayout = da.create_pango_layout("") +da.connect("expose_event", display) +da.add_events(gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK | gtk.gdk.KEY_PRESS_MASK | gtk.gdk.POINTER_MOTION_MASK) +da.connect("button-press-event", mouse_event) +da.connect("button-release-event", mouse_release_event) +da.connect("motion-notify-event", mouse_motion_event) +#da.connect("key_press_event", key_event) +win.add(vb) +vb.pack_start(da) +win.connect("destroy", gtk.main_quit) + + +win.show_all() + +gtk.main() + +print handles diff --git a/src/python/exact-arc-length-quad-bez.py b/src/python/exact-arc-length-quad-bez.py new file mode 100644 index 0000000..fd2f4b3 --- /dev/null +++ b/src/python/exact-arc-length-quad-bez.py @@ -0,0 +1,16 @@ +import math + +def q(x, a, b, c): + sa = math.sqrt(a) + y = math.sqrt(a*x*x+b*x+c) + dp = 2*a*x + b + r = dp*y/(4*a) + t = abs(dp + 2*y*sa) + s = (4*a*c-b*b)/(a*sa*8) + return r+s*math.log(t) + +a = 1160390 +b = -922658 +c = 249477 +print q(1, a,b,c) - q(0,a,b,c) + diff --git a/src/python/test_py2geom.py b/src/python/test_py2geom.py new file mode 100644 index 0000000..2ad6e66 --- /dev/null +++ b/src/python/test_py2geom.py @@ -0,0 +1,41 @@ +#!/usr/bin/python + +import py2geom as g + +a = g.Point(1,2) +b = g.Point(31,2) +print a, b + +point_fns_1 = ["L1", "L2", "L2sq", "LInfty", "is_zero", "is_unit_vector", + "atan2", "rot90", + "unit_vector", "abs"] +point_fns_2 = ["dot", "angle_between", "distance", "distanceSq", "cross"] + +for i in point_fns_1: + print "%s:" % i, g.__dict__[i](a) +for i in point_fns_2: + print "%s:" % i, g.__dict__[i](a,b) +print "a == b", a == b +print "Lerp:", g.lerp(0.3, a,b) + +bo = g.BezOrd(2,3) +print bo +print bo.point_at(0.3) + +print bo.reverse() + +sn = g.sin(g.BezOrd(0.0,8.0),5) +print sn +print g.inverse(sn,10) +print list(sn) + +r_sn = g.roots(sn) +print len(r_sn) +print list(r_sn) + +bo = g.BezOrd(-1,1) +sb = g.SBasis() +print sb +print list(g.roots(sb)) +sb.append(bo) +print list(g.roots(sb)) |