From 5835c9003ce75c8c88d6d981686e1f9ba653e9cb Mon Sep 17 00:00:00 2001 From: Ian Jauslin Date: Fri, 23 Sep 2022 20:49:58 -0400 Subject: [PATCH] Implement interaction between squares of different sizes --- src/painter.py | 21 ++++++++--- src/polyomino.py | 97 ++++++++++++++++++++++++++---------------------- 2 files changed, 68 insertions(+), 50 deletions(-) diff --git a/src/painter.py b/src/painter.py index f91558e..7bf3483 100644 --- a/src/painter.py +++ b/src/painter.py @@ -348,8 +348,14 @@ class Painter(Widget): def check_interaction_unselected_element(self,element,offset): for particle in self.unselected: for square in particle.squares: - if square.check_interaction(element.pos+offset): + # add offset + element.pos+=offset + if square.check_interaction(element): + # reset offset + element.pos-=offset return True + # reset offset + element.pos-=offset return False @@ -387,16 +393,21 @@ class Painter(Widget): for other in self.unselected: for obstacle in other.squares: # move would make element overlap with obstacle - if obstacle.check_interaction(element.pos+delta): + element.pos+=delta + if obstacle.check_interaction(element): + element.pos-=delta accept_newpos=False # check if particle already touches obstacle - if obstacle.check_touch(element.pos): + if obstacle.check_touch(element): # move along obstacle while remaining stuck - newdelta=obstacle.move_along(delta,element.pos) + newdelta=obstacle.move_along(delta,element) else: - newdelta=obstacle.move_on_line_to_stick(element.pos,delta) + newdelta=obstacle.move_on_line_to_stick(element,delta) if not self.check_interaction_unselected_element(element,newdelta): return newdelta + else: + # reset offset + element.pos-=delta if accept_newpos: return delta else: diff --git a/src/polyomino.py b/src/polyomino.py index ed82f0d..71ece63 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*painter.base_size,square.size*painter.base_size)) + Rectangle(pos=(painter.pos_tocoord_x(square.pos.x-0.5*square.size),painter.pos_tocoord_y(square.pos.y-0.5*square.size)),size=(square.size*painter.base_size,square.size*painter.base_size)) # draw boundary self.stroke(painter) @@ -67,8 +67,14 @@ class Polyomino(): def check_interaction(self,candidate,offset): for square1 in self.squares: for square2 in candidate.squares: - if square1.check_interaction(square2.pos+offset): + # add offset + square2.pos+=offset + if square1.check_interaction(square2): + # reset offset + square2.pos-=offset return True + # reset offset + square2.pos-=offset return False # square @@ -125,38 +131,39 @@ class Square_element(): self.pos.y=y - # check whether a square at pos interacts with square - def check_interaction(self,pos): - return l_infinity(pos-self.pos)0: - if relmath.ceil(rel/self.size)*self.size+1e-11 and math.ceil(rel/self.size)*self.size!=0: + if relmath.ceil(rel/((self.size+element.size)/2))*((self.size+element.size)/2)+1e-11 and math.ceil(rel/((self.size+element.size)/2))*((self.size+element.size)/2)!=0: # stick to corner - candidate.y=math.ceil(rel/self.size)*self.size+self.pos.y-pos.y + candidate.y=math.ceil(rel/((self.size+element.size)/2))*((self.size+element.size)/2)+self.pos.y-element.pos.y else: - if rel>math.floor(rel/self.size)*self.size+1e-11 and delta.y+relmath.floor(rel/((self.size+element.size)/2))*((self.size+element.size)/2)+1e-11 and delta.y+rel0: - if relmath.ceil(rel/self.size)*self.size+1e-11 and math.ceil(rel/self.size)*self.size!=0: + if relmath.ceil(rel/((self.size+element.size)/2))*((self.size+element.size)/2)+1e-11 and math.ceil(rel/((self.size+element.size)/2))*((self.size+element.size)/2)!=0: # stick to corner - candidate.x=math.ceil(rel/self.size)*self.size+self.pos.x-pos.x + candidate.x=math.ceil(rel/((self.size+element.size)/2))*((self.size+element.size)/2)+self.pos.x-element.pos.x else: - if rel>math.floor(rel/self.size)*self.size+1e-11 and delta.x+relmath.floor(rel/((self.size+element.size)/2))*((self.size+element.size)/2)+1e-11 and delta.x+rel