disk polyominoes
This commit is contained in:
		@@ -4,7 +4,7 @@ import sys
 | 
			
		||||
 | 
			
		||||
from point import Point,l_infinity,l_2
 | 
			
		||||
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
 | 
			
		||||
class Element():
 | 
			
		||||
@@ -199,6 +199,13 @@ class Element_square(Element):
 | 
			
		||||
# circular elements
 | 
			
		||||
# (size is the diameter)
 | 
			
		||||
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
 | 
			
		||||
    # TODO: this only works if element is a circle!
 | 
			
		||||
@@ -227,10 +234,10 @@ class Element_circle(Element):
 | 
			
		||||
        R=(element.size+self.size)/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 t*v
 | 
			
		||||
        return v*t
 | 
			
		||||
 | 
			
		||||
    # move along edge of 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 point import Point
 | 
			
		||||
from polyomino import Cross
 | 
			
		||||
from polyomino import Cross,Disk
 | 
			
		||||
 | 
			
		||||
from tools import remove_fromlist
 | 
			
		||||
 | 
			
		||||
@@ -215,7 +215,8 @@ class Painter(Widget):
 | 
			
		||||
 | 
			
		||||
            # create new particle
 | 
			
		||||
            if touch.button=="right":
 | 
			
		||||
                new=Cross(touchx,touchy)
 | 
			
		||||
                #new=Cross(touchx,touchy)
 | 
			
		||||
                new=Disk(touchx,touchy)
 | 
			
		||||
                # snap to lattice
 | 
			
		||||
                if self.lattice!=None:
 | 
			
		||||
                    new.move(self.lattice.nearest_delta(new.elements[0].pos))
 | 
			
		||||
 
 | 
			
		||||
@@ -52,4 +52,4 @@ def l_infinity(x):
 | 
			
		||||
 | 
			
		||||
# L 2 norm
 | 
			
		||||
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 point import l_infinity
 | 
			
		||||
from element import Element_square
 | 
			
		||||
from element import Element_square,Element_circle
 | 
			
		||||
 | 
			
		||||
# parent class of all polyominos
 | 
			
		||||
class Polyomino():
 | 
			
		||||
@@ -105,3 +105,7 @@ class Cross(Polyomino):
 | 
			
		||||
        *(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))])
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user