From 121b8fe4b0b13a003648314b8a4df9ca262a143b Mon Sep 17 00:00:00 2001 From: Ian Jauslin Date: Thu, 22 Feb 2024 17:19:52 -0500 Subject: [PATCH] 2squares --- src/command_prompt.py | 4 +++- src/painter.py | 11 +++++++++-- src/polyomino.py | 25 +++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/command_prompt.py b/src/command_prompt.py index 70e012b..5046997 100644 --- a/src/command_prompt.py +++ b/src/command_prompt.py @@ -20,7 +20,7 @@ import os.path import filecheck import colors -from polyomino import Cross,Disk,Staircase +from polyomino import Cross,Disk,Staircase,Square2 class Command_prompt(Label): @@ -390,6 +390,8 @@ class Command_prompt(Label): self.app.painter.shape=Disk elif argv[2]=="staircase": self.app.painter.shape=Staircase + elif argv[2]=="2square": + self.app.painter.shape=Square2 else: self.message="error: unrecognized shape '"+argv[2]+"'; supported shapes are cross|disk" return diff --git a/src/painter.py b/src/painter.py index 9701023..4abf1c0 100644 --- a/src/painter.py +++ b/src/painter.py @@ -21,7 +21,7 @@ from kivy.core.window import Window from kivy.graphics import Color,Line,Rectangle from point import Point -from polyomino import Cross,Disk,Staircase +from polyomino import Cross,Disk,Staircase,Square2 from tools import remove_fromlist @@ -542,6 +542,8 @@ class Painter(Widget): ff.write("%shape=disk\n") elif self.shape==Staircase: ff.write("%shape=staircase\n") + elif self.shape==Square2: + ff.write("%shape=2square\n") else: print("bug: unrecognized shape in write: '"+str(self.shape)+"'") ff.write("%zoom={:1.1f}\n".format(self.base_size/50)) @@ -555,6 +557,8 @@ class Painter(Widget): ff.write("{:d};".format(DISK_INDEX)) elif type(particle)==Staircase: ff.write("{:d};".format(STAIRCASE_INDEX)) + elif type(particle)==Square2: + ff.write("{:d};".format(SQUARE2_INDEX)) ff.write("{:05.2f},{:05.2f};{:3.1f},{:3.1f},{:3.1f}\n".format(particle.elements[0].pos.x,particle.elements[0].pos.y,particle.color[0],particle.color[1],particle.color[2])) ff.close() @@ -659,6 +663,8 @@ class Painter(Widget): candidate=Disk(pos.x,pos.y,color=color) elif particle_type==STAIRCASE_INDEX: candidate=Staircase(pos.x,pos.y,color=color) + elif particle_type==SQUARE2_INDEX: + candidate=Square2(pos.x,pos.y,color=color) else: print("warning: ignoring line "+str(i)+" in file '"+file+"': unrecognized particle type: '"+entries[0]+"'",file=sys.stderr) continue @@ -712,6 +718,7 @@ class Painter(Widget): # global variables (used like precompiler variables) CROSS_INDEX=1 DISK_INDEX=2 -STAIRCASE_INDEX=2 +STAIRCASE_INDEX=3 +SQUARE2_INDEX=4 diff --git a/src/polyomino.py b/src/polyomino.py index e8fdf27..e501680 100644 --- a/src/polyomino.py +++ b/src/polyomino.py @@ -152,3 +152,28 @@ class Staircase(Polyomino): *(coordx-0.5*painter.base_size,coordy+1.5*painter.base_size), *(coordx-0.5*painter.base_size,coordy-1.5*painter.base_size), )) + +# 2-square +class Square2(Polyomino): + def __init__(self,x,y,**kwargs): + super(Square2,self).__init__(**kwargs,elements=[\ + Element_square(x,y,1),\ + Element_square(x+1,y,1),\ + Element_square(x,y+1,1),\ + Element_square(x+1,y+1,1)\ + ]) + + # redefine stroke to avoid lines between touching elements + def stroke(self,painter): + # convert to graphical coordinates + coordx=painter.pos_tocoord_x(self.elements[0].pos.x) + coordy=painter.pos_tocoord_y(self.elements[0].pos.y) + + Color(1,1,1) + Line(points=( + *(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+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), + ))