drawing and stroking elements done in elements.py

This commit is contained in:
Ian Jauslin 2022-09-27 18:50:21 -04:00
parent 383cb5c462
commit c13b0f4556
2 changed files with 31 additions and 13 deletions

View File

@ -4,6 +4,7 @@ import sys
from point import Point,l_infinity,l_2 from point import Point,l_infinity,l_2
from tools import isint_nonzero,sgn,in_interval from tools import isint_nonzero,sgn,in_interval
from kivy.graphics import Rectangle
# parent class of all elements # parent class of all elements
class Element(): class Element():
@ -17,6 +18,16 @@ class Element():
self.pos.y=y 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 # override in each subclass
# check whether an element interacts with square # check whether an element interacts with square
def check_interaction(self,element): def check_interaction(self,element):
@ -46,6 +57,23 @@ class Element():
# square elements # square elements
class Element_square(Element): 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 # check whether an element interacts with square
# TODO: this only works if element is a square! # TODO: this only works if element is a square!

View File

@ -1,5 +1,5 @@
# a polyomino is a collection of elements, defined in elements.py # 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 point import l_infinity
from element import Element_square from element import Element_square
@ -28,27 +28,17 @@ class Polyomino():
Color(r/2,g/2,b/2,alpha) Color(r/2,g/2,b/2,alpha)
for element in self.elements: 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 # draw boundary
self.stroke(painter) self.stroke(painter)
# draw boundary (override for connected polyominos) # draw boundary (override for connected polyominos)
def stroke(self,painter): 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 # white
Color(1,1,1) Color(1,1,1)
for element in self.elements: for element in self.elements:
Line(points=( element.stroke(painter)
*(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)
))
# move by delta # move by delta
def move(self,delta): def move(self,delta):