【发布时间】:2018-11-12 12:11:21
【问题描述】:
我想要一个随机 x/y 坐标位于两个圆的交点
我现在的问题是我已经完成了繁重的部分,并且它确实可以在带有 Canvas 的浏览器中进行演示。但仅适用于 2D 坐标。我使用 Google 地图,我想输入两个带有 Lat Lng 位置和半径的圆。结果应该是我在 2D 上计算的 Lat/Lang。 但我的数学以现实世界坐标结束..
例如,circle1.x 和 .y 将是 .lat 和 .lng。
/******************************************************************
Generate New Coord From Intersect
Generate random position point on cntersect area of two circles.
*******************************************************************/
var circle1 = {x : 0, y : 0, radius : 10};
var circle2 = {x : 0, y : 0, radius : 10};
var ctx;
var p2 = {x : 0, y : 0};
var p3 = {x : 0, y : 0};
var t3 = {x : 0, y : 0};
var t6 = {x : 0, y : 0};
var t7 = {x : 0, y : 0};
function GenerateNewCoordFromIntersect(circle1, circle2) {
var c = document.getElementById('canvasID');
c.width = 500;
c.height = 500;
ctx = c.getContext('2d');
drawCircle(circle1.x,circle1.y,circle1.radius,"red");
drawCircle(circle2.x,circle2.y,circle2.radius,"blue");
var distance = Math.sqrt(Math.pow(circle2.x-circle1.x,2) + Math.pow(circle2.y-circle1.y,2));
// then there are no solutions, the circles are separate.
if (distance > circle1.radius + circle2.radius ) {
return;
}
// then there are no solutions because one circle is contained within the other.
if (distance < circle1.radius - circle2.radius ) {
return;
}
// then the circles are coincident and there are an infinite number of solutions.
if (distance == 0 && circle1.radius == circle2.radius) {
return;
}
// random if take sector of circle 1 or 2
if (Math.random() > 0.5) {
var newcircle1 = JSON.parse(JSON.stringify(circle1));
var newcircle2 = JSON.parse(JSON.stringify(circle2));
circle1 = newcircle2;
circle2 = newcircle1;
}
// calc a
a = ((Math.pow(circle1.radius,2) - Math.pow(circle2.radius,2) + Math.pow(distance,2))) / (2 * distance);
// calc height
h = Math.sqrt(Math.pow(circle1.radius,2) - Math.pow(a,2));
// calc middle point of intersect
p2.x = circle1.x + a * (circle2.x - circle1.x) / distance;
p2.y = circle1.y + a * (circle2.y - circle1.y) / distance;
// calc upper radius intersect point
p3.x = p2.x + h * (circle2.y - circle1.y) / distance;
p3.y = p2.y - h * (circle2.x - circle1.x) / distance;
// random height for random point position
var randNumber = Math.random() / 2 + Math.random() * 0.5;
var radiusOfH = (h*2);
var randh = (randNumber) * radiusOfH - h;
// calc random Hypotenuse
var hypDistance = Math.abs(Math.sqrt(Math.pow(a,2) + Math.pow(randh,2)));
var randomHyp = (circle1.radius - hypDistance) + hypDistance;
// random point on line of middlepoint
t3.x = p2.x + ((randh) * (circle2.y - circle1.y)) / distance;
t3.y = p2.y - (randh) * (circle2.x - circle1.x) / distance;
// angle calc
var winkel = Math.atan(randh / a);
var newA = Math.cos(winkel) * randomHyp; //(randomHyp);
var newH = Math.sin(winkel) * randomHyp;//newA ;
t6.x = circle1.x + newA * (circle2.x - circle1.x) / distance;
t6.y = circle1.y + newA * (circle2.y - circle1.y) / distance;
t7.x = t6.x + newH * (circle2.y - circle1.y) / distance;
t7.y = t6.y - newH * (circle2.x - circle1.x) / distance;
randNumber = Math.random();
var xDist = (t7.x - t3.x) * (randNumber);
var yDist = (t7.y - t3.y) * (randNumber);
var rx = t3.x + xDist;
var ry = t3.y + yDist;
drawCircle(rx,ry,2,"blue");
}
function drawCircle(x, y, r, fill) {
ctx.beginPath();
ctx.arc(x,y,r,0,2*Math.PI);
ctx.strokeStyle = fill;
ctx.stroke();
}
【问题讨论】:
-
你有什么问题?
-
问题只是这个脚本如何在画布上工作,用圆形标记和随机位置的标记在谷歌地图上显示它。但不是画布坐标,而是真实世界的 lat lng 坐标和半径的米。还考虑到世界投影,我正在努力让它发挥作用。可能我觉得太复杂了
标签: javascript google-maps math coordinates coordinate-systems