From 148d9f17823c897a8a06741bcd543b497669acf4 Mon Sep 17 00:00:00 2001 From: Skyler Lehmkuhl Date: Sat, 27 Apr 2013 16:18:25 -0400 Subject: [PATCH] Work on paint bucketing algorithm --- lightningbeam.py | 4 +++ misc_funcs.py | 22 ++++++++++++++ svlgui.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) diff --git a/lightningbeam.py b/lightningbeam.py index 0cd3137..551cd41 100755 --- a/lightningbeam.py +++ b/lightningbeam.py @@ -123,9 +123,11 @@ def onMouseDownGroup(self, x, y,button=1,clicks=1): for i in self.lines: if abs(self.cshape.endpoint1.x-i.endpoint1.x)<10 and abs(self.cshape.endpoint1.y-i.endpoint1.y)<10: self.cshape.connection1 = i.endpoint1 + self.cshape.connection1.lines.add(self.cshape) break elif abs(self.cshape.endpoint1.x-i.endpoint2.x)<10 and abs(self.cshape.endpoint1.y-i.endpoint2.y)<10: self.cshape.connection1 = i.endpoint2 + self.cshape.connection1.lines.add(self.cshape) break self.lines.append(self.cshape) return @@ -201,9 +203,11 @@ def onMouseUpGroup(self, x, y,button=1,clicks=1): for i in self.lines: if abs(self.cshape.endpoint2.x-i.endpoint1.x)<10 and abs(self.cshape.endpoint2.y-i.endpoint1.y)<10: self.cshape.connection2 = i.endpoint1 + self.cshape.connection2.lines.add(self.cshape) break elif abs(self.cshape.endpoint2.x-i.endpoint2.x)<10 and abs(self.cshape.endpoint2.y-i.endpoint2.y)<10: self.cshape.connection2 = i.endpoint2 + self.cshape.connection2.lines.add(self.cshape) break self.cshape = None MainWindow.stage.draw() diff --git a/misc_funcs.py b/misc_funcs.py index 336f251..d007ac2 100644 --- a/misc_funcs.py +++ b/misc_funcs.py @@ -96,8 +96,30 @@ def lastval(arr,index): return i +def angle_to_point(point1, point2): + deltaX = point2.x-point1.x + deltaY = point2.y-point1.y + angleInDegrees = math.atan2(-deltaY, deltaX) * 180 / math.pi + if angleInDegrees<0: angleInDegrees = 360+angleInDegrees + return angleInDegrees +def sqr(x) : + return x * x +def dist2(v, w): + return sqr(v.x - w.x) + sqr(v.y - w.y) +def distToSegmentSquared(p, v, w): + l2 = dist2(v, w) + if l2 == 0: + return dist2(p, v) + t = ((p.x - v.x) * (w.x - v.x) + (p.y - v.y) * (w.y - v.y)) / l2 + if t < 0: + return dist2(p, v) + if t > 1: + return dist2(p, w) + return dist2(p, svlgui.Point(x=(v.x+t*(w.x-v.x)), y=(v.y+t*(w.y-v.y)))) +def distToSegment(p, v, w): + return math.sqrt(distToSegmentSquared(p, v, w)) def catmullRom2bezier( points ) : #crp = points.split(/[,\s]/); diff --git a/svlgui.py b/svlgui.py index f7da3b5..9d6e4a0 100644 --- a/svlgui.py +++ b/svlgui.py @@ -2658,6 +2658,45 @@ class Group (object): i.endpoint2.y = y self.activepoint = i.endpoint2 return + elif MODE=="b": + nlines = [i for i in self.lines] + endsleft = True + while endsleft: + endsleft = False + print nlines + for i in reversed(nlines): + if not (i.endpoint1 in [j.endpoint1 for j in nlines if not j==i]+[j.endpoint2 for j in nlines if not j==i]): + nlines.remove(i) + endsleft = True + elif not (i.endpoint2 in [j.endpoint1 for j in nlines if not j==i]+[j.endpoint2 for j in nlines if not j==i]): + nlines.remove(i) + endsleft = True + if nlines: + mindist = sys.maxint + point = Point(x, y) + closestsegment = None + for i in nlines: + d = misc_funcs.distToSegment(point,i.endpoint1,i.endpoint2) + if d" class Line(object): @@ -2883,15 +2925,51 @@ class Line(object): super(Line, self).__init__() self.endpoint1 = endpoint1 self.endpoint2 = endpoint2 + self.endpoint1.lines.add(self) + self.endpoint2.lines.add(self) self.connection1 = connection1 self.connection2 = connection2 + if self.connection1: self.connection1.lines.add(self) + if self.connection2: self.connection2.lines.add(self) self.linecolor = LINECOLOR self.linewidth = LINEWIDTH + def __repr__(self): + return "" def assign(self, point, which): if which==1: self.connection1 = point + self.connection1.lines.add(self) elif which==2: self.connection2 = point + self.connection2.lines.add(self) + def angle(self, other): + if self.endpoint1==other.endpoint1: + x1 = self.endpoint2.x-self.endpoint1.x + y1 = self.endpoint2.y-self.endpoint1.y + x2 = other.endpoint2.x-other.endpoint1.x + y2 = other.endpoint2.y-other.endpoint1.y + elif self.endpoint2==other.endpoint1: + x1 = self.endpoint1.x-self.endpoint2.x + y1 = self.endpoint1.y-self.endpoint2.y + x2 = other.endpoint2.x-other.endpoint1.x + y2 = other.endpoint2.y-other.endpoint1.y + elif self.endpoint1==other.endpoint2: + x1 = self.endpoint2.x-self.endpoint1.x + y1 = self.endpoint2.y-self.endpoint1.y + x2 = other.endpoint1.x-other.endpoint2.x + y2 = other.endpoint1.y-other.endpoint2.y + elif self.endpoint2==other.endpoint2: + x1 = self.endpoint1.x-self.endpoint2.x + y1 = self.endpoint1.y-self.endpoint2.y + x2 = other.endpoint1.x-other.endpoint2.x + y2 = other.endpoint1.y-other.endpoint2.y + dot = x1*x2+y1*y2 + mag1 = math.sqrt(x1**2+y1**2) + mag2 = math.sqrt(x2**2+y2**2) + angle = math.acos(dot/(mag1*mag2))/math.pi*180 + cz = x1*y2-y1*x2 + if cz>0: angle=360-angle + return angle def draw(self, cr=None, transform=None, rect=None): if self.connection1: self.endpoint1 = self.connection1