Guest User

Untitled

a guest
Jan 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. import math
  2.  
  3.  
  4. def deg2rad(angle: float) -> float:
  5. return (angle % 360) / 180 * math.pi
  6.  
  7.  
  8. def rad2deg(angle: float) -> float:
  9. return (angle * 180 / math.pi) % 360
  10.  
  11.  
  12. class Point:
  13. def __init__(self, x: float, y: float):
  14. self.x = x
  15. self.y = y
  16.  
  17. def __repr__(self) -> str:
  18. return f"Point(x={self.x}, y={self.y})"
  19.  
  20. def __iter__(self):
  21. yield self.x
  22. yield self.y
  23.  
  24. def __add__(self, other):
  25. assert isinstance(other, Point)
  26. return Point(self.x + other.x, self.y + other.y)
  27.  
  28. def __sub__(self, other):
  29. assert isinstance(other, Point)
  30. return Point(self.x - other.x, self.y - other.y)
  31.  
  32. def __mul__(self, other):
  33. assert isinstance(other, int) or isinstance(other, float)
  34. return Point(self.x * other, self.y * other)
  35.  
  36. def __truediv__(self, other):
  37. assert isinstance(other, int) or isinstance(other, float)
  38. return Point(self.x / other, self.y / other)
  39.  
  40. def to_vector(self):
  41. x, y = self.x, self.y
  42. if y == 0:
  43. return Vector(x, 90)
  44. distance = math.sqrt(x ** 2 + y ** 2)
  45. angle = rad2deg(math.atan(-x / y))
  46. if y > 0:
  47. distance *= -1
  48. return Vector(distance, angle)
  49.  
  50.  
  51. class Vector:
  52. def __init__(self, distance: float, angle: float):
  53. if distance < 0:
  54. distance *= -1
  55. angle += 180
  56. self.distance = distance
  57. self.angle = angle % 360
  58.  
  59. def __repr__(self) -> str:
  60. return f"Vector(distance={self.distance}, angle={self.angle})"
  61.  
  62. def to_point(self):
  63. angle = self.angle % 360
  64. distance = self.distance
  65. if angle % 90 == 0:
  66. return [
  67. Point(0, -distance),
  68. Point(distance, 0),
  69. Point(0, distance),
  70. Point(-distance, 0)
  71. ][int(angle / 90)]
  72.  
  73. if angle > 180:
  74. angle -= 180
  75. distance *= -1
  76.  
  77. w = -1 / math.tan(deg2rad(angle))
  78. x = distance / math.sqrt(w ** 2 + 1)
  79. return Point(x, w * x)
Add Comment
Please, Sign In to add comment