Advertisement
Guest User

Untitled

a guest
Aug 21st, 2015
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.61 KB | None | 0 0
  1.  
  2.  
  3. from math import *
  4.  
  5. def run():
  6.     print("Podaj wspolrzedne punktow do analizy w postaci [x, y]")
  7.     a = eval (input("Podaj wspolrzedne pierwszego punktu:"))
  8.  
  9.  
  10.  
  11.     b = eval (input("Podaj wspolrzedne drugiego punktu:"))
  12.     c = eval (input("Podaj wspolrzedne trzeciego punktu:"))
  13.     p1 = Point(a[0], a[1])
  14.     p2 = Point(b[0], b[1])
  15.     p3 = Point(c[0], c[1])
  16.  
  17.     line = Line(p1,p2)
  18.  
  19.     dist = line.getDistanceToPoint(p3)
  20.  
  21.     if (dist == 0):
  22.         print ("Punkty leza na jednej lini")
  23.     else:
  24.         print (" Punkty nie leza na jednej linii")
  25.  
  26.  
  27.  
  28.  
  29. class Point:
  30.     def __init__(self, x, y):
  31.         self.x = x
  32.         self.y = y
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39. class Line:
  40.     #Constructor, accepts two points as an arguemnts
  41.     def __init__(self, p1 ,p2):
  42.         self.p1 = p1
  43.         self.p2 = p2
  44.         self.is_valid = True
  45.         self.is_vertical = False
  46.         self.a = 0
  47.         self.b = 0
  48.         if p1.x is p2.x and p1.y is p2.y:
  49.             self.is_valid = False
  50.         elif p1.x is p2.x:
  51.             self.is_vertical = True
  52.             self.vertical_pos = p1.x
  53.         else:
  54.             self.a = (p1.y - p2.y) / (p1.x - p2.x)
  55.             self.b = (p1.y - self.a * p1.x   )
  56.  
  57.  
  58.     def  getDistanceToPoint(self, point):
  59.         if not self.is_valid:
  60.             return None
  61.         if not self.is_vertical:
  62.             coefA = -self.a
  63.             coefB = 1
  64.             coefC = -self.b
  65.  
  66.             dist = abs(coefA * point.x + coefB * point.y + coefC)  / sqrt(pow(coefA,2) + pow(coefB,2))
  67.             return  dist
  68.         else:
  69.             print("calculating vertical line")
  70.             dist = abs (self.p1.x - point.x)
  71.             return dist
  72.  
  73.  
  74. def runTest1():
  75.     a = Point(0,2)
  76.     b = Point(5,2)
  77.     c = Point(3,3)
  78.  
  79.     line = Line(a,b)
  80.  
  81.     print ("Line \"a\"" + str(line.a) )
  82.  
  83.     print ("C dist from line = " + str(line.getDistanceToPoint(c)))
  84.  
  85.  
  86. def runTest2():
  87.     a = Point(0,0)
  88.     b = Point(1,1)
  89.     c = Point(0,1)
  90.  
  91.     line = Line(a,b)
  92.  
  93.     print ("Line \"a\"" + str(line.a) )
  94.  
  95.     print ("C dist from line = " + str(line.getDistanceToPoint(c)))
  96.  
  97.  
  98. def runTest2():
  99.     a = Point(0,0)
  100.     b = Point(1,1)
  101.     c = Point(0,1)
  102.  
  103.     line = Line(a,b)
  104.  
  105.     print ("Line \"a\"" + str(line.a) )
  106.  
  107.     print ("C dist from line = " + str(line.getDistanceToPoint(c)))
  108.  
  109.  
  110. def runTest3():
  111.     a = Point(0,0)
  112.     b = Point(0,1)
  113.     c = Point(1,0)
  114.  
  115.     line = Line(a,b)
  116.  
  117.     if line.is_vertical is True:
  118.         print("Line is vertical")
  119.  
  120.     print ("Line \"a\"" + str(line.a) )
  121.  
  122.     print ("C dist from line = " + str(line.getDistanceToPoint(c)))
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129. run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement