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 point import Point
|
||||||
from polyomino import Cross
|
from polyomino import Cross
|
||||||
from polyomino import Square_element
|
from polyomino import Square_element
|
||||||
|
from lattice import Square_lattice
|
||||||
|
|
||||||
from tools import remove_fromlist
|
from tools import remove_fromlist
|
||||||
|
|
||||||
@ -20,6 +21,10 @@ class Painter(Widget):
|
|||||||
# list of particles
|
# list of particles
|
||||||
self.particles=[]
|
self.particles=[]
|
||||||
|
|
||||||
|
# underlying lattice
|
||||||
|
#self.lattice=None
|
||||||
|
self.lattice=Square_lattice()
|
||||||
|
|
||||||
# particle under mouse
|
# particle under mouse
|
||||||
self.undermouse=None
|
self.undermouse=None
|
||||||
|
|
||||||
@ -74,6 +79,10 @@ class Painter(Widget):
|
|||||||
for particle in self.particles:
|
for particle in self.particles:
|
||||||
particle.draw(self)
|
particle.draw(self)
|
||||||
|
|
||||||
|
# draw lattice
|
||||||
|
if self.lattice!=None:
|
||||||
|
self.lattice.draw(self)
|
||||||
|
|
||||||
# draw grids
|
# draw grids
|
||||||
for particle in self.particles:
|
for particle in self.particles:
|
||||||
if particle.grid>0:
|
if particle.grid>0:
|
||||||
@ -175,6 +184,10 @@ class Painter(Widget):
|
|||||||
# create new cross
|
# create new cross
|
||||||
if touch.button=="right":
|
if touch.button=="right":
|
||||||
new=Cross(touchx,touchy)
|
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)):
|
if not self.check_interaction_any(new,Point(0,0)):
|
||||||
# add to list
|
# add to list
|
||||||
self.particles.append(new)
|
self.particles.append(new)
|
||||||
@ -237,18 +250,30 @@ class Painter(Widget):
|
|||||||
|
|
||||||
# respond to drag
|
# respond to drag
|
||||||
def on_touch_move(self,touch):
|
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
|
# only respond to touch in drawing area
|
||||||
if self.collide_point(*touch.pos):
|
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
|
# only move on left click
|
||||||
if touch.button=="left" and self.modifiers==[] and self.undermouse!=None:
|
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
|
# 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)
|
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
|
# redraw
|
||||||
self.draw()
|
self.draw()
|
||||||
|
@ -8,7 +8,7 @@ from tools import isint_nonzero,sgn,in_interval
|
|||||||
# parent class of all polyominos
|
# parent class of all polyominos
|
||||||
class Polyomino():
|
class Polyomino():
|
||||||
def __init__(self,**kwargs):
|
def __init__(self,**kwargs):
|
||||||
# square elements that maje up the polyomino
|
# square elements that make up the polyomino
|
||||||
self.squares=kwargs.get("squares",[])
|
self.squares=kwargs.get("squares",[])
|
||||||
|
|
||||||
self.color=kwargs.get("color",(0,0,1))
|
self.color=kwargs.get("color",(0,0,1))
|
||||||
|
Loading…
Reference in New Issue
Block a user