Package viewer :: Package lib :: Module calculations
[hide private]
[frames] | no frames]

Source Code for Module viewer.lib.calculations

  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   
14 -def lat_lon_to_tilenr(lat_deg, lon_deg, zoom):
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
23 -def latlon_to_xy(lat, lon, zoom, player):
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
36 -def bresenham_circle(x, y, rad):
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 # 0-1:30 eighth of the circle 68 for i, mod in enumerate(coord_mods): 69 if not i % 2: 70 # x-value 71 coords.append(x0 + mod) 72 else: 73 coords.append(y0 + mod) 74 75 # 1:30-3 eighth of the circle 76 for i, mod in enumerate(coord_mods_reversed): 77 if not i % 2: 78 # x-value 79 coords.append(x0 + mod) 80 else: 81 coords.append(y0 + mod) 82 83 # 3-4:30 eighth of the circle 84 # temp coords is used so that the coordinates for this part of the 85 # circle can be reversed. This is necessary for drawing transparent circles. 86 # Without doing this, there would be jumps in drawing the polygon which would 87 # result in uglyness. 88 temp_coords = [] 89 for i, mod in enumerate(coord_mods_reversed): 90 if not i % 2: 91 # x-value 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 # 4:30-6 eighth of the circle 101 temp_coords = [] 102 for i, mod in enumerate(coord_mods): 103 if not i % 2: 104 # x-value 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 # 6-7:30 eighth of the circle 114 for i, mod in enumerate(coord_mods): 115 if not i % 2: 116 # x-value 117 coords.append(x0 - mod) 118 else: 119 coords.append(y0 - mod) 120 121 # 7:30-9 eighth of the circle 122 for i, mod in enumerate(coord_mods_reversed): 123 if not i % 2: 124 # x-value 125 coords.append(x0 - mod) 126 else: 127 coords.append(y0 - mod) 128 129 # 9-10:30 eighth of the circle 130 temp_coords = [] 131 for i, mod in enumerate(coord_mods_reversed): 132 if not i % 2: 133 # x-value 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 # 10:30-12 eighth of the circle 143 for i, mod in enumerate(coord_mods): 144 if not i % 2: 145 # x-value 146 coords.append(x0 - mod) 147 else: 148 coords.append(y0 + mod) 149 150 return coords
151