Advertisement
roman_gemini

Geometry

Jun 17th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1. import math
  2.  
  3. class Point:
  4.  
  5.     def __init__(self, x, y):
  6.         self.x = x
  7.         self.y = y
  8.  
  9.     def __repr__(self):
  10.         return "Point(x: " + str(self.x) + ", y: " + str(self.y) + ")"
  11.  
  12.     def distanceX(self, that):
  13.         return that.x - self.x
  14.  
  15.     def distanceY(self, that):
  16.         return that.y - self.y
  17.  
  18.     def distanceTo(self, that):
  19.         w = self.distanceX(that)
  20.         h = self.distanceY(that)
  21.         return math.sqrt(w ** 2 + h ** 2)
  22.  
  23.     def lineTo(self, point):
  24.         return Line(self, point)
  25.  
  26.  
  27. class Line:
  28.  
  29.     def __init__(self, p1, p2):
  30.         self.p1 = p1
  31.         self.p2 = p2
  32.  
  33.     def __repr__(self):
  34.         return "Line(" + str(self.p1) + ", " + str(self.p2) + ")"
  35.  
  36.     def length(self):
  37.         w = self.p1.distanceX(self.p2)
  38.         h = self.p1.distanceY(self.p2)
  39.         return math.sqrt(w ** 2 + h ** 2)        
  40.  
  41.     def triangleTo(self, point):
  42.         return Triangle(self.p1, self.p2, point)
  43.  
  44.  
  45. class Triangle:
  46.  
  47.     def __init__(self, p1, p2, p3):
  48.         self.p1 = p1
  49.         self.p2 = p2
  50.         self.p3 = p3
  51.  
  52.     def __repr__(self):
  53.         return "Triangle(" + str(self.p1) + ", " + str(self.p2) + ", " + str(self.p3) + ")"    
  54.  
  55.     def perimeter(self):
  56.         l1 = self.p1.lineTo(self.p2).length()
  57.         l2 = self.p2.lineTo(self.p3).length()
  58.         l3 = self.p3.lineTo(self.p1).length()
  59.         return l1 + l2 + l3
  60.  
  61.     def square(self):
  62.         l1 = self.p1.lineTo(self.p2).length()
  63.         l2 = self.p2.lineTo(self.p3).length()
  64.         l3 = self.p3.lineTo(self.p1).length()
  65.         p = (l1 + l2 + l3) / 2
  66.         return math.sqrt(p * (p - l1) * (p - l2) * (p - l3))
  67.  
  68.     def isPointInside(self, point):
  69.         a = (self.p1.x - point.x) * (self.p2.y - self.p1.y) - (self.p2.x - self.p1.x) * (self.p1.y - point.y)
  70.         b = (self.p2.x - point.x) * (self.p3.y - self.p2.y) - (self.p3.x - self.p2.x) * (self.p2.y - point.y)
  71.         c = (self.p3.x - point.x) * (self.p1.y - self.p3.y) - (self.p1.x - self.p3.x) * (self.p3.y - point.y)
  72.         return (a >= 0 and b >= 0 and c >= 0) or (a <= 0 and b <= 0 and c <= 0)
  73.  
  74.  
  75. p = Point(10, 20)
  76.  
  77. print(p)
  78. print("No info")
  79. print("")
  80.  
  81. l = p.lineTo(Point(24, 44))
  82.  
  83. print(l)
  84. print("Length: " + str(l.length()))
  85. print("")
  86.  
  87. s = l.triangleTo(Point(33, 11))
  88.  
  89. print(s)
  90. print("Perimeter: " + str(s.perimeter()))
  91. print("Square: " + str(s.square()))
  92. print("")
  93.  
  94.  
  95. test1 = Point(15, 25)
  96. test2 = Point(0, 0)
  97.  
  98. print("Is " + str(test1) + " inside Triangle: " + str(s.isPointInside(test1)))
  99. print("Is " + str(test1) + " inside Triangle: " + str(s.isPointInside(test2)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement