diff --git a/lightningbeam.py b/lightningbeam.py index a41c515..dfa59ef 100755 --- a/lightningbeam.py +++ b/lightningbeam.py @@ -219,6 +219,26 @@ def onMouseUpGroup(self, x, y,button=1,clicks=1): lastline = l self.lines.append(l) self.delete(self.activelayer.frames[self.currentframe].objs[-1]) + for line in self.lines: + for otherline in self.lines: + if not otherline is line: + if line.connection1 and otherline in line.connection1.lines: continue + if line.connection2 and otherline in line.connection2.lines: continue + inter = line.intersects(otherline) + if inter: + print "INTERSECTION" + inter = svlgui.Point(*inter) + l1 = svlgui.Line(line.endpoint1,inter,line.connection1,inter) + l2 = svlgui.Line(line.endpoint2,inter,line.connection2,inter) + l3 = svlgui.Line(otherline.endpoint1,inter,otherline.connection1,inter) + l4 = svlgui.Line(otherline.endpoint2,inter,otherline.connection2,inter) + inter.lines.add(l1) + inter.lines.add(l2) + inter.lines.add(l3) + inter.lines.add(l4) + self.lines[self.lines.index(line):self.lines.index(line)+1]=[l1,l2] + self.lines[self.lines.index(otherline):self.lines.index(otherline)+1]=[l3,l4] + break '''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 diff --git a/svlgui.py b/svlgui.py index f36f4d0..6ecafe8 100644 --- a/svlgui.py +++ b/svlgui.py @@ -3128,6 +3128,52 @@ class Line(object): cz = x1*y2-y1*x2 if cz>0: angle=360-angle return angle + def intersects(self,other): + '''def IsOnLeft(a,b,c): + return Area2(a,b,c) > 0 + def IsOnRight(a,b,c): + return Area2(a,b,c) < 0 + def IsCollinear(a,b,c): + return Area2(a,b,c) == 0 + def Area2 (a,b,c): + return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y) + if (IsOnLeft(self.endpoint1,self.endpoint2,other.endpoint1) and IsOnRight(self.endpoint1,self.endpoint2,other.endpoint2)) + or (IsOnLeft(self.endpoint1,self.endpoint2,other.endpoint2) and IsOnRight(self.endpoint1,self.endpoint2,other.endpoint1): + if (IsOnLeft(other.endpoint1,other.endpoint2,self.endpoint1) and IsOnRight(other.endpoint1,other.endpoint2,self.endpoint2)) + or (IsOnLeft(other.endpoint1,other.endpoint2,self.endpoint2) and IsOnRight(other.endpoint1,other.endpoint2,self.endpoint1): + return True''' + # Formula for line is y = mx + b + try: + sm = (self.endpoint1.y-self.endpoint2.y)/(self.endpoint1.x-self.endpoint1.y) + om = (other.endpoint1.y-other.endpoint2.y)/(other.endpoint1.x-other.endpoint1.y) + sb = self.endpoint1.y-sm*self.endpoint1.x + ob = other.endpoint1.y-sm*other.endpoint1.x + if sm == om: return False + x = (ob-sb)/(sm-om) + y = sm*x + sb + if min(self.endpoint1.x,self.endpoint2.x)