2021-11-24 22:18:27 +00:00
|
|
|
import math
|
|
|
|
import sys
|
|
|
|
from kivy.graphics import Color,Line,Rectangle
|
|
|
|
|
|
|
|
from point import Point,l_infinity
|
|
|
|
from tools import isint_nonzero,sgn,in_interval
|
|
|
|
|
2021-11-24 22:51:57 +00:00
|
|
|
# parent class of all polyominos
|
|
|
|
class Polyomino():
|
|
|
|
def __init__(self,**kwargs):
|
|
|
|
# square elements that maje up the polyomino
|
|
|
|
self.squares=kwargs.get("squares",[])
|
2021-11-24 22:18:27 +00:00
|
|
|
|
|
|
|
self.color=kwargs.get("color",(0,0,1))
|
|
|
|
self.selected=False
|
|
|
|
|
2021-11-24 22:51:57 +00:00
|
|
|
# draw function
|
|
|
|
def draw(self):
|
2021-11-24 22:18:27 +00:00
|
|
|
# set color
|
|
|
|
if not self.selected:
|
|
|
|
Color(*self.color)
|
|
|
|
else:
|
|
|
|
(r,g,b)=self.color
|
|
|
|
# darken selected
|
|
|
|
Color(r/2,g/2,b/2)
|
|
|
|
|
2021-11-24 22:51:57 +00:00
|
|
|
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)
|
|
|
|
))
|
2021-11-25 00:22:05 +00:00
|
|
|
|
|
|
|
# whether x is in the support of the polyomino
|
|
|
|
def in_support(self,x):
|
|
|
|
for square in self.squares:
|
|
|
|
if l_infinity(square.pos-x)<=1/2:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
# check whether self interacts with candidate if candidate were moved by offset
|
|
|
|
def check_interaction(self,candidate,offset):
|
|
|
|
for square1 in self.squares:
|
|
|
|
for square2 in candidate.squares:
|
|
|
|
if square1.check_interaction(square2.pos+offset):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
# check whether self touches candidate
|
|
|
|
def check_touch(self,candidate):
|
|
|
|
for square1 in self.squares:
|
|
|
|
for square2 in candidate.squares:
|
|
|
|
if square1.check_touch(square2.pos):
|
|
|
|
return True
|
|
|
|
return False
|
2021-11-24 22:51:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
# square
|
|
|
|
class Square(Polyomino):
|
|
|
|
def __init__(self,x,y,**kwargs):
|
2021-11-25 00:22:05 +00:00
|
|
|
super(Square,self).__init__(kwargs,squares=[Square_element(x,y)])
|
2021-11-24 22:51:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2021-11-24 22:18:27 +00:00
|
|
|
|
|
|
|
# check whether a square at pos interacts with square
|
|
|
|
def check_interaction(self,pos):
|
2021-11-25 00:22:05 +00:00
|
|
|
return l_infinity(pos-self.pos)<1
|
2021-11-24 22:18:27 +00:00
|
|
|
|
|
|
|
# check whether a square at position pos is touching self
|
|
|
|
def check_touch(self,pos):
|
|
|
|
# allow for error
|
|
|
|
if in_interval(l_infinity(pos-solf.pos),1-1e-11,1+1e-11):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
# find position along a line that comes in contact with the line going through pos in direction v
|
|
|
|
def move_on_line_to_stick(self,pos,v):
|
|
|
|
# compute intersections with four lines making up square
|
|
|
|
intersections=[\
|
|
|
|
Point(self.pos.x+1/2,pos.y+v.y/v.x*(self.pos.x+1/2-pos.x)),\
|
|
|
|
Point(self.pos.x-1/2,pos.y+v.y/v.x*(self.pos.x-1/2-pos.x)),\
|
|
|
|
Point(pos.x+v.x/v.y*(self.pos.y+1/2-pos.y),self.pos.y+1/2),\
|
|
|
|
Point(pos.x+v.x/v.y*(self.pos.y-1/2-pos.y),self.pos.y-1/2)\
|
|
|
|
]
|
|
|
|
|
|
|
|
# compute closest one, on square
|
|
|
|
closest=None
|
|
|
|
dist=math.inf
|
|
|
|
for i in range(0,4):
|
|
|
|
# check that it is on square
|
|
|
|
if abs(intersections[i].x-self.pos.x)<=1/2+1e-11 and abs(intersections[i].y-self.pos.y)<=1/2+1e-11:
|
|
|
|
if (intersections[i]-pos)**2<dist:
|
|
|
|
closest=intersections[i]
|
|
|
|
dist=(intersections[i]-pos)**2
|
|
|
|
|
|
|
|
if closest==None:
|
|
|
|
print("error: cannot move particle at (",pos.x,",",pos.y,") to the boundary of (",self.pos.x,",",self.pos.y,")",file=sys.stderr)
|
|
|
|
exit(-1)
|
|
|
|
|
|
|
|
return closest
|
|
|
|
|
|
|
|
# move along edge of square
|
2021-11-25 00:22:05 +00:00
|
|
|
def move_along(self,delta,pos):
|
2021-11-24 22:18:27 +00:00
|
|
|
rel=pos-self.pos
|
|
|
|
# check if the particle is stuck in the x direction
|
|
|
|
if isint_nonzero(rel.x):
|
|
|
|
# check y direction
|
|
|
|
if isint_nonzero(rel.y):
|
|
|
|
# in corner
|
2021-11-25 00:22:05 +00:00
|
|
|
if sgn(delta.y)==-sgn(rel.y):
|
2021-11-24 22:18:27 +00:00
|
|
|
# stuck in x direction
|
2021-11-25 00:22:05 +00:00
|
|
|
return self.move_stuck_x(delta,pos)
|
|
|
|
elif sgn(delta.x)==-sgn(rel.x):
|
2021-11-24 22:18:27 +00:00
|
|
|
# stuck in y direction
|
2021-11-25 00:22:05 +00:00
|
|
|
return self.move_stuck_y(delta,pos)
|
2021-11-24 22:18:27 +00:00
|
|
|
# stuck in both directions
|
|
|
|
return pos
|
|
|
|
else:
|
|
|
|
# stuck in x direction
|
2021-11-25 00:22:05 +00:00
|
|
|
return self.move_stuck_x(delta,pos)
|
2021-11-24 22:18:27 +00:00
|
|
|
elif isint_nonzero(rel.y):
|
|
|
|
# stuck in y direction
|
2021-11-25 00:22:05 +00:00
|
|
|
return self.move_stuck_y(delta,pos)
|
2021-11-24 22:18:27 +00:00
|
|
|
# this should never happen
|
|
|
|
else:
|
|
|
|
print("error: stuck particle has non-integer relative position: (",rel.x,",",rel.y,")",file=sys.stderr)
|
|
|
|
exit(-1)
|
|
|
|
# move when stuck in the x direction
|
2021-11-25 00:22:05 +00:00
|
|
|
def move_stuck_x(self,delta,pos):
|
2021-11-24 22:18:27 +00:00
|
|
|
# only move in y direction
|
2021-11-25 00:22:05 +00:00
|
|
|
candidate=Point(pos.x,pos.x+delta.y)
|
2021-11-24 22:18:27 +00:00
|
|
|
# do not move past corners
|
|
|
|
rel=pos.y-self.pos.y
|
2021-11-25 00:22:05 +00:00
|
|
|
if delta.y>0:
|
|
|
|
if rel<math.ceil(rel)-1e-11 and delta.y+rel>math.ceil(rel)+1e-11 and math.ceil(rel)!=0:
|
2021-11-24 22:18:27 +00:00
|
|
|
# stick to corner
|
|
|
|
candidate.y=math.ceil(rel)+self.pos.y
|
|
|
|
else:
|
2021-11-25 00:22:05 +00:00
|
|
|
if rel>math.floor(rel)+1e-11 and delta.y+rel<math.floor(rel)-1e-11 and math.floor(rel)!=0:
|
2021-11-24 22:18:27 +00:00
|
|
|
# stick to corner
|
|
|
|
candidate.y=math.floor(rel)+self.pos.y
|
|
|
|
return candidate
|
|
|
|
# move when stuck in the y direction
|
2021-11-25 00:22:05 +00:00
|
|
|
def move_stuck_y(self,delta,pos):
|
2021-11-24 22:18:27 +00:00
|
|
|
# onlx move in x direction
|
|
|
|
candidate=Point(pos.x,newpos.x)
|
|
|
|
# do not move past corners
|
|
|
|
rel=pos.x-self.pos.x
|
2021-11-25 00:22:05 +00:00
|
|
|
if delta.x>0:
|
|
|
|
if rel<math.ceil(rel)-1e-11 and delta.x+rel>math.ceil(rel)+1e-11 and math.ceil(rel)!=0:
|
2021-11-24 22:18:27 +00:00
|
|
|
# stick to corner
|
|
|
|
candidate.x=math.ceil(rel)+self.pos.x
|
|
|
|
else:
|
2021-11-25 00:22:05 +00:00
|
|
|
if rel>math.floor(rel)+1e-11 and delta.x+rel<math.floor(rel)-1e-11 and math.floor(rel)!=0:
|
2021-11-24 22:18:27 +00:00
|
|
|
# stick to corner
|
|
|
|
candidate.x=math.floor(rel)+self.pos.x
|
|
|
|
return candidate
|
|
|
|
|