altoslib/altosuilib: Get new Map display code running in altosui and telegps
[fw/altos] / altoslib / AltosMapLine.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.altoslib_7;
19
20 import java.io.*;
21 import java.lang.Math;
22 import java.util.*;
23 import java.util.concurrent.*;
24
25 public abstract class AltosMapLine {
26         public AltosLatLon      start, end;
27
28         static public int stroke_width = 6;
29
30         public abstract void paint(AltosMapTransform t);
31
32         private AltosLatLon lat_lon(AltosPointInt pt, AltosMapTransform t) {
33                 return t.screen_lat_lon(pt);
34         }
35
36         public void dragged(AltosPointInt pt, AltosMapTransform t) {
37                 end = lat_lon(pt, t);
38         }
39
40         public void pressed(AltosPointInt pt, AltosMapTransform t) {
41                 start = lat_lon(pt, t);
42                 end = null;
43         }
44
45         public String line_dist() {
46                 String  format;
47                 AltosGreatCircle        g = new AltosGreatCircle(start.lat, start.lon,
48                                                                  end.lat, end.lon);
49                 double  distance = g.distance;
50
51                 if (AltosConvert.imperial_units) {
52                         distance = AltosConvert.meters_to_feet(distance);
53                         if (distance < 10000) {
54                                 format = "%4.0fft";
55                         } else {
56                                 distance /= 5280;
57                                 if (distance < 10)
58                                         format = "%5.3fmi";
59                                 else if (distance < 100)
60                                         format = "%5.2fmi";
61                                 else if (distance < 1000)
62                                         format = "%5.1fmi";
63                                 else
64                                         format = "%5.0fmi";
65                         }
66                 } else {
67                         if (distance < 10000) {
68                                 format = "%4.0fm";
69                         } else {
70                                 distance /= 1000;
71                                 if (distance < 100)
72                                         format = "%5.2fkm";
73                                 else if (distance < 1000)
74                                         format = "%5.1fkm";
75                                 else
76                                         format = "%5.0fkm";
77                         }
78                 }
79                 return String.format(format, distance);
80         }
81 }