-
Notifications
You must be signed in to change notification settings - Fork 5
/
generate-earth-tables.py
executable file
·65 lines (57 loc) · 1.52 KB
/
generate-earth-tables.py
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
#! /usr/bin/python
#
# Author: Andrew Zaborowski <[email protected]>
#
# Licensed under the BSD license.
import math
import Image
striplen = 15
stripp0 = ( -32, 48 )
stripp1 = ( -51, 282 )
dist = []
for num in xrange(0, striplen):
x = stripp0[0] + (stripp1[0] - stripp0[0]) * num * 1.0 / (striplen - 1)
y = stripp0[1] + (stripp1[1] - stripp0[1]) * num * 1.0 / (striplen - 1)
dist.append(math.hypot(x, y))
mdist = max(dist)
print('#include <avr/pgmspace.h>')
print('const uint8_t a_d_to_lon_data[] PROGMEM = {')
for d in dist:
r = d / mdist
for a in xrange(1, 128 + 1):
# Cover one quarter only
angle = math.pi * a / 256
x = math.sin(angle) * r
y = math.cos(angle) * r
lon = math.asin(x) / math.cos(math.asin(y))
print('\t' + str(int(lon / (math.pi / 2) * 64 + 0.49)) + ',')
print('};')
print('const uint8_t a_d_to_y_data[] PROGMEM = {')
for d in dist:
r = d / mdist
for a in xrange(0, 128):
# Cover one quarter only
angle = math.pi * a / 256
y = math.cos(angle) * r
print('\t' + str(int(y * 32 + 0.49)) + ',')
print('};')
img = Image.open('earth-lands.jpg')
lands = img.load()
print('const uint8_t y_lon_to_land[] PROGMEM = {')
bit = 0
for y in xrange(0, 65):
sin = (y - 32.0) / 32.0
lat = math.asin(sin)
for lon in xrange(0, 256):
imgy = ((-lat / math.pi) + 0.5) * (img.size[1] - 1)
imgx = lon / 255.0 * (img.size[0] - 1)
# TODO filtering
if bit == 0:
byte = 0
if lands[imgx, imgy] < 100:
byte |= 1 << bit
bit += 1
if bit == 8:
bit = 0;
print('\t' + str(byte) + ',')
print('};')