Recently I made a point dataclass and a point_calc class which performs various commonly used algorithms on a point.
from dataclasses import dataclass
import math
EPS = 7./3 - 4./3 -1
#note 7./3 - 4./3 -1: https://stackoverflow.com/questions/19141432/python-numpy-machine-epsilon
@dataclass
class point:
x: float = 0.0
y: float = 0.0
#x first then y (2,3) > (1,4) and (2,4) > (2,3)
def __lt__(self, other) -> bool:
if (abs(self.x - other.x) > EPS):
return self.x < other.x
return self.y < other.y
def __eq__(self, other) -> bool:
return (abs(self.x - other.x) < EPS) and (abs(self.y - other.y) < EPS)
def __str__(self):
return "("+str(point.x)+" ,"+str(point.y)+")"
class point_calc:
def dist(self, point, other, t) -> float: # t=0=euclidean, t=1=manhattan
match t:
case 0:
return math.dist([point.x, point.y], [other.x, other.y])
case 1:
return float(abs(abs(point.x-other.x)+abs(point.y-other.y)))
case _:
raise Exception("No Such Function")
###### HELPER FUNCS ######
def dr(self, d) -> float:
return (d*math.pi)/180
def rd(self, r) -> float:
return (r*180)/math.pi
# assumes theta is in Degrees, if not, just use point_clac.rd() to convert to deg
def rotate(self, point, theta, accuracy = 2) -> tuple:
dr = self.dr
return (round(point.x*math.cos(dr(theta))-point.y*math.sin(dr(theta)), accuracy),
round(point.x*math.sin(dr(theta))+point.y*math.cos(dr(theta)), accuracy))
Is there any suggestions on how it can be improved? Any suggestions will be accepted and any help (or constructive criticism) will be appreciated.