Staircases

This commit is contained in:
Ian Jauslin 2024-02-22 16:41:45 -05:00
parent 7e44418829
commit 29355191d6
3 changed files with 43 additions and 3 deletions

View File

@ -20,7 +20,7 @@ import os.path
import filecheck import filecheck
import colors import colors
from polyomino import Cross,Disk from polyomino import Cross,Disk,Staircase
class Command_prompt(Label): class Command_prompt(Label):
@ -388,6 +388,8 @@ class Command_prompt(Label):
self.app.painter.shape=Cross self.app.painter.shape=Cross
elif argv[2]=="disk": elif argv[2]=="disk":
self.app.painter.shape=Disk self.app.painter.shape=Disk
elif argv[2]=="staircase":
self.app.painter.shape=Staircase
else: else:
self.message="error: unrecognized shape '"+argv[2]+"'; supported shapes are cross|disk" self.message="error: unrecognized shape '"+argv[2]+"'; supported shapes are cross|disk"
return return

View File

@ -21,7 +21,7 @@ from kivy.core.window import Window
from kivy.graphics import Color,Line,Rectangle from kivy.graphics import Color,Line,Rectangle
from point import Point from point import Point
from polyomino import Cross,Disk from polyomino import Cross,Disk,Staircase
from tools import remove_fromlist from tools import remove_fromlist
@ -536,8 +536,12 @@ class Painter(Widget):
# save state (particle shape, zoom, lattice) # save state (particle shape, zoom, lattice)
if self.shape==Cross: if self.shape==Cross:
ff.write("%shape=cross\n") ff.write("%shape=cross\n")
else: elif self.shape==Disk:
ff.write("%shape=disk\n") 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("%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])) ff.write("%color={:d},{:d},{:d}\n".format(self.color[0],self.color[1],self.color[2]))
if self.lattice != None: if self.lattice != None:
@ -547,6 +551,8 @@ class Painter(Widget):
ff.write("{:d};".format(CROSS_INDEX)) ff.write("{:d};".format(CROSS_INDEX))
elif type(particle)==Disk: elif type(particle)==Disk:
ff.write("{:d};".format(DISK_INDEX)) 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.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() ff.close()
@ -649,6 +655,8 @@ class Painter(Widget):
candidate=Cross(pos.x,pos.y,color=color) candidate=Cross(pos.x,pos.y,color=color)
elif particle_type==DISK_INDEX: elif particle_type==DISK_INDEX:
candidate=Disk(pos.x,pos.y,color=color) candidate=Disk(pos.x,pos.y,color=color)
elif particle_type==STAIRCASE_INDEX:
candidate=Staircase(pos.x,pos.y,color=color)
else: else:
print("warning: ignoring line "+str(i)+" in file '"+file+"': unrecognized particle type: '"+entries[0]+"'",file=sys.stderr) print("warning: ignoring line "+str(i)+" in file '"+file+"': unrecognized particle type: '"+entries[0]+"'",file=sys.stderr)
continue continue
@ -702,5 +710,6 @@ class Painter(Widget):
# global variables (used like precompiler variables) # global variables (used like precompiler variables)
CROSS_INDEX=1 CROSS_INDEX=1
DISK_INDEX=2 DISK_INDEX=2
STAIRCASE_INDEX=2

View File

@ -123,3 +123,32 @@ class Cross(Polyomino):
class Disk(Polyomino): class Disk(Polyomino):
def __init__(self,x,y,**kwargs): def __init__(self,x,y,**kwargs):
super(Disk,self).__init__(**kwargs,elements=[Element_circle(x,y,size=kwargs.get("size",1.0))]) 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),
))