2021-09-28 19:23:06 +00:00
|
|
|
import math
|
2021-09-29 03:36:07 +00:00
|
|
|
import sys
|
2021-09-28 19:23:06 +00:00
|
|
|
from kivy.graphics import Color,Line,Rectangle
|
|
|
|
from kivy.uix.widget import Widget
|
|
|
|
|
|
|
|
from point import Point
|
2021-09-29 03:36:07 +00:00
|
|
|
from tools import isint_nonzero,sgn
|
2021-09-28 19:23:06 +00:00
|
|
|
|
|
|
|
class Cross():
|
|
|
|
# size of central square
|
|
|
|
size=50
|
|
|
|
|
|
|
|
def __init__(self,x,y,**kwargs):
|
|
|
|
self.pos=Point(x,y)
|
|
|
|
self.color=kwargs.get("color",(0,0,1))
|
|
|
|
self.selected=False
|
|
|
|
|
|
|
|
# set position
|
|
|
|
def setpos(self,x,y):
|
|
|
|
self.pos.x=x
|
|
|
|
self.pos.y=y
|
|
|
|
|
2021-10-20 05:05:20 +00:00
|
|
|
def draw(self,**kwargs):
|
2021-10-20 15:05:18 +00:00
|
|
|
# set color
|
|
|
|
if not self.selected:
|
|
|
|
Color(*self.color)
|
|
|
|
else:
|
|
|
|
(r,g,b)=self.color
|
|
|
|
# darken selected
|
|
|
|
Color(r/2,g/2,b/2)
|
|
|
|
|
2021-09-28 19:35:41 +00:00
|
|
|
Rectangle(pos=((self.pos.x-1.5)*self.size,(self.pos.y-0.5)*self.size),size=(3*self.size,self.size))
|
|
|
|
Rectangle(pos=((self.pos.x-0.5)*self.size,(self.pos.y-1.5)*self.size),size=(self.size,3*self.size))
|
2021-09-28 19:23:06 +00:00
|
|
|
|
|
|
|
# stroke
|
|
|
|
Color(1,1,1)
|
|
|
|
Line(points=(
|
2021-09-28 19:35:41 +00:00
|
|
|
*((self.pos.x-0.5)*self.size,(self.pos.y-0.5)*self.size),
|
|
|
|
*((self.pos.x-0.5)*self.size,(self.pos.y-1.5)*self.size),
|
|
|
|
*((self.pos.x+0.5)*self.size,(self.pos.y-1.5)*self.size),
|
|
|
|
*((self.pos.x+0.5)*self.size,(self.pos.y-0.5)*self.size),
|
|
|
|
*((self.pos.x+1.5)*self.size,(self.pos.y-0.5)*self.size),
|
|
|
|
*((self.pos.x+1.5)*self.size,(self.pos.y+0.5)*self.size),
|
|
|
|
*((self.pos.x+0.5)*self.size,(self.pos.y+0.5)*self.size),
|
|
|
|
*((self.pos.x+0.5)*self.size,(self.pos.y+1.5)*self.size),
|
|
|
|
*((self.pos.x-0.5)*self.size,(self.pos.y+1.5)*self.size),
|
|
|
|
*((self.pos.x-0.5)*self.size,(self.pos.y+0.5)*self.size),
|
|
|
|
*((self.pos.x-1.5)*self.size,(self.pos.y+0.5)*self.size),
|
|
|
|
*((self.pos.x-1.5)*self.size,(self.pos.y-0.5)*self.size),
|
|
|
|
*((self.pos.x-0.5)*self.size,(self.pos.y-0.5)*self.size),
|
2021-09-28 19:23:06 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
# check whether a cross at pos interacts with cross
|
|
|
|
def check_interaction(self,pos):
|
2021-09-29 07:24:23 +00:00
|
|
|
# allow for error
|
|
|
|
return int(pos.x-self.pos.x+sgn(pos.x-self.pos.x)*1e-11)**2+int(pos.y-self.pos.y+sgn(pos.y-self.pos.y)*1e-11)**2>=5
|
|
|
|
|
|
|
|
# check whether a cross at position pos is touching self
|
|
|
|
def check_touch(self,pos):
|
|
|
|
rel=pos-self.pos
|
|
|
|
for i in [-3,-2,-1,1,2,3]:
|
|
|
|
# allow for error
|
|
|
|
if abs(rel.x-i)<1e-11 and abs(rel.y)<=4-abs(i)+1e-11 and abs(rel.y)>=3-abs(i)-1e-11:
|
|
|
|
return True
|
|
|
|
if abs(rel.y-i)<1e-11 and abs(rel.x)<=4-abs(i)+1e-11 and abs(rel.x)>=3-abs(i)-1e-11:
|
|
|
|
return True
|
|
|
|
return False
|
2021-09-28 19:23:06 +00:00
|
|
|
|
|
|
|
# find position along a line that comes in contact with the line going through pos in direction v
|
|
|
|
def move_on_line_to_stick(self,pos,v):
|
|
|
|
# relative to cross
|
2021-09-28 19:35:41 +00:00
|
|
|
return self.move_on_line_to_stick_relative(pos-self.pos,v)+self.pos
|
2021-09-28 19:23:06 +00:00
|
|
|
def move_on_line_to_stick_relative(self,x,v):
|
|
|
|
|
|
|
|
# if x is in the right quadrant
|
|
|
|
if abs(x.y)<=x.x:
|
|
|
|
# find all stuck positions on lines
|
|
|
|
stuck=[]
|
|
|
|
|
|
|
|
# check intersections with vertical lines
|
|
|
|
if v.x!=0:
|
|
|
|
for i in range(1,4):
|
|
|
|
# candidate
|
|
|
|
y=Point(i,x.y+(i-x.x)*v.y/v.x)
|
|
|
|
# check that it is in the right range
|
|
|
|
if abs(y.y)<=4-i and abs(y.y)>=3-i:
|
|
|
|
stuck.append(y)
|
|
|
|
# check intersections with horizontal lines
|
|
|
|
if v.y!=0:
|
|
|
|
for i in [-3,-2,-1,1,2,3]:
|
|
|
|
# candidate
|
|
|
|
y=Point(x.x+(i-x.y)*v.x/v.y,i)
|
|
|
|
# check that it is in the right range
|
|
|
|
if y.x<=4-abs(i) and y.x>=3-abs(i):
|
|
|
|
stuck.append(y)
|
|
|
|
|
|
|
|
return x.closest(stuck)
|
|
|
|
|
|
|
|
# reflect other quadrants to the right one
|
|
|
|
# top quadrant
|
|
|
|
elif abs(x.x)<=x.y:
|
|
|
|
closest=self.move_on_line_to_stick_relative(Point(x.y,x.x),Point(v.y,v.x))
|
|
|
|
return Point(closest.y,closest.x)
|
|
|
|
# bottom quadrant
|
|
|
|
elif abs(x.x)<=-x.y:
|
|
|
|
closest=self.move_on_line_to_stick_relative(Point(-x.y,x.x),Point(-v.y,v.x))
|
|
|
|
return Point(closest.y,-closest.x)
|
|
|
|
# left quadrant
|
|
|
|
else:
|
|
|
|
closest=self.move_on_line_to_stick_relative(Point(-x.x,x.y),Point(-v.x,v.y))
|
|
|
|
return Point(-closest.x,closest.y)
|
|
|
|
|
2021-09-29 07:24:23 +00:00
|
|
|
# move along edge of cross
|
2021-09-29 03:36:07 +00:00
|
|
|
def move_along(self,newpos,pos):
|
|
|
|
rel=pos-self.pos
|
|
|
|
# check if the particle is stuck in the x direction
|
|
|
|
if isint_nonzero(rel.x):
|
|
|
|
# check y direction
|
|
|
|
if isint_nonzero(rel.y):
|
|
|
|
# in corner
|
|
|
|
# two types of corners: |x|_1=3 or |x|_1=4
|
|
|
|
if abs(rel.x)+abs(rel.y)<3.5:
|
|
|
|
if sgn(newpos.y-pos.y)==sgn(rel.y):
|
|
|
|
# stuck in x direction
|
|
|
|
return self.move_stuck_x(newpos,pos)
|
|
|
|
elif sgn(newpos.x-pos.x)==sgn(rel.x):
|
|
|
|
# stuck in y direction
|
|
|
|
return self.move_stuck_y(newpos,pos)
|
|
|
|
else:
|
|
|
|
if sgn(newpos.y-pos.y)==-sgn(rel.y):
|
|
|
|
# stuck in x direction
|
|
|
|
return self.move_stuck_x(newpos,pos)
|
|
|
|
elif sgn(newpos.x-pos.x)==-sgn(rel.x):
|
|
|
|
# stuck in y direction
|
|
|
|
return self.move_stuck_y(newpos,pos)
|
|
|
|
# stuck in both directions
|
|
|
|
return pos
|
|
|
|
else:
|
|
|
|
# stuck in x direction
|
|
|
|
return self.move_stuck_x(newpos,pos)
|
|
|
|
elif isint_nonzero(rel.y):
|
|
|
|
# stuck in y direction
|
|
|
|
return self.move_stuck_y(newpos,pos)
|
|
|
|
# this should never happen
|
|
|
|
else:
|
|
|
|
print("error: stuck particle has non-integer relative position: (",rel.x,",",rel.y,")",file=sys.stderr)
|
|
|
|
exit(-1)
|
|
|
|
# move when stuck in the x direction
|
|
|
|
def move_stuck_x(self,newpos,pos):
|
|
|
|
# only move in y direction
|
|
|
|
candidate=Point(pos.x,newpos.y)
|
|
|
|
# do not move past corners
|
|
|
|
rel=pos.y-self.pos.y
|
|
|
|
newrel=newpos.y-self.pos.y
|
|
|
|
if newpos.y>pos.y:
|
|
|
|
if self.check_interaction(candidate)==False or (rel<math.ceil(rel)-1e-11 and newrel>math.ceil(rel)+1e-11 and math.ceil(rel)!=0):
|
|
|
|
# in open corner
|
|
|
|
if rel>math.ceil(rel)-1e-11:
|
|
|
|
candidate.y=math.ceil(rel)+1+self.pos.y
|
|
|
|
else:
|
|
|
|
candidate.y=math.ceil(rel)+self.pos.y
|
|
|
|
else:
|
|
|
|
if self.check_interaction(candidate)==False or (rel>math.floor(rel)+1e-11 and newrel<math.floor(rel)-1e-11 and math.floor(rel)!=0):
|
|
|
|
# in open corner
|
|
|
|
if rel<math.floor(rel)+1e-11:
|
|
|
|
candidate.y=math.floor(rel)-1+self.pos.y
|
|
|
|
else:
|
|
|
|
candidate.y=math.floor(rel)+self.pos.y
|
|
|
|
return candidate
|
|
|
|
# move when stuck in the y direction
|
|
|
|
def move_stuck_y(self,newpos,pos):
|
|
|
|
# only move in x direction
|
|
|
|
candidate=Point(newpos.x,pos.y)
|
|
|
|
# do not move past corners
|
|
|
|
rel=pos.x-self.pos.x
|
|
|
|
newrel=newpos.x-self.pos.x
|
|
|
|
if newpos.x>pos.x:
|
|
|
|
if self.check_interaction(candidate)==False or (rel<math.ceil(rel)-1e-11 and newrel>math.ceil(rel)+1e-11 and math.ceil(rel)!=0):
|
|
|
|
# in open corner
|
|
|
|
if rel>math.ceil(rel)-1e-11:
|
|
|
|
candidate.x=math.ceil(rel)+1+self.pos.x
|
|
|
|
else:
|
|
|
|
candidate.x=math.ceil(rel)+self.pos.x
|
|
|
|
else:
|
|
|
|
if self.check_interaction(candidate)==False or (rel>math.floor(rel)+1e-11 and newrel<math.floor(rel)-1e-11 and math.floor(rel)!=0):
|
|
|
|
# in open corner
|
|
|
|
if rel<math.floor(rel)+1e-11:
|
|
|
|
candidate.x=math.floor(rel)-1+self.pos.x
|
|
|
|
else:
|
|
|
|
candidate.x=math.floor(rel)+self.pos.x
|
|
|
|
return candidate
|
|
|
|
|
|
|
|
|
2021-09-28 19:23:06 +00:00
|
|
|
|
|
|
|
# L_infinity distance rescalled by 3 in the x direction
|
|
|
|
def cross_distx(x,y):
|
|
|
|
return max(abs(x.x-y.x)/3,abs(x.y-y.y))
|
|
|
|
# L_infinity distance rescalled by 3 in the y direction
|
|
|
|
def cross_disty(x,y):
|
|
|
|
return max(abs(x.x-y.x),abs(x.y-y.y)/3)
|
|
|
|
|
|
|
|
# polar description of touching cross
|
|
|
|
def cross_polar(t):
|
|
|
|
# by symmetry, put angle in interval (-pi/4,pi/4), and take absolute value
|
|
|
|
tt=abs((t+math.pi/4)%(math.pi/2)-math.pi/4)
|
|
|
|
if tt<math.atan(1/3):
|
|
|
|
return 3/math.cos(tt)
|
|
|
|
elif tt<math.atan(1/2):
|
|
|
|
return 1/math.sin(tt)
|
|
|
|
else:
|
|
|
|
return 2/math.cos(tt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# cross painter
|
|
|
|
class Cross_painter(Widget):
|
|
|
|
|
2021-09-30 05:46:12 +00:00
|
|
|
def __init__(self,app,**kwargs):
|
2021-09-28 19:23:06 +00:00
|
|
|
|
|
|
|
# list of crosses
|
|
|
|
self.crosses=[]
|
|
|
|
|
|
|
|
# selected cross
|
|
|
|
self.selected=None
|
|
|
|
|
2021-09-30 05:46:12 +00:00
|
|
|
# app is used to share information between widgets
|
|
|
|
self.app=app
|
|
|
|
|
2021-09-28 19:23:06 +00:00
|
|
|
# init Widget
|
|
|
|
super(Cross_painter,self).__init__(**kwargs)
|
|
|
|
|
2021-10-18 22:57:35 +00:00
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
self.crosses=[]
|
|
|
|
self.selected=None
|
|
|
|
self.draw()
|
|
|
|
|
2021-09-28 19:23:06 +00:00
|
|
|
|
|
|
|
# draw all crosses
|
|
|
|
def draw(self):
|
2021-10-20 04:37:09 +00:00
|
|
|
self.canvas.clear()
|
2021-09-28 19:23:06 +00:00
|
|
|
with self.canvas:
|
|
|
|
for cross in self.crosses:
|
2021-10-20 15:05:18 +00:00
|
|
|
cross.draw()
|
2021-09-28 19:23:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
# respond to mouse down
|
|
|
|
def on_touch_down(self,touch):
|
2021-09-29 19:52:31 +00:00
|
|
|
# only respond to touch in area
|
|
|
|
if self.collide_point(*touch.pos):
|
|
|
|
# create new cross
|
|
|
|
if touch.button=="right":
|
|
|
|
if self.check_interaction_any(Point(touch.x/Cross.size,touch.y/Cross.size),None):
|
|
|
|
new=Cross(touch.x/Cross.size,touch.y/Cross.size)
|
|
|
|
with self.canvas:
|
|
|
|
new.draw()
|
|
|
|
# add to list
|
|
|
|
self.crosses.append(new)
|
|
|
|
|
|
|
|
# select cross
|
|
|
|
if touch.button=="left":
|
2021-10-20 15:05:18 +00:00
|
|
|
# unselect
|
|
|
|
if self.selected!=None:
|
|
|
|
self.selected.selected=False
|
|
|
|
|
2021-10-20 05:05:20 +00:00
|
|
|
# select cross under touch
|
2021-09-29 19:52:31 +00:00
|
|
|
self.selected=self.find_cross(Point(touch.x/Cross.size,touch.y/Cross.size))
|
2021-10-20 15:05:18 +00:00
|
|
|
if self.selected!=None:
|
|
|
|
self.selected.selected=True
|
2021-10-20 05:05:20 +00:00
|
|
|
self.draw()
|
2021-09-28 19:23:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
# respond to drag
|
|
|
|
def on_touch_move(self,touch):
|
2021-09-29 19:52:31 +00:00
|
|
|
if self.collide_point(*touch.pos):
|
|
|
|
# only move on left click
|
|
|
|
if touch.button=="left" and self.selected!=None:
|
|
|
|
self.selected.pos=self.check_move(Point(touch.x/Cross.size,touch.y/Cross.size),self.selected)
|
|
|
|
# redraw
|
|
|
|
self.draw()
|
2021-09-28 19:23:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
# find the cross at position pos
|
|
|
|
def find_cross(self,pos):
|
|
|
|
for cross in self.crosses:
|
2021-09-28 19:35:41 +00:00
|
|
|
if cross_distx(pos,cross.pos)<=0.5 or cross_disty(pos,cross.pos)<=0.5:
|
2021-09-28 19:23:06 +00:00
|
|
|
return cross
|
|
|
|
# none found
|
|
|
|
return None
|
|
|
|
|
2021-09-29 07:24:23 +00:00
|
|
|
# check whether a position intersects with any of the crosses
|
|
|
|
def check_interaction_any(self,pos,exception):
|
|
|
|
for other in self.crosses:
|
|
|
|
if other!=exception:
|
|
|
|
if other.check_interaction(pos)==False:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2021-09-28 19:23:06 +00:00
|
|
|
# check that a cross can move to new position
|
|
|
|
def check_move(self,newpos,cross):
|
2021-09-29 07:24:23 +00:00
|
|
|
# whether newpos is acceptable
|
|
|
|
accept_newpos=True
|
2021-09-28 19:23:06 +00:00
|
|
|
for other in self.crosses:
|
|
|
|
# do not compare a cross to itself
|
|
|
|
if other!=cross:
|
|
|
|
# move would make cross overlap with other
|
|
|
|
if other.check_interaction(newpos)==False:
|
2021-09-29 07:24:23 +00:00
|
|
|
accept_newpos=False
|
|
|
|
# check if cross touches other
|
|
|
|
if other.check_touch(cross.pos):
|
2021-09-29 03:36:07 +00:00
|
|
|
# move along other while remaining stuck
|
2021-09-29 07:24:23 +00:00
|
|
|
candidate=other.move_along(newpos,cross.pos)
|
2021-09-29 03:36:07 +00:00
|
|
|
else:
|
2021-09-29 07:24:23 +00:00
|
|
|
candidate=other.move_on_line_to_stick(cross.pos,newpos-cross.pos)
|
|
|
|
if self.check_interaction_any(candidate,cross):
|
|
|
|
return candidate
|
|
|
|
if accept_newpos:
|
|
|
|
return newpos
|
|
|
|
else:
|
|
|
|
# cannot move cross at all, try again
|
|
|
|
return self.check_move(candidate,cross)
|
|
|
|
|
2021-09-28 19:23:06 +00:00
|
|
|
|
2021-10-16 20:19:22 +00:00
|
|
|
# write configuration to file
|
|
|
|
def write(self,file):
|
|
|
|
ff=open(file,"w")
|
|
|
|
for cross in self.crosses:
|
|
|
|
ff.write("{:05.2f},{:05.2f};{:3.1f},{:3.1f},{:3.1f}\n".format(cross.pos.x,cross.pos.y,cross.color[0],cross.color[1],cross.color[2]))
|
|
|
|
ff.close()
|
|
|
|
|
|
|
|
# read configuration from file
|
|
|
|
def read(self,file):
|
2021-10-18 22:57:35 +00:00
|
|
|
self.reset()
|
|
|
|
try:
|
|
|
|
ff=open(file,"r")
|
|
|
|
except:
|
|
|
|
self.app.command_prompt.message="error: could not read file '"+file+"' (this should not happen and is probably a bug)"
|
|
|
|
return
|
2021-10-16 20:19:22 +00:00
|
|
|
|
|
|
|
# counter
|
|
|
|
i=0
|
|
|
|
|
2021-10-18 22:57:35 +00:00
|
|
|
try:
|
|
|
|
lines=ff.readlines()
|
|
|
|
except:
|
|
|
|
self.app.command_prompt.message="error: could not read the contents of file '"+file+"'"
|
|
|
|
return
|
|
|
|
|
|
|
|
for line in lines:
|
2021-10-16 20:19:22 +00:00
|
|
|
i+=1
|
|
|
|
|
|
|
|
# remove newline
|
|
|
|
line=line[:len(line)-1]
|
|
|
|
|
|
|
|
# ignore comments
|
|
|
|
if '#' in line:
|
|
|
|
line=line[:line.find('#')]
|
|
|
|
# ignore empty lines
|
|
|
|
if len(line)==0:
|
|
|
|
continue
|
|
|
|
|
|
|
|
entries=line.split(";")
|
|
|
|
# skip line if improperly formatted
|
|
|
|
if len(entries)>2:
|
|
|
|
print("warning: ignoring line "+str(i)+" in file '"+file+"': more than two ';' spearated entries in '"+line+"'",file=sys.stderr)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# position
|
|
|
|
pos_str=entries[0].split(",")
|
|
|
|
# skip line if improperly formatted
|
|
|
|
if len(pos_str)!=2:
|
|
|
|
print("warning: ignoring line "+str(i)+" in file '"+file+"': position '"+entries[0]+"' does not have two components",file=sys.stderr)
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
pos=Point(float(pos_str[0]),float(pos_str[1]))
|
|
|
|
except:
|
|
|
|
print("warning: ignoring line "+str(i)+" in file '"+file+"': position '"+entries[0]+"' cannot be read",file=sys.stderr)
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
# color
|
|
|
|
color=(0,0,1)
|
|
|
|
if len(entries)==2:
|
|
|
|
color_str=entries[1].split(",")
|
|
|
|
# skip line if improperly formatted
|
|
|
|
if len(color_str)!=3:
|
|
|
|
print("warning: ignoring line "+str(i)+" in file '"+file+"': color '"+entries[1]+"' does not have three components",file=sys.stderr)
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
color=(float(color_str[0]),float(color_str[1]),float(color_str[2]))
|
|
|
|
except:
|
|
|
|
print("warning: ignoring line "+str(i)+" in file '"+file+"': color '"+entries[1]+"' cannot be read",file=sys.stderr)
|
|
|
|
continue
|
|
|
|
|
|
|
|
if self.check_interaction_any(pos,None):
|
|
|
|
# add to list
|
|
|
|
self.crosses.append(Cross(pos.x,pos.y,color=color))
|
|
|
|
else:
|
|
|
|
print("warning: ignoring line "+str(i)+" in file '"+file+"': particle overlaps with existing particles",file=sys.stderr)
|
|
|
|
|
|
|
|
ff.close()
|
|
|
|
self.draw()
|
2021-09-28 19:23:06 +00:00
|
|
|
|