from MyPoint import * class MyLine: def __init__(self): self.a = MyPoint() self.b = MyPoint() self.intersecterror = 0 def pointonline(self, apoint): if ((self.a.x >= apoint.x) and (self.b.x <= apoint.x)) or ((self.b.x >= apoint.x) and (self.a.x <= apoint.x)): if ((self.a.y >= apoint.y) and (self.b.y <= apoint.y)) or ((self.b.y >= apoint.y) and (self.a.y <= apoint.y)): return 1 return 0 def intersect(self, anotherline): self.intersecterror = 0 interpoint = MyPoint() self.a.x = self.a.x * 1.0 self.a.y = self.a.y * 1.0 self.b.x = self.b.x * 1.0 self.b.y = self.b.y * 1.0 anotherline.a.x = anotherline.a.x * 1.0 anotherline.a.y = anotherline.a.y * 1.0 anotherline.b.x = anotherline.b.x * 1.0 anotherline.b.y = anotherline.b.y * 1.0 if anotherline.a.x == anotherline.b.x: interpoint.x = anotherline.a.x if self.a.y == self.b.y: interpoint.y = self.a.y else: if self.a.x == self.b.x: self.intersecterror = -1 else: slope = (self.b.y - self.a.y)/(self.b.x - self.a.x) interpoint.y = slope*interpoint.x-slope*self.b.x+self.b.y else: line_slope = (anotherline.b.y - anotherline.a.y)/(anotherline.b.x - anotherline.a.x) if self.a.x == self.b.x: interpoint.x = self.a.x interpoint.y = anotherline.b.y - (line_slope*(anotherline.b.x - interpoint.x)) else: slope = (self.b.y - self.a.y)/(self.b.x - self.a.x) if slope == 0: if line_slope == 0: self.intersecterror = -1 else: interpoint.y = self.a.y interpoint.x = (interpoint.y-anotherline.b.y+line_slope*anotherline.b.x)/line_slope else: if line_slope == 0: interpoint.y = anotherline.b.y else: if slope == line_slope: self.intersecterror = -1 n1 = 1+(slope/line_slope) n2 = self.b.y n3 = slope*anotherline.b.x n4 = slope*self.b.x n5 = (slope*self.a.x)/line_slope interpoint.y = (n2-n3+n4+n5)/n1 interpoint.x = (interpoint.y - self.b.y + slope * self.b.x)/ slope return interpoint