From ee2aa6ae2beda3d4182470a3d6796d64e9bd791d Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Fri, 17 Jan 2014 23:10:47 +0000 Subject: merge ignacio's gtk3 branch --- (limited to 'helpers.py') diff --git a/helpers.py b/helpers.py index 1d0d2fe..65b07cd 100644 --- a/helpers.py +++ b/helpers.py @@ -23,11 +23,13 @@ #================================================================== import math + def distance(pt1, pt2): """Distance calculator, pt1 and pt2 are ordred pairs. """ return math.sqrt((pt1[0] - pt2[0]) ** 2 + (pt1[1] - pt2[1]) ** 2) + def getAngle(pt1, pt2): """Returns angle between line segment pt1 -> pt2 and x axis, from -pi to pi. """ @@ -35,14 +37,18 @@ def getAngle(pt1, pt2): ycomp = pt1[1] - pt2[1] return math.atan2(ycomp, xcomp) + def constructTriangleFromLine(p1, p2): - """Returns list of ordered pairs describing equilteral triangle around segment pt1 --> pt2. + """ + Returns list of ordered pairs describing equilteral triangle around + segment pt1 --> pt2. """ 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] + def polyArea(vertices): """Returns the area of a polygon. """ @@ -56,10 +62,10 @@ def polyArea(vertices): q += 1 return A / 2.0 - + 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[2][0] - triangle[1][0] @@ -77,9 +83,10 @@ def insideTriangle(pt, triangle): aCROSSbp = ax * bpy - ay * bpx cCROSSap = cx * apy - cy * apx - bCROSScp = bx * cpy - by * cpx + bCROSScp = bx * cpy - by * cpx return aCROSSbp >= 0.0 and bCROSScp >= 0.0 and cCROSSap >= 0.0 + def polySnip(vertices, u, v, w, n): EPSILON = 0.0000000001 @@ -92,7 +99,7 @@ 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))): + if EPSILON > (((Bx - Ax) * (Cy - Ay)) - ((By - Ay) * (Cx - Ax))): return False for p in range(0, n): @@ -112,7 +119,8 @@ def decomposePoly(vertices): vertices = list(vertices) n = len(vertices) result = [] - if(n < 3): return [] # not a poly! + if(n < 3): + return [] # not a poly! # Force counter-clockwise polygon if 0 >= polyArea(vertices): @@ -120,20 +128,23 @@ def decomposePoly(vertices): # Remove nv-2 vertices, creating 1 triangle every time nv = n - count = 2 * nv # error detection + count = 2 * nv # error detection v = nv - 1 while nv > 2: count -= 1 if 0 >= count: - return [] # Error -- probably bad polygon + return [] # Error -- probably bad polygon # Three consecutive vertices - u = v - if nv <= u: u = 0 # previous + u = v + 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)): @@ -147,6 +158,7 @@ def decomposePoly(vertices): count = 2 * nv return result + def tuple_to_int(tuple_input): """Cast tuple values to ints to avoid gtk+ and pygame's dislike of floats. """ -- cgit v0.9.1