2021-09-28 19:23:06 +00:00
|
|
|
import math
|
|
|
|
|
|
|
|
# point in two dimensions
|
|
|
|
class Point:
|
|
|
|
def __init__(self,x,y):
|
|
|
|
self.x=x
|
|
|
|
self.y=y
|
|
|
|
|
|
|
|
def __add__(self,point):
|
|
|
|
return Point(self.x+point.x,self.y+point.y)
|
|
|
|
|
|
|
|
def __sub__(self,point):
|
|
|
|
return Point(self.x-point.x,self.y-point.y)
|
|
|
|
|
|
|
|
def __neg__(self):
|
|
|
|
return Point(-self.x,-self.y)
|
|
|
|
|
|
|
|
def __mul__(self,a):
|
|
|
|
return Point(a*self.x,a*self.y)
|
|
|
|
|
|
|
|
def __truediv__(self,a):
|
|
|
|
return Point(self.x/a,self.y/a)
|
|
|
|
|
|
|
|
def __pow__(self,a):
|
|
|
|
if a==2:
|
|
|
|
return self.dot(self)
|
|
|
|
|
|
|
|
# dot product
|
|
|
|
def dot(self,x):
|
|
|
|
return self.x*x.x+self.y*x.y
|
|
|
|
|
|
|
|
# integer part
|
|
|
|
def int(self):
|
|
|
|
return Point(int(self.x),int(self.y))
|
|
|
|
|
|
|
|
# find the closest among a list of points
|
|
|
|
def closest(self,points):
|
|
|
|
dist=math.inf
|
|
|
|
closest=None
|
|
|
|
for point in points:
|
|
|
|
if (self-point)**2<dist:
|
|
|
|
closest=point
|
|
|
|
dist=(self-point)**2
|
|
|
|
return closest
|
|
|
|
|
2021-11-24 22:18:27 +00:00
|
|
|
|
|
|
|
# L infinity norm
|
|
|
|
def l_infinity(x):
|
|
|
|
return max(abs(x.x),abs(x.y))
|