Jam/src/polyomino.py

118 lines
4.4 KiB
Python
Raw Normal View History

2022-09-24 01:17:53 +00:00
# a polyomino is a collection of elements, defined in elements.py
2021-11-24 22:18:27 +00:00
from kivy.graphics import Color,Line,Rectangle
2022-09-24 01:17:53 +00:00
from point import l_infinity
from element import Element_square
2021-11-24 22:18:27 +00:00
2021-11-24 22:51:57 +00:00
# parent class of all polyominos
class Polyomino():
def __init__(self,**kwargs):
2022-09-24 01:17:53 +00:00
# elements that make up the polyomino
self.elements=kwargs.get("elements",[])
2021-11-24 22:18:27 +00:00
self.color=kwargs.get("color",(0,0,1))
self.selected=False
2021-12-01 23:49:35 +00:00
# mesh of background grid (no grid for mesh size 0)
self.grid=kwargs.get("grid",0)
2021-12-01 20:37:07 +00:00
2021-11-24 22:51:57 +00:00
# draw function
def draw(self,painter,**kwargs):
2021-12-10 15:55:04 +00:00
alpha=kwargs.get("alpha",1)
2021-11-24 22:18:27 +00:00
# set color
if not self.selected:
2021-12-10 15:55:04 +00:00
Color(*self.color,alpha)
2021-11-24 22:18:27 +00:00
else:
(r,g,b)=self.color
# darken selected
2021-12-10 15:55:04 +00:00
Color(r/2,g/2,b/2,alpha)
2021-11-24 22:18:27 +00:00
2022-09-24 01:17:53 +00:00
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))
2021-11-24 22:51:57 +00:00
# draw boundary
self.stroke(painter)
2021-11-24 22:51:57 +00:00
# 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)
2021-11-24 22:51:57 +00:00
# white
Color(1,1,1)
2022-09-24 01:17:53 +00:00
for element in self.elements:
2021-11-24 22:51:57 +00:00
Line(points=(
2022-09-24 01:17:53 +00:00
*(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)
2021-11-24 22:51:57 +00:00
))
2021-11-25 00:22:05 +00:00
2021-11-25 01:30:51 +00:00
# move by delta
def move(self,delta):
2022-09-24 01:17:53 +00:00
for element in self.elements:
element.pos+=delta
2021-11-25 01:30:51 +00:00
2021-11-25 00:22:05 +00:00
# whether x is in the support of the polyomino
def in_support(self,x):
2022-09-24 01:17:53 +00:00
for element in self.elements:
if element.in_support(x):
2021-11-25 00:22:05 +00:00
return True
return False
# check whether self interacts with candidate if candidate were moved by offset
def check_interaction(self,candidate,offset):
2022-09-24 01:17:53 +00:00
for element1 in self.elements:
for element2 in candidate.elements:
# add offset
2022-09-24 01:17:53 +00:00
element2.pos+=offset
if element1.check_interaction(element2):
# reset offset
2022-09-24 01:17:53 +00:00
element2.pos-=offset
2021-11-25 00:22:05 +00:00
return True
# reset offset
2022-09-24 01:17:53 +00:00
element2.pos-=offset
2021-11-25 00:22:05 +00:00
return False
2021-11-24 22:51:57 +00:00
# square
class Square(Polyomino):
def __init__(self,x,y,**kwargs):
2022-09-24 01:17:53 +00:00
super(Square,self).__init__(**kwargs,elements=[Element_square(x,y,size=kwargs.get("size",1.0))])
2021-11-24 22:51:57 +00:00
2021-11-25 03:46:47 +00:00
# cross
class Cross(Polyomino):
def __init__(self,x,y,**kwargs):
2022-09-24 01:17:53 +00:00
super(Cross,self).__init__(**kwargs,elements=[\
Element_square(x,y,1),\
Element_square(x+1,y,1),\
Element_square(x-1,y,1),\
Element_square(x,y+1,1),\
Element_square(x,y-1,1)\
2021-11-25 03:46:47 +00:00
])
2022-09-24 01:17:53 +00:00
# redefine stroke to avoid lines between touching elements
def stroke(self,painter):
# convert to graphical coordinates
2022-09-24 01:17:53 +00:00
coordx=painter.pos_tocoord_x(self.elements[0].pos.x)
coordy=painter.pos_tocoord_y(self.elements[0].pos.y)
2021-11-25 03:46:47 +00:00
Color(1,1,1)
Line(points=(
*(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),
2021-11-25 03:46:47 +00:00
))