From b9be36b4e02365c9a97eb6c69da0d7b0c2c3cb52 Mon Sep 17 00:00:00 2001 From: Ian Jauslin Date: Fri, 23 Sep 2022 19:33:05 -0400 Subject: [PATCH] move Square_element.size to painter, and allow for squares to determine their own size --- src/painter.py | 17 +++++---- src/polyomino.py | 97 ++++++++++++++++++++++++------------------------ 2 files changed, 58 insertions(+), 56 deletions(-) diff --git a/src/painter.py b/src/painter.py index f11cfe8..f91558e 100644 --- a/src/painter.py +++ b/src/painter.py @@ -43,6 +43,9 @@ class Painter(Widget): # modifiers self.modifiers=[] + # base size for all particles + self.base_size=50 + # init Widget super(Painter,self).__init__(**kwargs) @@ -75,13 +78,13 @@ class Painter(Widget): # convert logical coordinates (normalized and centered) to the ones that are plotted def pos_tocoord_x(self,x): - return self.width/2+x*Square_element.size + return self.width/2+x*self.base_size def pos_tocoord_y(self,y): - return self.height/2+y*Square_element.size + return self.height/2+y*self.base_size def coord_topos_x(self,x): - return (x-self.width/2)/Square_element.size + return (x-self.width/2)/self.base_size def coord_topos_y(self,y): - return (y-self.height/2)/Square_element.size + return (y-self.height/2)/self.base_size @@ -176,10 +179,10 @@ class Painter(Widget): # zoom elif text=="+": # increment by 10% - self.set_zoom(Square_element.size/50*1.1) + self.set_zoom(self.base_size/50*1.1) elif text=="-": # decrease by 10% - self.set_zoom(Square_element.size/50*0.9) + self.set_zoom(self.base_size/50*0.9) elif text=="=": # reset self.set_zoom(1) @@ -557,7 +560,7 @@ class Painter(Widget): # set zoom level def set_zoom(self,level): - Square_element.size=level*50 + self.base_size=level*50 self.draw() diff --git a/src/polyomino.py b/src/polyomino.py index 6a79106..ed82f0d 100644 --- a/src/polyomino.py +++ b/src/polyomino.py @@ -29,7 +29,7 @@ class Polyomino(): Color(r/2,g/2,b/2,alpha) for square in self.squares: - Rectangle(pos=(painter.pos_tocoord_x(square.pos.x-0.5),painter.pos_tocoord_y(square.pos.y-0.5)),size=(square.size,square.size)) + Rectangle(pos=(painter.pos_tocoord_x(square.pos.x-0.5),painter.pos_tocoord_y(square.pos.y-0.5)),size=(square.size*painter.base_size,square.size*painter.base_size)) # draw boundary self.stroke(painter) @@ -44,11 +44,11 @@ class Polyomino(): Color(1,1,1) for square in self.squares: Line(points=( - *(coordx-0.5*square.size,coordy-0.5*square.size), - *(coordx-0.5*square.size,coordy+0.5*square.size), - *(coordx+0.5*square.size,coordy+0.5*square.size), - *(coordx+0.5*square.size,coordy-0.5*square.size), - *(coordx-0.5*square.size,coordy-0.5*square.size) + *(coordx-0.5*square.size*painter.base_size,coordy-0.5*square.size*painter.base_size), + *(coordx-0.5*square.size*painter.base_size,coordy+0.5*square.size*painter.base_size), + *(coordx+0.5*square.size*painter.base_size,coordy+0.5*square.size*painter.base_size), + *(coordx+0.5*square.size*painter.base_size,coordy-0.5*square.size*painter.base_size), + *(coordx-0.5*square.size*painter.base_size,coordy-0.5*square.size*painter.base_size) )) # move by delta @@ -74,17 +74,17 @@ class Polyomino(): # square class Square(Polyomino): def __init__(self,x,y,**kwargs): - super(Square,self).__init__(**kwargs,squares=[Square_element(x,y)]) + super(Square,self).__init__(**kwargs,squares=[Square_element(x,y,size=kwargs.get("size",1.0))]) # cross class Cross(Polyomino): def __init__(self,x,y,**kwargs): super(Cross,self).__init__(**kwargs,squares=[\ - Square_element(x,y),\ - Square_element(x+1,y),\ - Square_element(x-1,y),\ - Square_element(x,y+1),\ - Square_element(x,y-1)\ + Square_element(x,y,1),\ + Square_element(x+1,y,1),\ + Square_element(x-1,y,1),\ + Square_element(x,y+1,1),\ + Square_element(x,y-1,1)\ ]) # redefine stroke to avoid lines between touching squares @@ -95,30 +95,29 @@ class Cross(Polyomino): Color(1,1,1) Line(points=( - *(coordx-0.5*Square_element.size,coordy-0.5*Square_element.size), - *(coordx-0.5*Square_element.size,coordy-1.5*Square_element.size), - *(coordx+0.5*Square_element.size,coordy-1.5*Square_element.size), - *(coordx+0.5*Square_element.size,coordy-0.5*Square_element.size), - *(coordx+1.5*Square_element.size,coordy-0.5*Square_element.size), - *(coordx+1.5*Square_element.size,coordy+0.5*Square_element.size), - *(coordx+0.5*Square_element.size,coordy+0.5*Square_element.size), - *(coordx+0.5*Square_element.size,coordy+1.5*Square_element.size), - *(coordx-0.5*Square_element.size,coordy+1.5*Square_element.size), - *(coordx-0.5*Square_element.size,coordy+0.5*Square_element.size), - *(coordx-1.5*Square_element.size,coordy+0.5*Square_element.size), - *(coordx-1.5*Square_element.size,coordy-0.5*Square_element.size), - *(coordx-0.5*Square_element.size,coordy-0.5*Square_element.size), + *(coordx-0.5*painter.base_size,coordy-0.5*painter.base_size), + *(coordx-0.5*painter.base_size,coordy-1.5*painter.base_size), + *(coordx+0.5*painter.base_size,coordy-1.5*painter.base_size), + *(coordx+0.5*painter.base_size,coordy-0.5*painter.base_size), + *(coordx+1.5*painter.base_size,coordy-0.5*painter.base_size), + *(coordx+1.5*painter.base_size,coordy+0.5*painter.base_size), + *(coordx+0.5*painter.base_size,coordy+0.5*painter.base_size), + *(coordx+0.5*painter.base_size,coordy+1.5*painter.base_size), + *(coordx-0.5*painter.base_size,coordy+1.5*painter.base_size), + *(coordx-0.5*painter.base_size,coordy+0.5*painter.base_size), + *(coordx-1.5*painter.base_size,coordy+0.5*painter.base_size), + *(coordx-1.5*painter.base_size,coordy-0.5*painter.base_size), + *(coordx-0.5*painter.base_size,coordy-0.5*painter.base_size), )) # square building block of polyominos class Square_element(): - # size - size=50 - def __init__(self,x,y,**kwargs): + def __init__(self,x,y,size,**kwargs): self.pos=Point(x,y) + self.size=size # set position def setpos(self,x,y): @@ -128,12 +127,12 @@ class Square_element(): # check whether a square at pos interacts with square def check_interaction(self,pos): - return l_infinity(pos-self.pos)<1 + return l_infinity(pos-self.pos)0: - if relmath.ceil(rel)+1e-11 and math.ceil(rel)!=0: + if relmath.ceil(rel/self.size)*self.size+1e-11 and math.ceil(rel/self.size)*self.size!=0: # stick to corner - candidate.y=math.ceil(rel)+self.pos.y-pos.y + candidate.y=math.ceil(rel/self.size)*self.size+self.pos.y-pos.y else: - if rel>math.floor(rel)+1e-11 and delta.y+relmath.floor(rel/self.size)*self.size+1e-11 and delta.y+rel0: - if relmath.ceil(rel)+1e-11 and math.ceil(rel)!=0: + if relmath.ceil(rel/self.size)*self.size+1e-11 and math.ceil(rel/self.size)*self.size!=0: # stick to corner - candidate.x=math.ceil(rel)+self.pos.x-pos.x + candidate.x=math.ceil(rel/self.size)*self.size+self.pos.x-pos.x else: - if rel>math.floor(rel)+1e-11 and delta.x+relmath.floor(rel/self.size)*self.size+1e-11 and delta.x+rel