disk polyominoes
This commit is contained in:
parent
c13b0f4556
commit
c2ebf92a0e
@ -4,7 +4,7 @@ import sys
|
|||||||
|
|
||||||
from point import Point,l_infinity,l_2
|
from point import Point,l_infinity,l_2
|
||||||
from tools import isint_nonzero,sgn,in_interval
|
from tools import isint_nonzero,sgn,in_interval
|
||||||
from kivy.graphics import Rectangle
|
from kivy.graphics import Rectangle,Ellipse,Line
|
||||||
|
|
||||||
# parent class of all elements
|
# parent class of all elements
|
||||||
class Element():
|
class Element():
|
||||||
@ -199,6 +199,13 @@ class Element_square(Element):
|
|||||||
# circular elements
|
# circular elements
|
||||||
# (size is the diameter)
|
# (size is the diameter)
|
||||||
class Element_circle(Element):
|
class Element_circle(Element):
|
||||||
|
# draw element
|
||||||
|
def draw(self,painter):
|
||||||
|
Ellipse(pos=(painter.pos_tocoord_x(self.pos.x-0.5*self.size),painter.pos_tocoord_y(self.pos.y-0.5*self.size)),size=(self.size*painter.base_size,self.size*painter.base_size))
|
||||||
|
|
||||||
|
# draw boundary
|
||||||
|
def stroke(self,painter):
|
||||||
|
Line(circle=(painter.pos_tocoord_x(self.pos.x),painter.pos_tocoord_y(self.pos.y),self.size*0.5*painter.base_size))
|
||||||
|
|
||||||
# check whether an element interacts with square
|
# check whether an element interacts with square
|
||||||
# TODO: this only works if element is a circle!
|
# TODO: this only works if element is a circle!
|
||||||
@ -227,10 +234,10 @@ class Element_circle(Element):
|
|||||||
R=(element.size+self.size)/2
|
R=(element.size+self.size)/2
|
||||||
|
|
||||||
# smallest root of t^2 v^2+2x.v t+x^2-R^2
|
# smallest root of t^2 v^2+2x.v t+x^2-R^2
|
||||||
t=(-v.dot(x)+sqrt(v.dot(x)*v.dot(v)-v.dot(v)*(x.dot(x)-R*R)))/v.dot(v)
|
t=(-v.dot(x)-math.sqrt(v.dot(x)*v.dot(x)-v.dot(v)*(x.dot(x)-R*R)))/v.dot(v)
|
||||||
|
|
||||||
# return difference to pos
|
# return difference to pos
|
||||||
return t*v
|
return v*t
|
||||||
|
|
||||||
# move along edge of circle
|
# move along edge of circle
|
||||||
# TODO: this only works if element is a circle!
|
# TODO: this only works if element is a circle!
|
||||||
|
@ -7,7 +7,7 @@ from kivy.core.window import Window
|
|||||||
from kivy.graphics import Color,Line
|
from kivy.graphics import Color,Line
|
||||||
|
|
||||||
from point import Point
|
from point import Point
|
||||||
from polyomino import Cross
|
from polyomino import Cross,Disk
|
||||||
|
|
||||||
from tools import remove_fromlist
|
from tools import remove_fromlist
|
||||||
|
|
||||||
@ -215,7 +215,8 @@ class Painter(Widget):
|
|||||||
|
|
||||||
# create new particle
|
# create new particle
|
||||||
if touch.button=="right":
|
if touch.button=="right":
|
||||||
new=Cross(touchx,touchy)
|
#new=Cross(touchx,touchy)
|
||||||
|
new=Disk(touchx,touchy)
|
||||||
# snap to lattice
|
# snap to lattice
|
||||||
if self.lattice!=None:
|
if self.lattice!=None:
|
||||||
new.move(self.lattice.nearest_delta(new.elements[0].pos))
|
new.move(self.lattice.nearest_delta(new.elements[0].pos))
|
||||||
|
@ -52,4 +52,4 @@ def l_infinity(x):
|
|||||||
|
|
||||||
# L 2 norm
|
# L 2 norm
|
||||||
def l_2(x):
|
def l_2(x):
|
||||||
return sqrt(x.x*x.x+x.y*x.y)
|
return math.sqrt(x.x*x.x+x.y*x.y)
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
from kivy.graphics import Color,Line
|
from kivy.graphics import Color,Line
|
||||||
|
|
||||||
from point import l_infinity
|
from point import l_infinity
|
||||||
from element import Element_square
|
from element import Element_square,Element_circle
|
||||||
|
|
||||||
# parent class of all polyominos
|
# parent class of all polyominos
|
||||||
class Polyomino():
|
class Polyomino():
|
||||||
@ -105,3 +105,7 @@ class Cross(Polyomino):
|
|||||||
*(coordx-0.5*painter.base_size,coordy-0.5*painter.base_size),
|
*(coordx-0.5*painter.base_size,coordy-0.5*painter.base_size),
|
||||||
))
|
))
|
||||||
|
|
||||||
|
# disk
|
||||||
|
class Disk(Polyomino):
|
||||||
|
def __init__(self,x,y,**kwargs):
|
||||||
|
super(Disk,self).__init__(**kwargs,elements=[Element_circle(x,y,size=kwargs.get("size",1.0))])
|
||||||
|
Loading…
Reference in New Issue
Block a user