Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
 summaryrefslogtreecommitdiffstats log msg author committer range
path: root/helpers.py
diff options
 context: 12345678910152025303540 space: includeignore mode: unifiedssdiff
author committer Gary Martin 2010-10-08 15:25:04 (GMT) Gary Martin 2010-10-08 15:25:04 (GMT) 2161743d9147092cfaaa6b4faf275325e00e5c4f (patch) eba2c3cba2ee418323c8a296c14e19c84bc6cf98 /helpers.py 4dfc1de9c92a6607f137dc7f7a1333d155d88eb2 (diff)
Quick pass through pylint.
Diffstat (limited to 'helpers.py')
-rw-r--r--helpers.py74
1 files changed, 41 insertions, 33 deletions
 diff --git a/helpers.py b/helpers.pyindex b0c5c84..335852c 100644--- a/helpers.py+++ b/helpers.py@@ -22,52 +22,58 @@ # By Alex Levenson #================================================================== import math-# distance calculator, pt1 and pt2 are ordred pairs+ def distance(pt1, pt2):- return math.sqrt((pt1 - pt2) ** 2 + (pt1 - pt2) ** 2)+ """Distance calculator, pt1 and pt2 are ordred pairs.+ """+ return math.sqrt((pt1 - pt2) ** 2 + (pt1 - pt2) ** 2) -# returns the angle between the line segment from pt1 --> pt2 and the x axis, from -pi to pi def getAngle(pt1, pt2):+ """Returns angle between line segment pt1 -> pt2 and x axis, from -pi to pi.+ """ xcomp = pt2 - pt1 ycomp = pt1 - pt2 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):+def constructTriangleFromLine(p1, p2):+ """Returns list of ordered pairs describing equilteral triangle around segment pt1 --> pt2.+ """ halfHeightVector = (0.57735 * (p2 - p1), 0.57735 * (p2 - p1)) p3 = (p1 + halfHeightVector, p1 - halfHeightVector) p4 = (p1 - halfHeightVector, p1 + halfHeightVector) return [p2, p3, p4] -# returns the area of a polygon def polyArea(vertices):+ """Returns the area of a polygon.+ """ n = len(vertices) A = 0- p=n - 1- q=0+ p = n - 1+ q = 0 while q < n:- A+=vertices[p] * vertices[q] - vertices[q] * vertices[p]- p=q+ A += vertices[p] * vertices[q] - vertices[q] * vertices[p]+ p = q q += 1 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):-+ """Returns true if pt is in triangle.+ + Some polygon magic, thanks to John W. Ratcliff on www.flipcode.com+ """ ax = triangle - triangle ay = triangle - triangle bx = triangle - triangle by = triangle - triangle cx = triangle - triangle cy = triangle - triangle- apx= pt - triangle- apy= pt - triangle- bpx= pt - triangle- bpy= pt - triangle- cpx= pt - triangle- cpy= pt - triangle+ apx = pt - triangle+ apy = pt - triangle+ bpx = pt - triangle+ bpy = pt - triangle+ cpx = pt - triangle+ cpy = pt - triangle aCROSSbp = ax * bpy - ay * bpx cCROSSap = cx * apy - cy * apx@@ -86,39 +92,42 @@ def polySnip(vertices, u, v, w, n): Cx = vertices[w] Cy = vertices[w] - 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):- if p == u or p == v or p == w: continue+ if p == u or p == v or p == w:+ continue Px = vertices[p] Py = vertices[p]- 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 -# decomposes a polygon into its triangles def decomposePoly(vertices):+ """Decomposes a polygon into its triangles.+ """ vertices = list(vertices) n = len(vertices) result = [] if(n < 3): return [] # not a poly! - # force a counter-clockwise polygon+ # Force counter-clockwise polygon if 0 >= polyArea(vertices): vertices.reverse() - # remove nv-2 vertices, creating 1 triangle every time+ # 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 -= 1 if 0 >= count: return [] # Error -- probably bad polygon - # three consecutive vertices+ # Three consecutive vertices u = v if nv <= u: u = 0 # previous v = u + 1@@ -128,18 +137,17 @@ def decomposePoly(vertices): if(polySnip(vertices, u, v, w, nv)): - # record this triangle+ # Record this triangle result.append((vertices[u], vertices[v], vertices[w])) - m += 1- # remove v from remaining polygon+ # Remove v from remaining polygon vertices.pop(v) nv -= 1- # reset error detection+ # Reset error detection count = 2 * nv return result -def cast_tuple_to_int(tuple):+def cast_tuple_to_int(tuple_input): """Cast tuple values to ints to avoid gtk+ and pygame's dislike of floats. """- return [int(i) for i in tuple]+ return [int(i) for i in tuple_input]