61 lines
1.8 KiB
Python
61 lines
1.8 KiB
Python
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
|
|
|
|
# return the lattice according to a specification
|
|
def new(spec):
|
|
specs=spec.split(":")
|
|
# check type of lattice
|
|
if specs[0]=="square":
|
|
return Lattice_square.new_square(specs[1:],spec)
|
|
else:
|
|
return(None,"error: unrecognized lattice type: '"+specs[0]+"'")
|
|
|
|
|
|
# square lattice
|
|
class Lattice_square(Lattice):
|
|
def __init__(self,**kwargs):
|
|
|
|
self.spacing=kwargs.get("spacing",1.)
|
|
|
|
super(Lattice_square,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(self.spacing/2,self.spacing/2),self.spacing)
|
|
|
|
# return the lattice according to a specification
|
|
def new_square(specs,spec):
|
|
# no optional args
|
|
if len(specs)==0:
|
|
return (Lattice_square(),"")
|
|
if len(specs)>1:
|
|
return (None,"error: '"+spec+"' is not a valid specification for the square lattice: should be 'square[:spacing]'")
|
|
try:
|
|
spacing=float(specs[0])
|
|
return (Lattice_square(spacing=spacing),"")
|
|
except:
|
|
return (None,"error: '"+spec+"' is not a valid specification for the square lattice: should be 'square[:spacing]'")
|
|
|