From d2f01601ced94bfcf0ce542812a9ab3d385d009e Mon Sep 17 00:00:00 2001 From: Skyler Lehmkuhl Date: Wed, 11 Jan 2012 14:27:05 -0500 Subject: [PATCH] Added drawing functionality - both line and smooth --- lightningbeam | 6 ++++ misc_funcs.py | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/lightningbeam b/lightningbeam index 1794ce3..233da6e 100755 --- a/lightningbeam +++ b/lightningbeam @@ -85,6 +85,12 @@ def onMouseUpGroup(self, x, y): self.clicked = False if svlgui.MODE in ["r", "e"]: self.cshape = None + elif svlgui.MODE=="p": + print len(self.cshape.shapedata) + self.cshape.shapedata = misc_funcs.simplify_shape(self.cshape.shapedata, "curve",1) + print len(self.cshape.shapedata) + self.cshape = None + MainWindow.stage.draw() def onMouseUpObj(self, x, y): self.clicked = False def onMouseMoveGroup(self, x, y): diff --git a/misc_funcs.py b/misc_funcs.py index af73053..0e81aa7 100644 --- a/misc_funcs.py +++ b/misc_funcs.py @@ -5,6 +5,7 @@ import svlgui from threading import Event, Thread +import math def select_any(self): svlgui.MODE = " " @@ -52,6 +53,90 @@ def lastval(arr,index): if i: return i + + + + +def catmullRom2bezier( points ) : + #crp = points.split(/[,\s]/); + crp = points + d = [["M",points[0][1],points[0][2]]]; + ''' + i = 0 + iLen = len(crp); + while iLen - 2 > i: + i+=2 + p = []; + if 0 == i: + p.append( [crp[i], crp[i+1]); + p.append( [crp[i], crp[i+1]); + p.append( [crp[i+2], crp[i+3]); + p.append( [crp[i+4], crp[i+5]); + elif iLen - 4 == i: + p.append( [crp[i-2], crp[i-1]]); + p.append( [crp[i], crp[i+1]]); + p.append( [crp[i+2], crp[i+3]]); + p.append( [crp[i+2], crp[i+3]]); + } else { + p.append( [crp[i-2], crp[i-1]]); + p.append( [crp[i], crp[i+1]]); + p.append( [crp[i+2], crp[i+3]]); + p.append( [crp[i+4], crp[i+5]]); + } + ''' + for i in range(2,len(crp)-2): + p = [ [crp[i-1][1],crp[i-1][2]], [crp[i][1],crp[i][2]], [crp[i+1][1],crp[i+1][2]], [crp[i+2][1],crp[i+2][2]] ] + + # Catmull-Rom to Cubic Bezier conversion matrix + # 0 1 0 0 + # -1/6 1 1/6 0 + # 0 1/6 1 -1/6 + # 0 0 1 0 + + bp = [] + bp.append( [p[1][0], p[1][1]] ); + bp.append( [(-p[0][0]+6*p[1][0]+ p[2][0])/6, ((-p[0][1]+6*p[1][1]+p[2][1])/6)]); + bp.append( [(p[1][0]+6*p[2][0]-p[3][0])/6, ((p[1][1]+6*p[2][1]-p[3][1])/6)]); + bp.append( [ p[2][0], p[2][1] ] ); + + + d.append( ["C", bp[1][0], bp[1][1], bp[2][0], bp[2][1], bp[3][0], bp[3][1]]); + + return d; + +def simplify_shape(shape,mode,iterations): + if mode in ("straight","curve"): + for i in xrange(iterations): + for j in reversed(range(len(shape))): + if j>0 and j(165-500/(ab+bc)): # at least 15 degrees away from straight angle + del shape[j] + if mode=="curve": + shape = catmullRom2bezier([shape[0]]*2+shape+[shape[-1]]) + print shape + + return shape#+nshape # Timer module - not mine