Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. class Point:
  2. def __init__(self, x, y, index):
  3. self.x = x
  4. self.y = y
  5. self.index = index
  6.  
  7. def distance(self, point):
  8. return ((self.x - point.x)**2 + (self.y - point.y)**2)**0.5
  9.  
  10. def __repr__(self):
  11. return "Point P" + str(self.index) + "(" + str(self.x) + ", " + str(self.y) + ")"
  12.  
  13. def orientation(p1, p2, p3):
  14. return (p2.y - p1.y)*(p3.x - p2.x) - (p3.y - p2.y)*(p2.x - p1.x)
  15.  
  16. def check_convex_quadrilateral(points):
  17. negative_angle_found = False
  18. positive_angle_found = False
  19.  
  20. p1 = points[-2]
  21. p2 = points[-1]
  22. for i in range(len(points)):
  23. new_point = points[i]
  24. if orientation(p1, p2, new_point) > 0:
  25. positive_angle_found = True
  26. else:
  27. negative_angle_found = True
  28.  
  29. p1 = p2
  30. p2 = new_point
  31.  
  32. if positive_angle_found == negative_angle_found:
  33. return False
  34.  
  35. return True
  36.  
  37.  
  38.  
  39. def isOnCircle(point, r):
  40. # Compare radius of circle with distance of its center from given point
  41. if (point.x - r.x)**2 + (point.y - r.y)**2 == 0:
  42. print("On the circle")
  43. return True
  44. else:
  45. point("Isn't on circle")
  46. return False
  47.  
  48. def position(point, r):
  49. if point.x**2 + point.y**2 - r == 0:
  50. print("On circle")
  51. elif point.x**2 + point.y**2 - r > 0:
  52. print("Outside")
  53. else:
  54. print("Inside")
  55.  
  56.  
  57. #Check if a polygon is convex -- complexity: O(n)
  58. def main():
  59. points = []
  60. count = 0
  61. with open("points.txt") as fin:
  62. for line in fin:
  63. input = []
  64. input.append([int(x) for x in line.split()])
  65. points.append(Point(input[0][0], input[0][1], count))
  66. count = count + 1
  67. fin.close()
  68.  
  69. #Part I: Check if a quadrilateral is convex:
  70. if check_convex_quadrilateral(points) == True:
  71. print("The quadrilateral is convex")
  72. #Part II: Position of A4 for the triangle circumscribed about a circle
  73.  
  74. else:
  75. print("The quadrilateral is not convex")
  76.  
  77.  
  78.  
  79. if __name__ == '__main__':
  80. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement