Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- class Point:
- def __init__(self, x, y):
- self.x = x
- self.y = y
- def __repr__(self):
- return "Point(x: " + str(self.x) + ", y: " + str(self.y) + ")"
- def distanceX(self, that):
- return that.x - self.x
- def distanceY(self, that):
- return that.y - self.y
- def distanceTo(self, that):
- w = self.distanceX(that)
- h = self.distanceY(that)
- return math.sqrt(w ** 2 + h ** 2)
- def lineTo(self, point):
- return Line(self, point)
- class Line:
- def __init__(self, p1, p2):
- self.p1 = p1
- self.p2 = p2
- def __repr__(self):
- return "Line(" + str(self.p1) + ", " + str(self.p2) + ")"
- def length(self):
- w = self.p1.distanceX(self.p2)
- h = self.p1.distanceY(self.p2)
- return math.sqrt(w ** 2 + h ** 2)
- def triangleTo(self, point):
- return Triangle(self.p1, self.p2, point)
- class Triangle:
- def __init__(self, p1, p2, p3):
- self.p1 = p1
- self.p2 = p2
- self.p3 = p3
- def __repr__(self):
- return "Triangle(" + str(self.p1) + ", " + str(self.p2) + ", " + str(self.p3) + ")"
- def perimeter(self):
- l1 = self.p1.lineTo(self.p2).length()
- l2 = self.p2.lineTo(self.p3).length()
- l3 = self.p3.lineTo(self.p1).length()
- return l1 + l2 + l3
- def square(self):
- l1 = self.p1.lineTo(self.p2).length()
- l2 = self.p2.lineTo(self.p3).length()
- l3 = self.p3.lineTo(self.p1).length()
- p = (l1 + l2 + l3) / 2
- return math.sqrt(p * (p - l1) * (p - l2) * (p - l3))
- def isPointInside(self, point):
- a = (self.p1.x - point.x) * (self.p2.y - self.p1.y) - (self.p2.x - self.p1.x) * (self.p1.y - point.y)
- b = (self.p2.x - point.x) * (self.p3.y - self.p2.y) - (self.p3.x - self.p2.x) * (self.p2.y - point.y)
- c = (self.p3.x - point.x) * (self.p1.y - self.p3.y) - (self.p1.x - self.p3.x) * (self.p3.y - point.y)
- return (a >= 0 and b >= 0 and c >= 0) or (a <= 0 and b <= 0 and c <= 0)
- p = Point(10, 20)
- print(p)
- print("No info")
- print("")
- l = p.lineTo(Point(24, 44))
- print(l)
- print("Length: " + str(l.length()))
- print("")
- s = l.triangleTo(Point(33, 11))
- print(s)
- print("Perimeter: " + str(s.perimeter()))
- print("Square: " + str(s.square()))
- print("")
- test1 = Point(15, 25)
- test2 = Point(0, 0)
- print("Is " + str(test1) + " inside Triangle: " + str(s.isPointInside(test1)))
- print("Is " + str(test1) + " inside Triangle: " + str(s.isPointInside(test2)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement