首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将X/ Y圆坐标转换为函数Lat/ Lng

将X/ Y圆坐标转换为函数Lat/ Lng
EN

Stack Overflow用户
提问于 2018-06-03 00:20:52
回答 1查看 157关注 0票数 0

我希望在两个圆的交点的区域上有一个随机x/ y坐标

我现在的问题是,我完成了繁重的部分,并且它确实可以在浏览器中使用Canvas进行演示。但仅适用于2D坐标。我正在使用谷歌地图,我想输入两个圆圈与Lat Lng的位置和半径。结果应该是我在2D上计算的Lat/ Lang。但是我的数学在真实世界的坐标上结束了..

例如,circle1.x和.y就是.lat和.lng。

代码语言:javascript
复制
/******************************************************************
    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();
}
EN

回答 1

Stack Overflow用户

发布于 2018-06-03 03:58:30

您需要知道map projection,它用于将经纬度坐标转换为2d平面的x/y坐标。

您可以找到Google Maps API here的特定信息。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50658877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档