''' Created on Sep 4, 2013 @author: James ''' import Point import LineSegment from sre_constants import RANGE ''' test method ''' arr = [] for i in range(0, 99): x = [] for j in range(0, 2): x.append(0) arr.append(x) for i in range(0,99): '''Fill with random lines''' point1 = Point.Point(0,0) point1.random() point2 = Point.Point(0,0) point2.random() point3 = Point.Point(0,0) point3.random() point4 = Point.Point(0,0) point4.random() arr[i][0] = LineSegment.MyClass(point1,point2) arr[i][1] = LineSegment.MyClass(point3,point4) print(" Line 1 [ " + str(point1.getX()) + "," + str(point1.getY()) + " ]" + " [ " + str(point2.getX()) + "," + str(point2.getY()) + " ]") print(" Line 2 [ " + str(point3.getX()) + "," + str(point3.getY()) + " ]" + " [ " + str(point4.getX()) + "," + str(point4.getY()) + " ]") intersect = LinesIntersect(arr[i][0], arr[i][1]) print(" Intersect: " + intersect ); def LinesIntersect ( line1, line2): '''First determine if Parrallel''' if (line1.getSlope() == line2.getSlope()): return False else: ''' Lines are not parallel, check if point of intersection is in range First find the y intercepts''' intercept1 = line1.getA().getY() - line1.getSlope * line1.getA.getX() intercept2 = line2.getA().getY() - line2.getSlope * line2.getA.getX() ''' Next find the intersects''' intersectX = (intercept1/intercept2) / ( line1.getSlope - line2.getSlope) intersectY = line1.getSlope * intersectX + intercept1 if ( intersectX > line1.getA().getX().MAX_X or intersectX < 0 or intersectY > line1.getA().getY().MAX_Y or intersectY < 0): return False else: return True

''' Created on Sep 4, 2013 @author: James ''' ''' A line segment should consists of two points: A and B ''' class MyClass(object): '''Constructor Takes 2 points as an argument''' def __init__(self, pointA, pointB): self.A = pointA self.B = pointB ''' Returns point A''' def getA(self): return self.pointA ''' Returns point B''' def getB(self): return self.pointB ''' Sets point A as the point passed through the argument''' def setA(self, point): self.A = point ''' Sets point B as the point passed through an argument''' def setB(self, point): self.B = point ''' Returns the slope of the line''' def getSlope(self): slope = (self.getA().getY() - self.getB().getY())/(self.getA().getX() - self.getB().getX()) return slope

This post has been edited by **andrewsw**: 05 September 2013 - 04:24 PM