From 4b0352f5d7433e69e8febfc43a5f8431c35b642a Mon Sep 17 00:00:00 2001 From: Ian Jauslin Date: Wed, 1 Dec 2021 18:49:35 -0500 Subject: [PATCH] Variable mesh in grid --- command_prompt.py | 14 +++++++++++--- painter.py | 35 ++++++++++++++++++----------------- polyomino.py | 4 ++-- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/command_prompt.py b/command_prompt.py index b1cd71d..e3856c5 100644 --- a/command_prompt.py +++ b/command_prompt.py @@ -368,12 +368,20 @@ class Command_prompt(Label): if len(argv)==2: # no argument: set to toggle self.app.painter.set_grid(-1) - elif argv[2]=="on" or argv[2]=="1": + elif argv[2]=="on": self.app.painter.set_grid(1) - elif argv[2]=="off" or argv[2]=="0": + elif argv[2]=="off": self.app.painter.set_grid(0) else: - self.message="error: unrecognized argument '"+argv[2]+"' -- usage 'set grid [on|off]'" + try: + mesh=float(argv[2]) + except: + self.message="error: unrecognized argument '"+argv[2]+"' -- usage 'set grid [on|off|]'" + return + if mesh<0: + self.message="error: grid size cannot be negative: '"+argv[2]+"'" + return + self.app.painter.set_grid(mesh) diff --git a/painter.py b/painter.py index 8546c43..d54b314 100644 --- a/painter.py +++ b/painter.py @@ -55,27 +55,27 @@ class Painter(Widget): with self.canvas: # draw grids for particle in self.particles: - if particle.grid: - self.draw_grid(particle.squares[0].pos) + if particle.grid>0: + self.draw_grid(particle.squares[0].pos,particle.grid) for particle in self.particles: particle.draw() - def draw_grid(self,pos): + def draw_grid(self,pos,mesh): # height offset due to status bar and command prompt height_offset=self.app.status_bar.height+self.app.command_prompt.height # vertical lines # offest wrt 0 - offset=(pos.x-0.5)%1 - for i in range(math.floor(self.width/Square_element.size-offset)+1): + offset=(pos.x-mesh/2)%mesh + for i in range(math.floor((self.width/Square_element.size-offset)/mesh)+1): Color(1,1,1) - Line(points=((i+offset)*Square_element.size,height_offset,(i+offset)*Square_element.size,self.height+height_offset)) + Line(points=((i*mesh+offset)*Square_element.size,height_offset,(i*mesh+offset)*Square_element.size,self.height+height_offset)) # horizontal lines # offset wrt 0 - offset=(pos.y-0.5)%1-height_offset/Square_element.size - for i in range(math.floor(self.height/Square_element.size-offset)+1): + offset=(pos.y-mesh/2)%1-height_offset/Square_element.size + for i in range(math.floor((self.height/Square_element.size-offset)/mesh)+1): Color(1,1,1) - Line(points=(0,(i+offset)*Square_element.size+height_offset,self.width,(i+offset)*Square_element.size+height_offset)) + Line(points=(0,(i*mesh+offset)*Square_element.size+height_offset,self.width,(i*mesh+offset)*Square_element.size+height_offset)) # respond to keyboard @@ -280,15 +280,16 @@ class Painter(Widget): self.draw() # set grid for selected particles - # onoff: 1:on, 0:off, -1:toggle - def set_grid(self,onoff): + # set mesh to -1 to toggle on/off + def set_grid(self,mesh): for particle in self.selected: - if onoff==1: - particle.grid=True - elif onoff==0: - particle.grid=False - elif onoff==-1: - particle.grid=not particle.grid + if mesh==-1: + if particle.grid==0: + particle.grid=1 + else: + particle.grid=-particle.grid + else: + particle.grid=mesh # redraw self.draw() diff --git a/polyomino.py b/polyomino.py index 7bfe366..1b5aa22 100644 --- a/polyomino.py +++ b/polyomino.py @@ -14,8 +14,8 @@ class Polyomino(): self.color=kwargs.get("color",(0,0,1)) self.selected=False - # whether to draw a background grid - self.grid=kwargs.get("grid",False) + # mesh of background grid (no grid for mesh size 0) + self.grid=kwargs.get("grid",0) # draw function def draw(self):