From 29355191d66404bbe135e2c2fd9247f3b706f91e Mon Sep 17 00:00:00 2001 From: Ian Jauslin Date: Thu, 22 Feb 2024 16:41:45 -0500 Subject: [PATCH] Staircases --- src/command_prompt.py | 4 +++- src/painter.py | 13 +++++++++++-- src/polyomino.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/command_prompt.py b/src/command_prompt.py index 54712a5..70e012b 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 +from polyomino import Cross,Disk,Staircase class Command_prompt(Label): @@ -388,6 +388,8 @@ class Command_prompt(Label): self.app.painter.shape=Cross elif argv[2]=="disk": self.app.painter.shape=Disk + elif argv[2]=="staircase": + self.app.painter.shape=Staircase 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 cae39cb..cef8b4c 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 +from polyomino import Cross,Disk,Staircase from tools import remove_fromlist @@ -536,8 +536,12 @@ class Painter(Widget): # save state (particle shape, zoom, lattice) if self.shape==Cross: ff.write("%shape=cross\n") - else: + elif self.shape==Disk: ff.write("%shape=disk\n") + elif self.shape==Staircase: + ff.write("%shape=staircase\n") + else: + print("bug: unrecognized shape in write: '"+str(self.shape)+"'") ff.write("%zoom={:1.1f}\n".format(self.base_size/50)) ff.write("%color={:d},{:d},{:d}\n".format(self.color[0],self.color[1],self.color[2])) if self.lattice != None: @@ -547,6 +551,8 @@ class Painter(Widget): ff.write("{:d};".format(CROSS_INDEX)) elif type(particle)==Disk: ff.write("{:d};".format(DISK_INDEX)) + elif type(particle)==Staircase: + ff.write("{:d};".format(STAIRCASE_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() @@ -649,6 +655,8 @@ class Painter(Widget): candidate=Cross(pos.x,pos.y,color=color) elif particle_type==DISK_INDEX: candidate=Disk(pos.x,pos.y,color=color) + elif particle_type==STAIRCASE_INDEX: + candidate=Staircase(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 @@ -702,5 +710,6 @@ class Painter(Widget): # global variables (used like precompiler variables) CROSS_INDEX=1 DISK_INDEX=2 +STAIRCASE_INDEX=2 diff --git a/src/polyomino.py b/src/polyomino.py index 126a3d2..e8fdf27 100644 --- a/src/polyomino.py +++ b/src/polyomino.py @@ -123,3 +123,32 @@ class Cross(Polyomino): class Disk(Polyomino): def __init__(self,x,y,**kwargs): super(Disk,self).__init__(**kwargs,elements=[Element_circle(x,y,size=kwargs.get("size",1.0))]) + +# 3-staircase +class Staircase(Polyomino): + def __init__(self,x,y,**kwargs): + super(Staircase,self).__init__(**kwargs,elements=[\ + Element_square(x,y+1,1,aspect=3),\ + Element_square(x+1,y,1),\ + Element_square(x+1,y+1,1),\ + Element_square(x+2,y,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-1.5*painter.base_size), + *(coordx+2.5*painter.base_size,coordy-1.5*painter.base_size), + *(coordx+2.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-1.5*painter.base_size), + ))