Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/helpers.py
diff options
context:
space:
mode:
authorGary Martin <gary@garycmartin.com>2012-10-24 01:39:03 (GMT)
committer Gary Martin <gary@garycmartin.com>2012-10-24 01:39:03 (GMT)
commitef30a6108aef12544b06df1969842d0029addd42 (patch)
treec5234616d95f751f305e1df24ed9719fba462ea2 /helpers.py
parentb58fd82fc7c5b6aad5115f2ac4735a3e8d5671f9 (diff)
Some basic pylint tidy up, spaces after commas and around formulars, line wrapping etc.
Diffstat (limited to 'helpers.py')
-rw-r--r--helpers.py78
1 files changed, 40 insertions, 38 deletions
diff --git a/helpers.py b/helpers.py
index b0ac16e..a50c5c6 100644
--- a/helpers.py
+++ b/helpers.py
@@ -6,37 +6,39 @@
import math
# distance calculator, pt1 and pt2 are ordred pairs
def distance(pt1, pt2):
- return math.sqrt((pt1[0] - pt2[0]) ** 2 + (pt1[1] -pt2[1]) ** 2)
+ return math.sqrt((pt1[0] - pt2[0]) ** 2 + (pt1[1] - pt2[1]) ** 2)
-# returns the angle between the line segment from pt1 --> pt2 and the x axis, from -pi to pi
-def getAngle(pt1,pt2):
+# returns the angle between the line segment from
+# pt1 --> pt2 and the x axis, from -pi to pi
+def getAngle(pt1, pt2):
xcomp = pt2[0] - pt1[0]
ycomp = pt1[1] - pt2[1]
- return math.atan2(ycomp,xcomp)
+ return math.atan2(ycomp, xcomp)
-# returns a list of ordered pairs that describe an equilteral triangle around the segment from pt1 --> pt2
-def constructTriangleFromLine(p1,p2):
- halfHeightVector = (0.57735*(p2[1] - p1[1]), 0.57735*(p2[0] - p1[0]))
+# returns a list of ordered pairs that describe an
+# equilteral triangle around the segment from pt1 --> pt2
+def constructTriangleFromLine(p1, p2):
+ halfHeightVector = (0.57735 * (p2[1] - p1[1]), 0.57735 * (p2[0] - p1[0]))
p3 = (p1[0] + halfHeightVector[0], p1[1] - halfHeightVector[1])
p4 = (p1[0] - halfHeightVector[0], p1[1] + halfHeightVector[1])
- return [p2,p3,p4]
+ return [p2, p3, p4]
# returns the area of a polygon
def polyArea(vertices):
n = len(vertices)
A = 0
- p=n-1
- q=0
+ p = n - 1
+ q = 0
while q<n:
- A+=vertices[p][0]*vertices[q][1] - vertices[q][0]*vertices[p][1]
- p=q
+ A += vertices[p][0] * vertices[q][1] - vertices[q][0] * vertices[p][1]
+ p = q
q += 1
- return A/2.0
+ return A / 2.0
#Some polygon magic, thanks to John W. Ratcliff on www.flipcode.com
# returns true if pt is in triangle
-def insideTriangle(pt,triangle):
+def insideTriangle(pt, triangle):
ax = triangle[2][0] - triangle[1][0]
ay = triangle[2][1] - triangle[1][1]
@@ -44,19 +46,19 @@ def insideTriangle(pt,triangle):
by = triangle[0][1] - triangle[2][1]
cx = triangle[1][0] - triangle[0][0]
cy = triangle[1][1] - triangle[0][1]
- apx= pt[0] - triangle[0][0]
- apy= pt[1] - triangle[0][1]
- bpx= pt[0] - triangle[1][0]
- bpy= pt[1] - triangle[1][1]
- cpx= pt[0] - triangle[2][0]
- cpy= pt[1] - triangle[2][1]
+ apx = pt[0] - triangle[0][0]
+ apy = pt[1] - triangle[0][1]
+ bpx = pt[0] - triangle[1][0]
+ bpy = pt[1] - triangle[1][1]
+ cpx = pt[0] - triangle[2][0]
+ cpy = pt[1] - triangle[2][1]
- aCROSSbp = ax*bpy - ay*bpx
- cCROSSap = cx*apy - cy*apx
- bCROSScp = bx*cpy - by*cpx
+ aCROSSbp = ax * bpy - ay * bpx
+ cCROSSap = cx * apy - cy * apx
+ bCROSScp = bx * cpy - by * cpx
return aCROSSbp >= 0.0 and bCROSScp >= 0.0 and cCROSSap >= 0.0
-def polySnip(vertices,u,v,w,n):
+def polySnip(vertices, u, v, w, n):
EPSILON = 0.0000000001
Ax = vertices[u][0]
@@ -68,13 +70,13 @@ def polySnip(vertices,u,v,w,n):
Cx = vertices[w][0]
Cy = vertices[w][1]
- if EPSILON > (((Bx-Ax)*(Cy-Ay)) - ((By-Ay)*(Cx-Ax))): return False
+ if EPSILON > (((Bx - Ax) * (Cy - Ay)) - ((By - Ay) * (Cx - Ax))): return False
- for p in range(0,n):
+ for p in range(0, n):
if p == u or p == v or p == w: continue
Px = vertices[p][0];
Py = vertices[p][1];
- if insideTriangle((Px,Py),((Ax,Ay),(Bx,By),(Cx,Cy))): return False;
+ if insideTriangle((Px, Py), ((Ax, Ay), (Bx, By), (Cx, Cy))): return False;
return True;
@@ -92,32 +94,32 @@ def decomposePoly(vertices):
# remove nv-2 vertices, creating 1 triangle every time
nv = n
- count = 2*nv # error detection
- m=0
- v=nv-1
- while nv>2:
+ count = 2 * nv # error detection
+ m = 0
+ v = nv - 1
+ while nv > 2:
count -= 1
- if 0>= count:
+ if 0 >= count:
return [] # Error -- probably bad polygon
# three consecutive vertices
u = v
- if nv<=u: u = 0 # previous
+ if nv <= u: u = 0 # previous
v = u+1
- if nv<=v: v = 0 # new v
+ if nv <= v: v = 0 # new v
w = v+1
- if nv<=w: w = 0 # next
+ if nv <= w: w = 0 # next
- if(polySnip(vertices,u,v,w,nv)):
+ if(polySnip(vertices, u, v, w, nv)):
# record this triangle
- result.append((vertices[u],vertices[v],vertices[w]))
+ result.append((vertices[u], vertices[v], vertices[w]))
m+=1
# remove v from remaining polygon
vertices.pop(v)
nv -= 1
# reset error detection
- count = 2*nv
+ count = 2 * nv
return result