-
Notifications
You must be signed in to change notification settings - Fork 0
/
c1.pyx
81 lines (63 loc) · 2.18 KB
/
c1.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#==================================================
# actually python code
# import math
# def great_circle(lon1,lat1,lon2,lat2):
# radius = 3956 #miles
# x = math.pi/180.0
# a = (90.0-lat1)*(x)
# b = (90.0-lat2)*(x)
# theta = (lon2-lon1)*(x)
# c = math.acos((math.cos(a)*math.cos(b)) +
# (math.sin(a)*math.sin(b)*math.cos(theta)))
# return radius*c
#==================================================
# with python math package
# import math
# def great_circle(float lon1,float lat1,float lon2,float lat2):
# cdef float radius = 3956.0
# cdef float pi = 3.14159265
# cdef float x = pi/180.0
# cdef float a,b,theta,c
# a = (90.0-lat1)*x
# b = (90.0-lat2)*x
# theta = (lon2-lon1)*x
# c = math.acos((math.cos(a)*math.cos(b)) + (math.sin(a)*math.sin(b)*math.cos(theta)))
# return radius*c
#====================================================
# with c library
# cdef extern from "math.h":
# float cosf(float theta)
# float sinf(float theta)
# float acosf(float theta)
# def great_circle(float lon1,float lat1,float lon2,float lat2):
# cdef float radius = 3956.0
# cdef float pi = 3.14159265
# cdef float x = pi/180.0
# cdef float a,b,theta,c
# a = (90.0-lat1)*(x)
# b = (90.0-lat2)*(x)
# theta = (lon2-lon1)*(x)
# c = acosf((cosf(a)*cosf(b)) + (sinf(a)*sinf(b)*cosf(theta)))
# return radius*c
#=====================================================
# use the pure c function
cdef extern from "math.h":
float cosf(float theta)
float sinf(float theta)
float acosf(float theta)
cdef float circle(float lon1,float lat1,float lon2,float lat2):
cdef float radius = 3956.0
cdef float pi = 3.14159265
cdef float x = pi/180.0
cdef float a,b,theta,c
a = (90.0-lat1)*(x)
b = (90.0-lat2)*(x)
theta = (lon2-lon1)*(x)
c = acosf((cosf(a)*cosf(b)) + (sinf(a)*sinf(b)*cosf(theta)))
return radius*c
def great_circle(float lon1,float lat1,float lon2,float lat2,int num):
cdef int i
cdef float x
for i from 0 <= i < num: #do not use xrange function
x = circle(lon1,lat1,lon2,lat2) #I can't print the result, why?
return x