forked from mapsforge/vtm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHexagonDrawable.java
More file actions
83 lines (71 loc) · 3.21 KB
/
Copy pathHexagonDrawable.java
File metadata and controls
83 lines (71 loc) · 3.21 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package org.oscim.layers.vector.geometries;
import org.oscim.core.GeoPoint;
import org.oscim.utils.geom.GeomBuilder;
/**
* Predefined class for drawing hexagons on the map.
*/
public class HexagonDrawable extends JtsDrawable {
/**
* @param center GeoPoint - center of the hexagon
* @param radiusKm Radius of the hexagon in kilometers. The size of the
* hexagon may be distorted due to the Mercator projections
* properties.
*/
public HexagonDrawable(GeoPoint center, double radiusKm) {
super(Style.DEFAULT_STYLE);
GeomBuilder gb = new GeomBuilder();
for (int i = 0; i < 6; i++) {
GeoPoint point = findGeoPointWithGivenDistance(center, i * Math.PI / 3, radiusKm);
gb.points(point.getLongitude(), point.getLatitude());
}
geometry = gb.toPolygon();
}
/**
* @param center GeoPoint - center of the hexagon
* @param radiusKm Radius of the hexagon in kilometers. The size of the
* hexagon may be distorted due to the Mercator projections
* properties.
* @param rotationRad rotation of the hexagon in radians
* @param style
*/
public HexagonDrawable(GeoPoint center, double radiusKm, double rotationRad, Style style) {
super(style);
GeomBuilder gb = new GeomBuilder();
for (int i = 0; i < 6; i++) {
GeoPoint point = findGeoPointWithGivenDistance(center,
rotationRad + i * Math.PI / 3,
radiusKm);
gb.points(point.getLongitude(), point.getLatitude());
}
geometry = gb.toPolygon();
}
/**
* This function finds a GeoPoint offset by a distance in the direction
* given in the bearing parameter. It is an approximation due to the
* Mercator projections properties
*
* @param startPoint
* @param initialBearingRadians
* @param distanceKilometres
* @return a new GeoPoint located distanceKilometers away from the
* startPoint in the direction of the initialBearing
*/
private static GeoPoint findGeoPointWithGivenDistance(GeoPoint startPoint,
double initialBearingRadians, double distanceKilometres) {
double radiusEarthKilometres = 6371.01;
double distRatio = distanceKilometres / radiusEarthKilometres;
double distRatioSine = Math.sin(distRatio);
double distRatioCosine = Math.cos(distRatio);
double startLatRad = Math.toRadians(startPoint.getLatitude());
double startLonRad = Math.toRadians(startPoint.getLongitude());
double startLatCos = Math.cos(startLatRad);
double startLatSin = Math.sin(startLatRad);
double endLatRads = Math.asin((startLatSin * distRatioCosine)
+ (startLatCos * distRatioSine * Math.cos(initialBearingRadians)));
double endLonRads = startLonRad
+ Math.atan2(
Math.sin(initialBearingRadians) * distRatioSine * startLatCos,
distRatioCosine - startLatSin * Math.sin(endLatRads));
return new GeoPoint(Math.toDegrees(endLatRads), Math.toDegrees(endLonRads));
}
}