Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import *
- def run():
- print("Podaj wspolrzedne punktow do analizy w postaci [x, y]")
- a = eval (input("Podaj wspolrzedne pierwszego punktu:"))
- b = eval (input("Podaj wspolrzedne drugiego punktu:"))
- c = eval (input("Podaj wspolrzedne trzeciego punktu:"))
- p1 = Point(a[0], a[1])
- p2 = Point(b[0], b[1])
- p3 = Point(c[0], c[1])
- line = Line(p1,p2)
- dist = line.getDistanceToPoint(p3)
- if (dist == 0):
- print ("Punkty leza na jednej lini")
- else:
- print (" Punkty nie leza na jednej linii")
- class Point:
- def __init__(self, x, y):
- self.x = x
- self.y = y
- class Line:
- #Constructor, accepts two points as an arguemnts
- def __init__(self, p1 ,p2):
- self.p1 = p1
- self.p2 = p2
- self.is_valid = True
- self.is_vertical = False
- self.a = 0
- self.b = 0
- if p1.x is p2.x and p1.y is p2.y:
- self.is_valid = False
- elif p1.x is p2.x:
- self.is_vertical = True
- self.vertical_pos = p1.x
- else:
- self.a = (p1.y - p2.y) / (p1.x - p2.x)
- self.b = (p1.y - self.a * p1.x )
- def getDistanceToPoint(self, point):
- if not self.is_valid:
- return None
- if not self.is_vertical:
- coefA = -self.a
- coefB = 1
- coefC = -self.b
- dist = abs(coefA * point.x + coefB * point.y + coefC) / sqrt(pow(coefA,2) + pow(coefB,2))
- return dist
- else:
- print("calculating vertical line")
- dist = abs (self.p1.x - point.x)
- return dist
- def runTest1():
- a = Point(0,2)
- b = Point(5,2)
- c = Point(3,3)
- line = Line(a,b)
- print ("Line \"a\"" + str(line.a) )
- print ("C dist from line = " + str(line.getDistanceToPoint(c)))
- def runTest2():
- a = Point(0,0)
- b = Point(1,1)
- c = Point(0,1)
- line = Line(a,b)
- print ("Line \"a\"" + str(line.a) )
- print ("C dist from line = " + str(line.getDistanceToPoint(c)))
- def runTest2():
- a = Point(0,0)
- b = Point(1,1)
- c = Point(0,1)
- line = Line(a,b)
- print ("Line \"a\"" + str(line.a) )
- print ("C dist from line = " + str(line.getDistanceToPoint(c)))
- def runTest3():
- a = Point(0,0)
- b = Point(0,1)
- c = Point(1,0)
- line = Line(a,b)
- if line.is_vertical is True:
- print("Line is vertical")
- print ("Line \"a\"" + str(line.a) )
- print ("C dist from line = " + str(line.getDistanceToPoint(c)))
- run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement