From c2ebf92a0e1b99ae874ffe17627b4828232de565 Mon Sep 17 00:00:00 2001 From: Ian Jauslin Date: Tue, 27 Sep 2022 19:01:17 -0400 Subject: [PATCH] disk polyominoes --- src/element.py | 13 ++++++++++--- src/painter.py | 5 +++-- src/point.py | 2 +- src/polyomino.py | 6 +++++- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/element.py b/src/element.py index 2e81f74..b004318 100644 --- a/src/element.py +++ b/src/element.py @@ -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! diff --git a/src/painter.py b/src/painter.py index f2cc6a5..f9bf433 100644 --- a/src/painter.py +++ b/src/painter.py @@ -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)) diff --git a/src/point.py b/src/point.py index 91dca72..1856ed4 100644 --- a/src/point.py +++ b/src/point.py @@ -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) diff --git a/src/polyomino.py b/src/polyomino.py index 7487651..6f422a1 100644 --- a/src/polyomino.py +++ b/src/polyomino.py @@ -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))])