source: inundation/ga/storm_surge/pyvolution/util.py @ 262

Last change on this file since 262 was 258, checked in by ole, 20 years ago

Added C-headers to share code
Removed length and distance from util

File size: 1.4 KB
Line 
1
2
3def angle(v):
4    """Compute angle between e1 (the unit vector in the x-direction)
5    and the specified vector
6    """
7
8    from math import acos, pi, sqrt
9    from Numeric import sum, array
10
11    l = sqrt( sum (array(v)**2))
12    v1 = v[0]/l
13    v2 = v[1]/l
14       
15    theta = acos(v1)
16
17    if v2 < 0:
18        #Quadrant 3 or 4
19        theta = 2*pi-theta
20
21    return theta
22
23
24def anglediff(v0, v1):
25    """Compute difference between angle of vector x0, y0 and x1, y1.
26    This is used for determining the ordering of vertices,
27    e.g. for checking if they are counter clockwise.
28   
29    Always return a positive value
30    """
31       
32    from math import pi
33   
34    a0 = angle(v0)
35    a1 = angle(v1)
36
37    #Ensure that difference will be positive
38    if a0 < a1:
39        a0 += 2*pi
40           
41    return a0-a1
42
43
44
45####################################################################
46#Python versions of function that are also implemented in util_gateway.c
47#
48
49def gradient_python(x0, y0, x1, y1, x2, y2, q0, q1, q2):
50    """
51    """
52   
53    det = (y2-y0)*(x1-x0) - (y1-y0)*(x2-x0)           
54    a = (y2-y0)*(q1-q0) - (y1-y0)*(q2-q0)
55    a /= det
56
57    b = (x1-x0)*(q2-q0) - (x2-x0)*(q1-q0)
58    b /= det           
59
60    return a, b
61
62
63
64
65
66##############################################
67#Initialise module
68
69import compile
70if compile.can_use_C_extension('util_ext.c'):
71    from util_ext import gradient
72else:
73    gradient = gradient_python
74
75
76if __name__ == "__main__":
77    pass
78
Note: See TracBrowser for help on using the repository browser.