altoslib: Add getBytes/putBytes interface to AltosPreferencesBackend
[fw/altos] / altosuilib / AltosUIMapTransform.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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altosuilib_3;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import java.io.*;
24 import java.lang.Math;
25 import java.awt.geom.*;
26 import java.util.*;
27 import java.util.concurrent.*;
28 import org.altusmetrum.altoslib_5.*;
29
30 public class AltosUIMapTransform {
31
32         double  scale_x, scale_y;
33
34         double  offset_x, offset_y;
35
36         public AltosUILatLon lat_lon (Point2D.Double point) {
37                 double lat, lon;
38                 double rads;
39
40                 lon = point.x/scale_x;
41                 rads = 2 * Math.atan(Math.exp(-point.y/scale_y));
42                 lat = Math.toDegrees(rads - Math.PI/2);
43
44                 return new AltosUILatLon(lat,lon);
45         }
46
47         public Point2D.Double screen_point(Point screen) {
48                 return new Point2D.Double(screen.x + offset_x, screen.y + offset_y);
49         }
50
51         public AltosUILatLon screen_lat_lon(Point screen) {
52                 return lat_lon(screen_point(screen));
53         }
54
55         public Point2D.Double point(AltosUILatLon lat_lon) {
56                 double x, y;
57                 double e;
58
59                 x = lat_lon.lon * scale_x;
60
61                 e = Math.sin(Math.toRadians(lat_lon.lat));
62                 e = Math.max(e,-(1-1.0E-15));
63                 e = Math.min(e,  1-1.0E-15 );
64
65                 y = 0.5*Math.log((1+e)/(1-e))*-scale_y;
66
67                 return new Point2D.Double(x, y);
68         }
69
70         public Point2D.Double screen(Point2D.Double point) {
71                 return new Point2D.Double(point.x - offset_x, point.y - offset_y);
72         }
73
74         public Point screen(Point point) {
75                 return new Point((int) (point.x - offset_x + 0.5),
76                                  (int) (point.y - offset_y + 0.5));
77         }
78
79         public Rectangle screen(AltosUIMapRectangle map_rect) {
80                 Point2D.Double  ul = screen(map_rect.ul);
81                 Point2D.Double  lr = screen(map_rect.lr);
82
83                 return new Rectangle((int) ul.x, (int) ul.y, (int) (lr.x - ul.x), (int) (lr.y - ul.y));
84         }
85
86         public Point2D.Double screen(AltosUILatLon lat_lon) {
87                 return screen(point(lat_lon));
88         }
89
90         private boolean has_location;
91
92         public boolean has_location() {
93                 return has_location;
94         }
95
96         public AltosUIMapTransform(int width, int height, int zoom, AltosUILatLon centre_lat_lon) {
97                 scale_x = 256/360.0 * Math.pow(2, zoom);
98                 scale_y = 256/(2.0*Math.PI) * Math.pow(2, zoom);
99
100                 Point2D.Double centre_pt = point(centre_lat_lon);
101
102                 has_location = (centre_lat_lon.lat != 0 || centre_lat_lon.lon != 0);
103                 offset_x = centre_pt.x - width / 2.0;
104                 offset_y = centre_pt.y - height / 2.0;
105         }
106 }