Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. import math
  2.  
  3. class Point(object):
  4. def __init__(self, *args, **kargs):
  5. self.num_dimensions = kargs.get("num_dimensions", len(args))
  6. self.coords = [0 for i in range(self.num_dimensions)]
  7. for i in range(len(args)):
  8. self.coords[i] = args[i]
  9.  
  10. """Gives the distance from this point to the origin."""
  11. def magnitude(self):
  12. return math.sqrt(sum(c*c for c in self.coords))
  13.  
  14. """
  15. Gives the angle of this point above the x axis.
  16. Measured in radians.
  17. Ranges between -pi and pi.
  18. """
  19. def angle(self):
  20. assert self.num_dimensions == 2
  21. assert self.x != 0 or self.y != 0
  22. return math.atan2(self.y,self.x)
  23. def tuple(self):
  24. return tuple(self.coords)
  25. def map(self, func):
  26. new_coords = [func(a) for a in self.coords]
  27. return Point(*new_coords)
  28. def _applyVectorFunc(self, other, f):
  29. assert self.num_dimensions == other.num_dimensions
  30. new_coords = [f(a,b) for a,b in zip(self.coords, other.coords)]
  31. return Point(*new_coords)
  32. def _applyScalarFunc(self, val, f):
  33. return self.map(lambda a: f(a,val))
  34. """
  35. new_coords = [f(a, val) for a in self.coords]
  36. return Point(*new_coords)
  37. """
  38.  
  39. def normalized(self):
  40. return self.__div__(self.magnitude())
  41.  
  42. def __add__(self, other):
  43. return self._applyVectorFunc(other, lambda a,b: a+b)
  44. def __sub__(self, other):
  45. return self._applyVectorFunc(other, lambda a,b: a-b)
  46. def __mul__(self, a):
  47. return self._applyScalarFunc(a, lambda b,c: b*c)
  48. def __rmul__(self, a):
  49. return self.__mul__(a)
  50. def __truediv__(self, a):
  51. return self._applyScalarFunc(a, lambda b,c: b/c)
  52.  
  53. def __neg__(self):
  54. return self * -1
  55. def __pos__(self):
  56. return self
  57.  
  58. def __eq__(self, other):
  59. try:
  60. return self.num_dimensions == other.num_dimensions and self.coords == other.coords
  61. except:
  62. return False
  63.  
  64. def __ne__(self, other):
  65. return not self.__eq__(other)
  66.  
  67. #simple comparator for sorting purposes
  68. def __lt__(self, other):
  69. if not isinstance(other, Point): raise Exception("can only compare points to points")
  70. return self.coords < other.coords
  71.  
  72. def __getattr__(self, name):
  73. if name == "x": return self.coords[0]
  74. if name == "y": return self.coords[1]
  75. if name == "z": return self.coords[2]
  76. raise AttributeError(name)
  77. def __setattr__(self, name, value):
  78. if name == "x": self.coords[0] = value
  79. elif name == "y": self.coords[1] = value
  80. elif name == "z": self.coords[2] = value
  81. else: object.__setattr__(self, name, value)
  82. def copy(self):
  83. return Point(*self.coords[:])
  84. def __hash__(self):
  85. return hash(self.tuple())
  86. def __repr__(self):
  87. use_nice_floats = False
  88. if use_nice_floats:
  89. return "Point(" + ", ".join("%.1f" % c for c in self.coords) + ")"
  90. else:
  91. return "Point(" + ", ".join(str(c) for c in self.coords) + ")"
  92.  
  93. #old version that is always three dimensions
  94. """
  95. class Point:
  96. def __init__(self, x=0, y=0, z=0):
  97. self.x = x
  98. self.y = y
  99. self.z = z
  100.  
  101. def magnitude(self):
  102. return math.sqrt(self.x*self.x + self.y*self.y + self.z*self.z)
  103.  
  104. def tuple(self):
  105. return (self.x, self.y, self.z)
  106.  
  107. def _applyVectorFunc(self, other, f):
  108. return Point(f(self.x, other.x), f(self.y, other.y), f(self.z, other.z))
  109. def _applyScalarFunc(self, a, f):
  110. return self._applyVectorFunc(Point(a,a,a), f)
  111.  
  112. def __add__(self, other):
  113. return self._applyVectorFunc(other, lambda a,b: a+b)
  114. def __sub__(self, other):
  115. return self._applyVectorFunc(other, lambda a,b: a-b)
  116. def __mul__(self, a):
  117. return self._applyScalarFunc(a, lambda b,c: b*c)
  118. def __div__(self, a):
  119. return self._applyScalarFunc(a, lambda b,c: b/c)
  120.  
  121. def __eq__(self, other):
  122. try:
  123. return self.x == other.x and self.y == other.y and self.z == other.z
  124. except:
  125. return False
  126.  
  127. def copy(self):
  128. return Point(self.x, self.y, self.z)
  129.  
  130. def __hash__(self):
  131. return hash(self.tuple())
  132.  
  133. def __repr__(self):
  134. #return "Point({}, {}, {})".format(self.x, self.y, self.z)
  135. return "Point({}, {})".format(self.x, self.y)
  136. """
  137.  
  138. def distance(a,b):
  139. return (a-b).magnitude()
  140.  
  141. def dot_product(a,b):
  142. return sum(a._applyVectorFunc(b, lambda x,y: x*y).coords)
  143.  
  144. def cross_product(u,v):
  145. #todo: support more dimensions than three, if it is possible to do so
  146. x = u.y*v.z - u.z*v.y
  147. y = u.z*v.x - u.x*v.z
  148. z = u.x*v.y - u.y*v.x
  149. return Point(x,y,z)
  150.  
  151. def midpoint(a,b, frac=0.5):
  152. return a*(1-frac) + b*frac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement