From b8a3e44f7c974c70b38b600d6430e01c6c95dcac Mon Sep 17 00:00:00 2001 From: Ian Jauslin Date: Tue, 28 Sep 2021 10:40:49 -0400 Subject: [PATCH] Redefine positions as Point objects --- main.py | 68 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 25 deletions(-) diff --git a/main.py b/main.py index e53204d..ec86a30 100644 --- a/main.py +++ b/main.py @@ -42,7 +42,7 @@ class Cross_painter(Widget): # create new cross if touch.button=="right": if self.check_add((touch.x,touch.y)): - new=Cross((touch.x,touch.y)) + new=Cross(touch.x,touch.y) with self.canvas: new.draw() # add to list @@ -66,7 +66,7 @@ class Cross_painter(Widget): # only move on left click if touch.button=="left" and self.selected!=None: #self.selected.pos=self.check_move((touch.x,touch.y),self.selected) - self.selected.pos=(touch.x,touch.y) + self.selected.setpos(touch.x,touch.y) # redraw self.canvas.clear() self.draw() @@ -95,17 +95,13 @@ class Cross_painter(Widget): return False return True - # check whether a cross at pos interacts with cross - def check_interaction(self,pos,cross): - return int((pos[0]-cross.pos[0])/cross.size)**2+int((pos[1]-cross.pos[1])/cross.size)**2>=5 - # cross class Cross(): # size of central square size=50 - def __init__(self,pos,**kwargs): - self.pos=pos + def __init__(self,x,y,**kwargs): + self.pos=Point(x,y) self.color=kwargs.get("color",(0,0,1)) self.selected=False @@ -116,33 +112,55 @@ class Cross(): #else: # Color(1,0,0) Color(*(self.color)) - Rectangle(pos=(self.pos[0]-self.size*1.5,self.pos[1]-self.size*0.5),size=(3*self.size,self.size)) - Rectangle(pos=(self.pos[0]-self.size*0.5,self.pos[1]-self.size*1.5),size=(self.size,3*self.size)) + Rectangle(pos=(self.pos.x-self.size*1.5,self.pos.y-self.size*0.5),size=(3*self.size,self.size)) + Rectangle(pos=(self.pos.x-self.size*0.5,self.pos.y-self.size*1.5),size=(self.size,3*self.size)) # stroke Color(1,1,1) Line(points=( - *(self.pos[0]-self.size*0.5,self.pos[1]-self.size*0.5), - *(self.pos[0]-self.size*0.5,self.pos[1]-self.size*1.5), - *(self.pos[0]+self.size*0.5,self.pos[1]-self.size*1.5), - *(self.pos[0]+self.size*0.5,self.pos[1]-self.size*0.5), - *(self.pos[0]+self.size*1.5,self.pos[1]-self.size*0.5), - *(self.pos[0]+self.size*1.5,self.pos[1]+self.size*0.5), - *(self.pos[0]+self.size*0.5,self.pos[1]+self.size*0.5), - *(self.pos[0]+self.size*0.5,self.pos[1]+self.size*1.5), - *(self.pos[0]-self.size*0.5,self.pos[1]+self.size*1.5), - *(self.pos[0]-self.size*0.5,self.pos[1]+self.size*0.5), - *(self.pos[0]-self.size*1.5,self.pos[1]+self.size*0.5), - *(self.pos[0]-self.size*1.5,self.pos[1]-self.size*0.5), - *(self.pos[0]-self.size*0.5,self.pos[1]-self.size*0.5), + *(self.pos.x-self.size*0.5,self.pos.y-self.size*0.5), + *(self.pos.x-self.size*0.5,self.pos.y-self.size*1.5), + *(self.pos.x+self.size*0.5,self.pos.y-self.size*1.5), + *(self.pos.x+self.size*0.5,self.pos.y-self.size*0.5), + *(self.pos.x+self.size*1.5,self.pos.y-self.size*0.5), + *(self.pos.x+self.size*1.5,self.pos.y+self.size*0.5), + *(self.pos.x+self.size*0.5,self.pos.y+self.size*0.5), + *(self.pos.x+self.size*0.5,self.pos.y+self.size*1.5), + *(self.pos.x-self.size*0.5,self.pos.y+self.size*1.5), + *(self.pos.x-self.size*0.5,self.pos.y+self.size*0.5), + *(self.pos.x-self.size*1.5,self.pos.y+self.size*0.5), + *(self.pos.x-self.size*1.5,self.pos.y-self.size*0.5), + *(self.pos.x-self.size*0.5,self.pos.y-self.size*0.5), )) + # check whether a cross at pos interacts with cross + def check_interaction(self,pos): + return int((pos.x-self.pos.x)/self.size)**2+int((pos.y-self.pos.y)/self.size)**2>=5 + + # find position along a line that comes in contact with the line + #def move_on_line_to_stick(self,x,v): + +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 __neg__(self): + return Point(-self.x,self.y) + + def setpos(self,x,y): + self.x=x + self.y=y + # L_infinity distance rescalled by 3 in the x direction def cross_distx(x,y): - return max(abs(x[0]-y[0])/3,abs(x[1]-y[1])) + return max(abs(x.x-y.x)/3,abs(x.y-y.y)) # L_infinity distance rescalled by 3 in the y direction def cross_disty(x,y): - return max(abs(x[0]-y[0]),abs(x[1]-y[1])/3) + return max(abs(x.x-y.x),abs(x.y-y.y)/3) # polar description of touching cross def cross_polar(t):