forked from WorldWindLabs/Quake-Hunter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircle.js
94 lines (71 loc) · 2.91 KB
/
Circle.js
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
84
85
86
87
88
89
90
91
92
93
94
/**
* Created by gagaus on 7/29/16.
*/
define(['./Rectangle',
'./worldwind.min',
'./WorldPoint'],
function(Rectangle,
WorldWind,
WorldPoint) {
"use strict";
var Circle = function (p1, p2) {
var R = 6371; // Radius of the earth in km
function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
var dLat = deg2rad(lat2-lat1); // deg2rad below
var dLon = deg2rad(lon2-lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Distance in km
return d;
}
function deg2rad(deg) {
return deg * (Math.PI/180);
}
function rad2deg(rad) {
return rad * (180/Math.PI);
}
function distance2d(p1, p2) {
return Math.sqrt(Math.pow(p1.X-p2.X, 2) + Math.pow(p2.Y-p1.Y, 2));
}
var minLong = Math.min(p2.Long, p1.Long);
var maxLong = Math.max(p2.Long, p1.Long);
var minLati = Math.min(p2.Lati, p1.Lati);
var maxLati = Math.max(p2.Lati, p1.Lati);
var renderedCircle = function () {
var canvas = document.createElement("canvas"),
ctx2d = canvas.getContext("2d"),
radius = distance2d(p1,p2),
size = 2*radius,
c = radius;
canvas.width = size;
canvas.height = size;
ctx2d.lineWidth = 100;
ctx2d.arc(c, c, radius, 0, 2 * Math.PI, false);
ctx2d.fill();
return canvas;
};
this.origin = p1;
this.radius3D = getDistanceFromLatLonInKm(p1.Lati, p1.Long, p2.Lati, p2.Long);
this.radius2D = distance2d(p1,p2);
var alpha = rad2deg(this.radius3D/R);
var lowerVertex = new WorldPoint(p1.wwd);
lowerVertex.setLong(p1.Long - alpha);
lowerVertex.setLati(p1.Lati - alpha);
var upperVertex = new WorldPoint(p1.wwd);
upperVertex.setLong(p1.Long + alpha);
upperVertex.setLati(p1.Lati + alpha);
var square = new Rectangle(lowerVertex, upperVertex, false, false);
square.origin = p1;
square.radius3D = this.radius3D;
square.attributes.imageSource = ["./images/circle3.png"];
// square.attributes.imageSource = renderedCircle();
square.attributes.drawInterior = true;
// square.attributes.drawOutline = false;
return square;
};
return Circle;
});