Python intersection of two lines
(Geometry: intersection) Suppose two line segments intersect. The two endpoints for the first line segment are (x1, y1) and (x2, y2) and for the second line segment are (x3, y3) and (x4, y4). Write a program that prompts the user to enter these four endpoints and displays the intersecting point.
class Intersection():
def __init__(self, x1, y1, x2, y2, x3, y3, x4, y4, a = 1, b = 1, c = 1, d = 1, e = 1, f = 1):
self.__x1 = x1
self.__x2 = x2
self.__x3 = x3
self.__x4 = x4
self.__y1 = y1
self.__y2 = y2
self.__y3 = y3
self.__y4 = y4
self.__a = a
self.__b = b
self.__c = c
self.__d = d
self.__e = e
self.__f = f
def getA(self):
return self.__a
def getB(self):
return self.__b
def getC(self):
return self.__c
def getD(self):
return self.__d
def getE(self):
return self.__e
def getF(self):
return self.__f
def isSolvable(self):
if Intersection.getA(self) * Intersection.getD(self) - Intersection.getB(self) * Intersection.getC(self) != 0:
return True
else:
return False
def getX(self):
if Intersection.isSolvable(self) == False:
return 'The equation has no solution!'
else:
x = (Intersection.getE(self) * Intersection.getD(self) - Intersection.getB(self) * \
Intersection.getF(self)) / (Intersection.getA(self) * Intersection.getD(self) - \
Intersection.getB(self) * Intersection.getC(self))
return x
def getY(self):
if Intersection.isSolvable(self) == False:
return 'The equation has no solution!'
else:
y = (Intersection.getA(self) * Intersection.getF(self) - Intersection.getE(self) * \
Intersection.getC(self)) / (Intersection.getA(self) * Intersection.getD(self) - \
Intersection.getB(self) * Intersection.getC(self))
return y
def getx1(self):
return self.__x1
def getx2(self):
return self.__x2
def getx3(self):
return self.__x3
def getx4(self):
return self.__x4
def gety1(self):
return self.__y1
def gety2(self):
return self.__y2
def gety3(self):
return self.__y3
def gety4(self):
return self.__y4
def setA(self):
self.__a = self.__a * -1
def setC(self):
self.__c = self.__c * -1
def getA1(self):
self.__a = (Intersection.gety2(self) - Intersection.gety1(self)) / (Intersection.getx2(self) - Intersection.getx1(self))
return self.__a
def getC1(self):
self.__c = (Intersection.gety4(self) - Intersection.gety3(self)) / (Intersection.getx4(self) - Intersection.getx3(self))
return self.__c
def getE1(self):
self.__e = -(Intersection.getA1(self)) * Intersection.getx1(self) + (Intersection.gety1(self))
return self.__e
def getF1(self):
self.__f = -(Intersection.getC1(self) * Intersection.getx3(self)) + (Intersection.gety3(self))
return self.__f
def main():
i = Intersection(2, 2, 0, 0, 0, 2, 2, 0)
i.getA1()
i.getC1()
i.getE1()
i.getF1()
i.setA()
i.setC()
print('X = ', i.getX(), ' y = ', i.getY())
def __init__(self, x1, y1, x2, y2, x3, y3, x4, y4, a = 1, b = 1, c = 1, d = 1, e = 1, f = 1):
self.__x1 = x1
self.__x2 = x2
self.__x3 = x3
self.__x4 = x4
self.__y1 = y1
self.__y2 = y2
self.__y3 = y3
self.__y4 = y4
self.__a = a
self.__b = b
self.__c = c
self.__d = d
self.__e = e
self.__f = f
def getA(self):
return self.__a
def getB(self):
return self.__b
def getC(self):
return self.__c
def getD(self):
return self.__d
def getE(self):
return self.__e
def getF(self):
return self.__f
def isSolvable(self):
if Intersection.getA(self) * Intersection.getD(self) - Intersection.getB(self) * Intersection.getC(self) != 0:
return True
else:
return False
def getX(self):
if Intersection.isSolvable(self) == False:
return 'The equation has no solution!'
else:
x = (Intersection.getE(self) * Intersection.getD(self) - Intersection.getB(self) * \
Intersection.getF(self)) / (Intersection.getA(self) * Intersection.getD(self) - \
Intersection.getB(self) * Intersection.getC(self))
return x
def getY(self):
if Intersection.isSolvable(self) == False:
return 'The equation has no solution!'
else:
y = (Intersection.getA(self) * Intersection.getF(self) - Intersection.getE(self) * \
Intersection.getC(self)) / (Intersection.getA(self) * Intersection.getD(self) - \
Intersection.getB(self) * Intersection.getC(self))
return y
def getx1(self):
return self.__x1
def getx2(self):
return self.__x2
def getx3(self):
return self.__x3
def getx4(self):
return self.__x4
def gety1(self):
return self.__y1
def gety2(self):
return self.__y2
def gety3(self):
return self.__y3
def gety4(self):
return self.__y4
def setA(self):
self.__a = self.__a * -1
def setC(self):
self.__c = self.__c * -1
def getA1(self):
self.__a = (Intersection.gety2(self) - Intersection.gety1(self)) / (Intersection.getx2(self) - Intersection.getx1(self))
return self.__a
def getC1(self):
self.__c = (Intersection.gety4(self) - Intersection.gety3(self)) / (Intersection.getx4(self) - Intersection.getx3(self))
return self.__c
def getE1(self):
self.__e = -(Intersection.getA1(self)) * Intersection.getx1(self) + (Intersection.gety1(self))
return self.__e
def getF1(self):
self.__f = -(Intersection.getC1(self) * Intersection.getx3(self)) + (Intersection.gety3(self))
return self.__f
def main():
i = Intersection(2, 2, 0, 0, 0, 2, 2, 0)
i.getA1()
i.getC1()
i.getE1()
i.getF1()
i.setA()
i.setC()
print('X = ', i.getX(), ' y = ', i.getY())
main()
SOLUTION:
X = 1.0 y = 1.0
Коментари
Постави коментар