1 """Calculations routines"""
2
3 import math
4
5 __author__ = "P. Tute"
6 __maintainer__ = "B. Henne"
7 __contact__ = "henne@dcsec.uni-hannover.de"
8 __copyright__ = "(c) 2011, DCSec, Leibniz Universitaet Hannover, Germany"
9 __license__ = "GPLv3"
10
11
12 TILE_SIZE = 256
13
15 """Calculates the OpenStreetMap-tile number from given latitude, longitude and zoom.
16 @author: P. Tute"""
17 lat_rad = math.radians(lat_deg)
18 n = 2.0 ** zoom
19 xtile = (lon_deg + 180.0) / 360.0 * n
20 ytile = (1.0 - math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 * n
21 return (xtile, ytile)
22
24 """Calculates x any y coordinates for drawing based on current center-tile and zoom.
25 @author: P. Tute"""
26 x, y = lat_lon_to_tilenr(lat, lon, zoom)
27 if not (0, 0) in player.tiles_used:
28 return 0, 0
29 or_x, or_y = player.tiles_used[(0, 0)][:2]
30 local_x = int(x) - int(or_x)
31 local_y = int(or_y) - int(y)
32 x = local_x * TILE_SIZE + TILE_SIZE * (x - int(x)) - TILE_SIZE/2
33 y = local_y * TILE_SIZE - TILE_SIZE * (y - int(y)) + TILE_SIZE/2
34 return int(x), int(y)
35
37 """Calculates coordinates for drawing a circle using Bresenham's circle algorithm.
38
39 Returns a list of coordinates.
40
41 The algorithm is slightly modified to allow drawing transparent circles.
42 http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
43 @author: P. Tute"""
44 coords = []
45 x0, y0 = x, y
46 f = 1 - rad;
47 ddF_x = 0;
48 ddF_y = -2 * rad;
49 x = 0;
50 y = rad;
51 coord_mods = [x, y]
52
53 while x < y:
54 if f >= 0:
55 y -= 1
56 ddF_y += 2
57 f += ddF_y
58 x += 1
59 ddF_x += 2
60 f += ddF_x + 1
61 coord_mods.append(x)
62 coord_mods.append(y)
63
64 coord_mods_reversed = coord_mods[:]
65 coord_mods_reversed.reverse()
66
67
68 for i, mod in enumerate(coord_mods):
69 if not i % 2:
70
71 coords.append(x0 + mod)
72 else:
73 coords.append(y0 + mod)
74
75
76 for i, mod in enumerate(coord_mods_reversed):
77 if not i % 2:
78
79 coords.append(x0 + mod)
80 else:
81 coords.append(y0 + mod)
82
83
84
85
86
87
88 temp_coords = []
89 for i, mod in enumerate(coord_mods_reversed):
90 if not i % 2:
91
92 temp_coords.append(x0 + mod)
93 else:
94 temp_coords.append(y0 - mod)
95 temp_coords.reverse()
96 for i in xrange(0, len(temp_coords), 2):
97 coords.append(temp_coords[i+1])
98 coords.append(temp_coords[i])
99
100
101 temp_coords = []
102 for i, mod in enumerate(coord_mods):
103 if not i % 2:
104
105 temp_coords.append(x0 + mod)
106 else:
107 temp_coords.append(y0 - mod)
108 temp_coords.reverse()
109 for i in xrange(0, len(temp_coords), 2):
110 coords.append(temp_coords[i+1])
111 coords.append(temp_coords[i])
112
113
114 for i, mod in enumerate(coord_mods):
115 if not i % 2:
116
117 coords.append(x0 - mod)
118 else:
119 coords.append(y0 - mod)
120
121
122 for i, mod in enumerate(coord_mods_reversed):
123 if not i % 2:
124
125 coords.append(x0 - mod)
126 else:
127 coords.append(y0 - mod)
128
129
130 temp_coords = []
131 for i, mod in enumerate(coord_mods_reversed):
132 if not i % 2:
133
134 temp_coords.append(x0 - mod)
135 else:
136 temp_coords.append(y0 + mod)
137 temp_coords.reverse()
138 for i in xrange(0, len(temp_coords), 2):
139 coords.append(temp_coords[i+1])
140 coords.append(temp_coords[i])
141
142
143 for i, mod in enumerate(coord_mods):
144 if not i % 2:
145
146 coords.append(x0 - mod)
147 else:
148 coords.append(y0 + mod)
149
150 return coords
151