lattice
This commit is contained in:
parent
88959cb233
commit
9cb25730eb
38
src/lattice.py
Normal file
38
src/lattice.py
Normal file
@ -0,0 +1,38 @@
|
||||
from point import Point
|
||||
|
||||
# parent class of all lattices
|
||||
class Lattice():
|
||||
def __init__(self,**kwargs):
|
||||
self.type=kwargs.get("type","")
|
||||
|
||||
# lattice point nearest to point
|
||||
# overwrite in subclasses
|
||||
def nearest(self,point):
|
||||
return point
|
||||
|
||||
# delta to nearest point
|
||||
def nearest_delta(self,point):
|
||||
return self.nearest(point)-point
|
||||
|
||||
# draw lattice
|
||||
# overwrite in subclasses
|
||||
def draw(self,painter):
|
||||
return
|
||||
|
||||
|
||||
# square lattice
|
||||
class Square_lattice(Lattice):
|
||||
def __init__(self,**kwargs):
|
||||
|
||||
self.spacing=kwargs.get("spacing",1.)
|
||||
|
||||
super(Square_lattice,self).__init__(**kwargs,type="square")
|
||||
|
||||
# lattice point nearest to point
|
||||
def nearest(self,point):
|
||||
return Point(round(point.x/self.spacing)*self.spacing,round(point.y/self.spacing)*self.spacing)
|
||||
|
||||
# draw
|
||||
def draw(self,painter):
|
||||
painter.draw_grid(Point(0,0),self.spacing)
|
||||
|
@ -7,6 +7,7 @@ from kivy.graphics import Color,Line
|
||||
from point import Point
|
||||
from polyomino import Cross
|
||||
from polyomino import Square_element
|
||||
from lattice import Square_lattice
|
||||
|
||||
from tools import remove_fromlist
|
||||
|
||||
@ -20,6 +21,10 @@ class Painter(Widget):
|
||||
# list of particles
|
||||
self.particles=[]
|
||||
|
||||
# underlying lattice
|
||||
#self.lattice=None
|
||||
self.lattice=Square_lattice()
|
||||
|
||||
# particle under mouse
|
||||
self.undermouse=None
|
||||
|
||||
@ -74,6 +79,10 @@ class Painter(Widget):
|
||||
for particle in self.particles:
|
||||
particle.draw(self)
|
||||
|
||||
# draw lattice
|
||||
if self.lattice!=None:
|
||||
self.lattice.draw(self)
|
||||
|
||||
# draw grids
|
||||
for particle in self.particles:
|
||||
if particle.grid>0:
|
||||
@ -175,6 +184,10 @@ class Painter(Widget):
|
||||
# create new cross
|
||||
if touch.button=="right":
|
||||
new=Cross(touchx,touchy)
|
||||
# snap to lattice
|
||||
if self.lattice!=None:
|
||||
new.move(self.lattice.nearest_delta(new.squares[0].pos))
|
||||
|
||||
if not self.check_interaction_any(new,Point(0,0)):
|
||||
# add to list
|
||||
self.particles.append(new)
|
||||
@ -237,18 +250,30 @@ class Painter(Widget):
|
||||
|
||||
# respond to drag
|
||||
def on_touch_move(self,touch):
|
||||
# convert to logical
|
||||
touchx=self.coord_topos_x(touch.x)
|
||||
touchy=self.coord_topos_y(touch.y)
|
||||
|
||||
# only respond to touch in drawing area
|
||||
if self.collide_point(*touch.pos):
|
||||
# convert to logical
|
||||
touchx=self.coord_topos_x(touch.x)
|
||||
touchy=self.coord_topos_y(touch.y)
|
||||
|
||||
# only move on left click
|
||||
if touch.button=="left" and self.modifiers==[] and self.undermouse!=None:
|
||||
# attempted move determined by the relative position to the relative position of click within self.undermouse
|
||||
delta=self.adjust_move(Point(touchx,touchy)-(self.offset+self.undermouse.squares[0].pos),0)
|
||||
for particle in self.selected:
|
||||
particle.move(delta)
|
||||
|
||||
# snap to lattice
|
||||
if self.lattice!=None:
|
||||
delta=self.lattice.nearest(delta)
|
||||
# check that the move is possible (which is not guaranteed after snapping to lattice)
|
||||
if not self.check_interaction_unselected_list(self.selected,delta):
|
||||
for particle in self.selected:
|
||||
particle.move(delta)
|
||||
|
||||
# no lattice, move is guaranteed to be acceptable
|
||||
else:
|
||||
for particle in self.selected:
|
||||
particle.move(delta)
|
||||
|
||||
# redraw
|
||||
self.draw()
|
||||
|
@ -8,7 +8,7 @@ from tools import isint_nonzero,sgn,in_interval
|
||||
# parent class of all polyominos
|
||||
class Polyomino():
|
||||
def __init__(self,**kwargs):
|
||||
# square elements that maje up the polyomino
|
||||
# square elements that make up the polyomino
|
||||
self.squares=kwargs.get("squares",[])
|
||||
|
||||
self.color=kwargs.get("color",(0,0,1))
|
||||
|
Loading…
Reference in New Issue
Block a user