diff --git a/src/element.py b/src/element.py index 2c3bbd2..2e81f74 100644 --- a/src/element.py +++ b/src/element.py @@ -4,6 +4,7 @@ import sys from point import Point,l_infinity,l_2 from tools import isint_nonzero,sgn,in_interval +from kivy.graphics import Rectangle # parent class of all elements class Element(): @@ -17,6 +18,16 @@ class Element(): self.pos.y=y + # override in each subclass + # draw element + def draw(self,painter): + return + + # override in each subclass + # draw boundary + def stroke(self,painter): + return + # override in each subclass # check whether an element interacts with square def check_interaction(self,element): @@ -46,6 +57,23 @@ class Element(): # square elements class Element_square(Element): + # draw element + def draw(self,painter): + Rectangle(pos=(painter.pos_tocoord_x(self.pos.x-0.5*self.size),painter.pos_tocoord_y(self.pos.y-0.5*self.size)),size=(self.size*painter.base_size,self.size*painter.base_size)) + + # draw boundary + def stroke(self,painter): + # convert to graphical coordinates + coordx=painter.pos_tocoord_x(square.pos.x) + coordy=painter.pos_tocoord_y(square.pos.y) + + Line(points=( + *(coordx-0.5*self.size*painter.base_size,coordy-0.5*self.size*painter.base_size), + *(coordx-0.5*self.size*painter.base_size,coordy+0.5*self.size*painter.base_size), + *(coordx+0.5*self.size*painter.base_size,coordy+0.5*self.size*painter.base_size), + *(coordx+0.5*self.size*painter.base_size,coordy-0.5*self.size*painter.base_size), + *(coordx-0.5*self.size*painter.base_size,coordy-0.5*self.size*painter.base_size) + )) # check whether an element interacts with square # TODO: this only works if element is a square! diff --git a/src/polyomino.py b/src/polyomino.py index 2d5c550..7487651 100644 --- a/src/polyomino.py +++ b/src/polyomino.py @@ -1,5 +1,5 @@ # a polyomino is a collection of elements, defined in elements.py -from kivy.graphics import Color,Line,Rectangle +from kivy.graphics import Color,Line from point import l_infinity from element import Element_square @@ -28,27 +28,17 @@ class Polyomino(): Color(r/2,g/2,b/2,alpha) for element in self.elements: - Rectangle(pos=(painter.pos_tocoord_x(element.pos.x-0.5*element.size),painter.pos_tocoord_y(element.pos.y-0.5*element.size)),size=(element.size*painter.base_size,element.size*painter.base_size)) + element.draw(painter) # draw boundary self.stroke(painter) # draw boundary (override for connected polyominos) def stroke(self,painter): - # convert to graphical coordinates - coordx=painter.pos_tocoord_x(square.pos.x) - coordy=painter.pos_tocoord_y(square.pos.y) - # white Color(1,1,1) for element in self.elements: - Line(points=( - *(coordx-0.5*element.size*painter.base_size,coordy-0.5*element.size*painter.base_size), - *(coordx-0.5*element.size*painter.base_size,coordy+0.5*element.size*painter.base_size), - *(coordx+0.5*element.size*painter.base_size,coordy+0.5*element.size*painter.base_size), - *(coordx+0.5*element.size*painter.base_size,coordy-0.5*element.size*painter.base_size), - *(coordx-0.5*element.size*painter.base_size,coordy-0.5*element.size*painter.base_size) - )) + element.stroke(painter) # move by delta def move(self,delta):