1 | /* |
---|
2 | Copyright (C) 2005 Nick Whitelegg, Hogweed Software, nick@hogweed.org |
---|
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, |
---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | GNU General Public License for more details. |
---|
13 | |
---|
14 | You should have received a copy of the GNU General Public License |
---|
15 | along with this program; if not, write to the Free Software |
---|
16 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA |
---|
17 | |
---|
18 | */ |
---|
19 | |
---|
20 | // Note. Anything to do with JEEPS has been put in this file. Using JEEPS |
---|
21 | // in C++ is problematic; if you try and call JEEPS from two C++ source files |
---|
22 | // then link them into the same application, you get errors. This seems to |
---|
23 | // be due to non-standard use of global variables in JEEPS. |
---|
24 | |
---|
25 | #include <map> |
---|
26 | #include <iostream> |
---|
27 | #include <cmath> |
---|
28 | #include <sstream> |
---|
29 | #include <cctype> |
---|
30 | |
---|
31 | #include <float.h> |
---|
32 | |
---|
33 | #include "functions.h" |
---|
34 | |
---|
35 | namespace OSM |
---|
36 | { |
---|
37 | // Distance function |
---|
38 | double dist (double x1, double y1, double x2, double y2) |
---|
39 | { |
---|
40 | double dx=x1-x2, dy=y1-y2; |
---|
41 | return sqrt (dx*dx + dy*dy); |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | // find the distance from a point to a line |
---|
46 | // based on theory at: |
---|
47 | // astronomy.swin.edu.au/~pbourke/geometry/pointline/ |
---|
48 | // given equation was proven starting with dot product |
---|
49 | |
---|
50 | double distp (double px,double py,double x1,double y1, double x2, double y2) |
---|
51 | { |
---|
52 | double u = ((px-x1)*(x2-x1)+(py-y1)*(y2-y1)) / (pow(x2-x1,2)+pow(y2-y1,2)); |
---|
53 | double xintersection = x1+u*(x2-x1), yintersection=y1+u*(y2-y1); |
---|
54 | return (u>=0&&u<=1) ? dist(px,py,xintersection,yintersection) : DBL_MAX; |
---|
55 | } |
---|
56 | |
---|
57 | // Angle function (cosine rule) |
---|
58 | double getAngle(double a, double b, double c) |
---|
59 | { |
---|
60 | double d = (b*b+c*c-a*a) / (2*b*c); |
---|
61 | return acos(d); |
---|
62 | } |
---|
63 | |
---|
64 | // These are taken from JOSM |
---|
65 | LatLon Mercator::toLatLon() |
---|
66 | { |
---|
67 | return LatLon (atan(sinh(n))*180/M_PI, e*180/M_PI); |
---|
68 | } |
---|
69 | |
---|
70 | Mercator LatLon::toMercator() |
---|
71 | { |
---|
72 | return Mercator (lon*M_PI/180, log(tan(M_PI/4+lat*M_PI/360))); |
---|
73 | } |
---|
74 | |
---|
75 | } |
---|