.
This commit is contained in:
27
extensions/fablabchemnitz/strip_line/geometry/Circle.py
Normal file
27
extensions/fablabchemnitz/strip_line/geometry/Circle.py
Normal file
@ -0,0 +1,27 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import math
|
||||
import inkex
|
||||
from lxml import etree
|
||||
|
||||
class Circle():
|
||||
def __init__(self,_c,_r):
|
||||
self.radius=_r
|
||||
self.center=_c
|
||||
|
||||
def __str__(self):
|
||||
return "Circle: center:"+str(self.center)+" radius:"+str(self.radius)+"\n"
|
||||
|
||||
def __repr__(self):
|
||||
return "Circle: center"+str(self.center)+" radius:"+str(self.radius)+"\n"
|
||||
|
||||
def isHit(p):
|
||||
distance=(center-p).length()
|
||||
if(distance<radius):
|
||||
return True
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
def toSVGObject(cls,x,y,r,color,strokewidth):
|
||||
att={"cx":str(x),"cy":str(y),"r":str(r),"fill":color,"stroke-width":str(strokewidth)}
|
||||
return etree.Element("circle",att)
|
29
extensions/fablabchemnitz/strip_line/geometry/Minus.py
Normal file
29
extensions/fablabchemnitz/strip_line/geometry/Minus.py
Normal file
@ -0,0 +1,29 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import math
|
||||
import inkex
|
||||
from lxml import etree
|
||||
|
||||
class Minus():
|
||||
def __init__(self,_x,_y,_r):
|
||||
self.x=_x
|
||||
self.y=_y
|
||||
self.r=_r
|
||||
|
||||
def isHit(p):
|
||||
distance=(center-p).length()
|
||||
if(distance<radius):
|
||||
return True
|
||||
return False
|
||||
|
||||
def appendToSVG(self,color,svg):
|
||||
attbackcicle={"cx":str(self.x),"cy":str(self.y),"r":str(self.r),"fill":"white",
|
||||
"stroke-width":"0"}
|
||||
attcicle={"cx":str(self.x),"cy":str(self.y),"r":str(self.r),"fill":color,"fill-opacity":"0.6",
|
||||
"stroke-width":str(max(1,self.r/4)),"stroke":color}
|
||||
|
||||
atthline={"x1":str(self.x-self.r),"y1":str(self.y),
|
||||
"x2":str(self.x+self.r),"y2":str(self.y),"stroke":color}
|
||||
svg.append(etree.Element("circle",attbackcicle))
|
||||
svg.append(etree.Element("circle",attcicle))
|
||||
svg.append(etree.Element("line",atthline))
|
34
extensions/fablabchemnitz/strip_line/geometry/Plus.py
Normal file
34
extensions/fablabchemnitz/strip_line/geometry/Plus.py
Normal file
@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import math
|
||||
import inkex
|
||||
from lxml import etree
|
||||
|
||||
class Plus():
|
||||
def __init__(self,_x,_y,_r):
|
||||
self.x=_x
|
||||
self.y=_y
|
||||
self.r=_r
|
||||
|
||||
def isHit(p):
|
||||
distance=(center-p).length()
|
||||
if(distance<radius):
|
||||
return True
|
||||
return False
|
||||
|
||||
def appendToSVG(self,color,svg):
|
||||
attbackcicle={"cx":str(self.x),"cy":str(self.y),"r":str(self.r),"fill":"white",
|
||||
"stroke-width":"0"}
|
||||
attcicle={"cx":str(self.x),"cy":str(self.y),"r":str(self.r),"fill":color,"fill-opacity":"0.6",
|
||||
"stroke-width":str(max(1,self.r/4)),"stroke":color}
|
||||
#horizontal line
|
||||
atthline={"x1":str(self.x-self.r),"y1":str(self.y),
|
||||
"x2":str(self.x+self.r),"y2":str(self.y),"stroke":color}
|
||||
#Vertical line
|
||||
attvline={"x1":str(self.x),"y1":str(self.y-self.r),
|
||||
"x2":str(self.x),"y2":str(self.y+self.r),"stroke":color}
|
||||
#Vertical line
|
||||
svg.append(etree.Element("circle",attbackcicle))
|
||||
svg.append(etree.Element("circle",attcicle))
|
||||
svg.append(etree.Element("line",atthline))
|
||||
svg.append(etree.Element("line",attvline))
|
99
extensions/fablabchemnitz/strip_line/geometry/Triangle.py
Normal file
99
extensions/fablabchemnitz/strip_line/geometry/Triangle.py
Normal file
@ -0,0 +1,99 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
class Triangle():
|
||||
#given segment
|
||||
def __init__(self,_a,_b,_c):
|
||||
self.a=_a
|
||||
self.b=_b
|
||||
self.c=_c
|
||||
|
||||
def __repr__(self):
|
||||
return "Triangle:("+str(self.a)+","+str(self.b)+","+str(self.c)+")"
|
||||
# Center of gravity centered on #a
|
||||
# @return u how much to b
|
||||
# @return v how much in the c direction
|
||||
|
||||
def barycentric(self,p):
|
||||
#How to use barycentric coordinates
|
||||
#http://www.blackpawn.com/texts/pointinpoly/default.html
|
||||
ca = self.c-self.a
|
||||
ba = self.b-self.a
|
||||
pa = p-self.a
|
||||
|
||||
#Calculate dot product
|
||||
dotca = ca.dot(ca)
|
||||
dotcba = ca.dot(ba)
|
||||
dotcpa = ca.dot(pa)
|
||||
dotba = ba.dot(ba)
|
||||
dotbpa = ba.dot(pa)
|
||||
|
||||
#Calculation of barycentric coordinates
|
||||
invDenom = 1 / (dotca * dotba - dotcba * dotcba);
|
||||
|
||||
u = (dotba * dotcpa-dotcba*dotbpa)*invDenom
|
||||
v = (dotca * dotbpa - dotcba * dotcpa) * invDenom
|
||||
return u,v
|
||||
|
||||
# Whether there is a point inside the triangle
|
||||
def isContain(self,p):
|
||||
u,v=self.barycentric(p)
|
||||
inkex.errormsg("\n u="+str(u)+" v="+str(v))
|
||||
If #u=v=0, the same point as the triangle point
|
||||
#Check if there is a point in the triangle
|
||||
return (u > 0) and (v > 0) and (u + v < 1)
|
||||
|
||||
def toSVG(self):
|
||||
return str(self.a.x)+","+str(self.a.y)+","+str(self.b.x)+","+str(self.b.y)+","+str(self.c.x)+","+str(self.c.y)
|
||||
# Is the arrangement of vertices a, b, c clockwise?
|
||||
|
||||
def isClockWise(self):
|
||||
circle=self.circumcircle()
|
||||
center=circle.center
|
||||
aTob=self.b-self.a
|
||||
aToc=self.c-self.a
|
||||
#Let's find the cross product
|
||||
cross=aTob.cross(aToc)
|
||||
|
||||
inkex.errormsg(" cross"+str(cross))
|
||||
if(cross>0):
|
||||
return True
|
||||
return False#Counterclockwise
|
||||
|
||||
def circumcircle(self):
|
||||
#Find the length of each side
|
||||
ab=(self.a-self.b).length()
|
||||
bc=(self.b-self.c).length()
|
||||
ca=(self.c-self.a).length()
|
||||
s=(ab+bc+ca)/2.0;
|
||||
area=math.sqrt(s*(s-ab)*(s-bc)*(s-ca));
|
||||
maxlength=0
|
||||
if(maxlength<ab):
|
||||
maxlength=ab;longestEdge=2
|
||||
if(maxlength<bc):
|
||||
maxlength=bc;longestEdge=0;
|
||||
if(maxlength<ca):
|
||||
maxlength=ca;longestEdge=1
|
||||
|
||||
if longestEdge==0:
|
||||
ha=2*area/bc;
|
||||
angleB=math.asin(ha/ab);
|
||||
angleC=math.asin(ha/ca);
|
||||
angleA=math.pi-angleB-angleC;
|
||||
if longestEdge== 1:
|
||||
hb=2*area/ca;
|
||||
angleA=math.asin(hb/ab);
|
||||
angleC=math.asin(hb/bc);
|
||||
angleB=math.pi-angleC-angleA;
|
||||
if longestEdge== 2:
|
||||
hc=2*area/ab;
|
||||
angleB=math.asin(hc/bc);
|
||||
angleA=math.asin(hc/ca);
|
||||
angleC=math.pi-angleA-angleB;
|
||||
|
||||
A = math.sin(2.0*angleA);
|
||||
B = math.sin(2.0*angleB);
|
||||
C = math.sin(2.0*angleC);
|
||||
center=Vertex((self.a.x * A + self.b.x * B + self.c.x * C) / (A+B+C),
|
||||
(self.a.y * A + self.b.y * B + self.c.y * C) / (A+B+C));
|
||||
rad=bc / math.sin(angleA) / 2.0
|
||||
return Circle(center,rad);
|
52
extensions/fablabchemnitz/strip_line/geometry/Vertex.py
Normal file
52
extensions/fablabchemnitz/strip_line/geometry/Vertex.py
Normal file
@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import math
|
||||
|
||||
class Vertex():
|
||||
post=None
|
||||
pre=None
|
||||
def __init__(self,_x,_y):
|
||||
self.x=_x
|
||||
self.y=_y
|
||||
|
||||
def __sub__(self,r):
|
||||
return Vertex(self.x-r.x,self.y-r.y)
|
||||
|
||||
def __str__(self):
|
||||
return "("+str(self.x)+","+str(self.y)+")"
|
||||
|
||||
def __repr__(self):
|
||||
return "("+str(self.x)+","+str(self.y)+")"
|
||||
|
||||
def __eq__(self,p):
|
||||
return (self.x==p.x) and (self.y==p.y)
|
||||
|
||||
def __add__(self,p):
|
||||
return Vertex(self.x+p.x,self.y+p.y)
|
||||
|
||||
#inner product
|
||||
def dot(self,p):
|
||||
return self.x*p.x+self.y*p.y
|
||||
|
||||
def cross(self,p):
|
||||
return self.x*p.y-self.y*p.x
|
||||
|
||||
def length(self):
|
||||
return math.hypot(self.x,self.y)
|
||||
|
||||
def rotate(self,radian):
|
||||
originalX=self.x
|
||||
self.x=self.x*math.cos(radian)-self.y*math.sin(radian)
|
||||
self.y=originalX*math.sin(radian)+self.y*math.cos(radian)
|
||||
|
||||
def set(self,_x,_y):
|
||||
self.x=_x
|
||||
self.y=_y
|
||||
|
||||
def set(self,p):
|
||||
self.x=p.x
|
||||
self.y=p.y
|
||||
|
||||
#SVG for output
|
||||
def toSVG(self):
|
||||
return str(self.x)+","+str(self.y)
|
Reference in New Issue
Block a user