altos: Add bit-bang i2c driver
[fw/altos] / altoslib / AltosMapTransform.java
1 /*
2  * Copyright © 2014 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altoslib_14;
20
21 import java.io.*;
22 import java.lang.Math;
23 import java.util.*;
24 import java.util.concurrent.*;
25
26 public class AltosMapTransform {
27
28         double  scale_x, scale_y;
29
30         double  offset_x, offset_y;
31
32         int     width, height;
33
34         public AltosLatLon lat_lon (AltosPointDouble point) {
35                 double lat, lon;
36                 double rads;
37
38                 lon = point.x/scale_x;
39                 rads = 2 * Math.atan(Math.exp(-point.y/scale_y));
40                 lat = Math.toDegrees(rads - Math.PI/2);
41
42                 return new AltosLatLon(lat,lon);
43         }
44
45         public AltosLatLon lat_lon (AltosPointInt point) {
46                 return lat_lon(new AltosPointDouble(point.x, point.y));
47         }
48
49         public AltosPointDouble screen_point(AltosPointInt screen) {
50                 return new AltosPointDouble(screen.x + offset_x, screen.y + offset_y);
51         }
52
53         public AltosPointDouble screen_point(AltosPointDouble screen) {
54                 return new AltosPointDouble(screen.x + offset_x, screen.y + offset_y);
55         }
56
57         public double hypot(AltosLatLon a, AltosLatLon b) {
58                 AltosPointDouble        a_pt = point(a);
59                 AltosPointDouble        b_pt = point(b);
60
61                 return Math.hypot(a_pt.x - b_pt.x, a_pt.y - b_pt.y);
62         }
63
64         public AltosLatLon screen_lat_lon(AltosPointInt screen) {
65                 return lat_lon(screen_point(screen));
66         }
67
68         public AltosLatLon screen_lat_lon(AltosPointDouble screen) {
69                 return lat_lon(screen_point(screen));
70         }
71
72         public  AltosPointDouble point(double lat, double lon) {
73                 double x, y;
74                 double e;
75
76                 x = lon * scale_x;
77
78                 e = Math.sin(Math.toRadians(lat));
79                 e = Math.max(e,-(1-1.0E-15));
80                 e = Math.min(e,  1-1.0E-15 );
81
82                 y = 0.5*Math.log((1+e)/(1-e))*-scale_y;
83
84                 return new AltosPointDouble(x, y);
85         }
86
87         public AltosPointDouble point(AltosLatLon lat_lon) {
88                 return point(lat_lon.lat, lat_lon.lon);
89         }
90
91         public AltosPointDouble screen(AltosPointDouble point) {
92                 return new AltosPointDouble(point.x - offset_x, point.y - offset_y);
93         }
94
95         public AltosPointInt screen(AltosPointInt point) {
96                 return new AltosPointInt((int) (point.x - offset_x + 0.5),
97                                          (int) (point.y - offset_y + 0.5));
98         }
99
100         public AltosRectangle screen(AltosMapRectangle map_rect) {
101                 AltosPointDouble        ul = screen(map_rect.ul);
102                 AltosPointDouble        lr = screen(map_rect.lr);
103
104                 return new AltosRectangle((int) ul.x, (int) ul.y, (int) (lr.x - ul.x), (int) (lr.y - ul.y));
105         }
106
107         public AltosPointDouble screen(AltosLatLon lat_lon) {
108                 return screen(point(lat_lon));
109         }
110
111         public AltosPointDouble screen(double lat, double lon) {
112                 return screen(point(lat, lon));
113         }
114
115         /* Return first longitude value which ends up on-screen */
116         public double first_lon(double lon) {
117                 /* Find a longitude left of the screen */
118                 for (;;) {
119                         double x = lon * scale_x - offset_x;
120                         if (x < 0)
121                                 break;
122                         lon -= 360.0;
123                 }
124                 /* Find the first longitude on the screen */
125                 for (;;) {
126                         double x = lon * scale_x - offset_x;
127                         if (x >= 0)
128                                 break;
129                         lon += 360.0;
130                 }
131                 return lon;
132         }
133
134         /* Return last longitude value which ends up on-screen */
135         public double last_lon(double lon) {
136                 lon = first_lon(lon);
137
138                 for(;;) {
139                         double next_lon = lon + 360.0;
140                         double next_x = next_lon * scale_x - offset_x;
141                         if (next_x >= width)
142                                 break;
143                         lon = next_lon;
144                 }
145                 return lon;
146         }
147
148         private boolean has_location;
149
150         public boolean has_location() {
151                 return has_location;
152         }
153
154         public AltosMapTransform(int width, int height, int zoom, AltosLatLon centre_lat_lon) {
155                 scale_x = 256/360.0 * Math.pow(2, zoom);
156                 scale_y = 256/(2.0*Math.PI) * Math.pow(2, zoom);
157
158                 this.width = width;
159                 this.height = height;
160
161                 AltosPointDouble centre_pt = point(centre_lat_lon);
162
163                 has_location = (centre_lat_lon.lat != 0 || centre_lat_lon.lon != 0);
164                 offset_x = centre_pt.x - width / 2.0;
165                 offset_y = centre_pt.y - height / 2.0;
166         }
167
168         public static double lon_from_distance(double lat, double distance) {
169                 double c = AltosGreatCircle.earth_radius * Math.cos(lat * Math.PI / 180) * 2 * Math.PI;
170
171                 if (c < 10)
172                         return 0;
173                 return distance/c * 360.0;
174         }
175 }