Redefine positions as Point objects
This commit is contained in:
parent
bbdbf3bef9
commit
b8a3e44f7c
68
main.py
68
main.py
@ -42,7 +42,7 @@ class Cross_painter(Widget):
|
|||||||
# create new cross
|
# create new cross
|
||||||
if touch.button=="right":
|
if touch.button=="right":
|
||||||
if self.check_add((touch.x,touch.y)):
|
if self.check_add((touch.x,touch.y)):
|
||||||
new=Cross((touch.x,touch.y))
|
new=Cross(touch.x,touch.y)
|
||||||
with self.canvas:
|
with self.canvas:
|
||||||
new.draw()
|
new.draw()
|
||||||
# add to list
|
# add to list
|
||||||
@ -66,7 +66,7 @@ class Cross_painter(Widget):
|
|||||||
# only move on left click
|
# only move on left click
|
||||||
if touch.button=="left" and self.selected!=None:
|
if touch.button=="left" and self.selected!=None:
|
||||||
#self.selected.pos=self.check_move((touch.x,touch.y),self.selected)
|
#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
|
# redraw
|
||||||
self.canvas.clear()
|
self.canvas.clear()
|
||||||
self.draw()
|
self.draw()
|
||||||
@ -95,17 +95,13 @@ class Cross_painter(Widget):
|
|||||||
return False
|
return False
|
||||||
return True
|
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
|
# cross
|
||||||
class Cross():
|
class Cross():
|
||||||
# size of central square
|
# size of central square
|
||||||
size=50
|
size=50
|
||||||
|
|
||||||
def __init__(self,pos,**kwargs):
|
def __init__(self,x,y,**kwargs):
|
||||||
self.pos=pos
|
self.pos=Point(x,y)
|
||||||
self.color=kwargs.get("color",(0,0,1))
|
self.color=kwargs.get("color",(0,0,1))
|
||||||
self.selected=False
|
self.selected=False
|
||||||
|
|
||||||
@ -116,33 +112,55 @@ class Cross():
|
|||||||
#else:
|
#else:
|
||||||
# Color(1,0,0)
|
# Color(1,0,0)
|
||||||
Color(*(self.color))
|
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.x-self.size*1.5,self.pos.y-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*0.5,self.pos.y-self.size*1.5),size=(self.size,3*self.size))
|
||||||
|
|
||||||
# stroke
|
# stroke
|
||||||
Color(1,1,1)
|
Color(1,1,1)
|
||||||
Line(points=(
|
Line(points=(
|
||||||
*(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[0]-self.size*0.5,self.pos[1]-self.size*1.5),
|
*(self.pos.x-self.size*0.5,self.pos.y-self.size*1.5),
|
||||||
*(self.pos[0]+self.size*0.5,self.pos[1]-self.size*1.5),
|
*(self.pos.x+self.size*0.5,self.pos.y-self.size*1.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[0]+self.size*1.5,self.pos[1]-self.size*0.5),
|
*(self.pos.x+self.size*1.5,self.pos.y-self.size*0.5),
|
||||||
*(self.pos[0]+self.size*1.5,self.pos[1]+self.size*0.5),
|
*(self.pos.x+self.size*1.5,self.pos.y+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[0]+self.size*0.5,self.pos[1]+self.size*1.5),
|
*(self.pos.x+self.size*0.5,self.pos.y+self.size*1.5),
|
||||||
*(self.pos[0]-self.size*0.5,self.pos[1]+self.size*1.5),
|
*(self.pos.x-self.size*0.5,self.pos.y+self.size*1.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[0]-self.size*1.5,self.pos[1]+self.size*0.5),
|
*(self.pos.x-self.size*1.5,self.pos.y+self.size*0.5),
|
||||||
*(self.pos[0]-self.size*1.5,self.pos[1]-self.size*0.5),
|
*(self.pos.x-self.size*1.5,self.pos.y-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),
|
||||||
))
|
))
|
||||||
|
|
||||||
|
# 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
|
# L_infinity distance rescalled by 3 in the x direction
|
||||||
def cross_distx(x,y):
|
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
|
# L_infinity distance rescalled by 3 in the y direction
|
||||||
def cross_disty(x,y):
|
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
|
# polar description of touching cross
|
||||||
def cross_polar(t):
|
def cross_polar(t):
|
||||||
|
Loading…
Reference in New Issue
Block a user