def angle(v): """Compute angle between e1 (the unit vector in the x-direction) and the specified vector """ from math import acos, pi, sqrt from Numeric import sum, array l = sqrt( sum (array(v)**2)) v1 = v[0]/l v2 = v[1]/l theta = acos(v1) if v2 < 0: #Quadrant 3 or 4 theta = 2*pi-theta return theta def anglediff(v0, v1): """Compute difference between angle of vector x0, y0 and x1, y1. This is used for determining the ordering of vertices, e.g. for checking if they are counter clockwise. Always return a positive value """ from math import pi a0 = angle(v0) a1 = angle(v1) #Ensure that difference will be positive if a0 < a1: a0 += 2*pi return a0-a1 def mean(x): from Numeric import sum return sum(x)/len(x) #################################################################### #Python versions of function that are also implemented in util_gateway.c # def gradient_python(x0, y0, x1, y1, x2, y2, q0, q1, q2): """ """ det = (y2-y0)*(x1-x0) - (y1-y0)*(x2-x0) a = (y2-y0)*(q1-q0) - (y1-y0)*(q2-q0) a /= det b = (x1-x0)*(q2-q0) - (x2-x0)*(q1-q0) b /= det return a, b ############################################## #Initialise module import compile if compile.can_use_C_extension('util_ext.c'): from util_ext import gradient else: gradient = gradient_python if __name__ == "__main__": pass