From 221aa0a713d122658bf3af233002917472cb3c8f Mon Sep 17 00:00:00 2001 From: Ian Jauslin Date: Wed, 24 Nov 2021 17:51:57 -0500 Subject: [PATCH] Polyomino class --- polyomino.py | 58 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 12 deletions(-) diff --git a/polyomino.py b/polyomino.py index 6a52b0f..5196d72 100644 --- a/polyomino.py +++ b/polyomino.py @@ -5,21 +5,17 @@ from kivy.graphics import Color,Line,Rectangle from point import Point,l_infinity from tools import isint_nonzero,sgn,in_interval -class Square(): - # size - size=50 +# parent class of all polyominos +class Polyomino(): + def __init__(self,**kwargs): + # square elements that maje up the polyomino + self.squares=kwargs.get("squares",[]) - def __init__(self,x,y,**kwargs): - self.pos=Point(x,y) self.color=kwargs.get("color",(0,0,1)) self.selected=False - # set position - def setpos(self,x,y): - self.pos.x=x - self.pos.y=y - - def draw(self,**kwargs): + # draw function + def draw(self): # set color if not self.selected: Color(*self.color) @@ -28,7 +24,45 @@ class Square(): # darken selected Color(r/2,g/2,b/2) - Rectangle(pos=((self.pos.x-0.5)*self.size,(self.pos.y-0.5)*self.size),size=(self.size,self.size)) + for square in self.squares: + Rectangle(pos=((square.pos.x-0.5)*square.size,(square.pos.y-0.5)*square.size),size=(square.size,square.size)) + + # draw boundary + self.stroke() + + # draw boundary (override for connected polyominos) + def stroke(self): + # white + Color(1,1,1) + for square in self.squares: + Line(points=( + *((square.pos.x-0.5)*square.size,(square.pos.y-0.5)*square.size), + *((square.pos.x-0.5)*square.size,(square.pos.y+0.5)*square.size), + *((square.pos.x+0.5)*square.size,(square.pos.y+0.5)*square.size), + *((square.pos.x+0.5)*square.size,(square.pos.y-0.5)*square.size) + )) + + +# square +class Square(Polyomino): + def __init__(self,x,y,**kwargs): + super(Square,self).__init__(squares=[Square_element(x,y)],kwargs) + + + +# square building block of polyominos +class Square_element(): + # size + size=50 + + def __init__(self,x,y,**kwargs): + self.pos=Point(x,y) + + # set position + def setpos(self,x,y): + self.pos.x=x + self.pos.y=y + # check whether a square at pos interacts with square def check_interaction(self,pos):