0

I wanted a random x/ y coordinate on the area of an intersection of two circles

What the script does

My problem now is that i got the heavy part done, and it does work in the browser with Canvas for demonstration. But only on 2D Coordinates. Im using Google Maps, and i wanted to input two Circles with the Lat Lng positions and a radius. The result should have been the Lat/ Lang which i calculated on 2D. But my mathematics end on real world coordinates..

so circle1.x and .y would be .lat and .lng for example.

/******************************************************************
    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();
}
geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • What is your question? – geocodezip Jun 02 '18 at 18:29
  • The question was just that how this script works on canvas, to show it on Google maps with circle markers and a marker with the random position. But instead of canvas coordinates, real world lat lng coordinates and meters for the radius. Also having the world projection in mind, I'm doing hard to get this working. Maybe I think to overcomplicated – Markus Lechner Jun 03 '18 at 21:24

1 Answers1

0

You need to know the map projection that it's used to convert between lat/long coordinates to a x/y coordinates of a 2d plane.

You can find specific information for the Google Maps API here.

vassiliskrikonis
  • 566
  • 2
  • 10
  • Thanks for the hint! I'm aware of the projections, but somehow my mind blocks here. I don't know where to convert the 2D to world coordinates. In the beginning of the script or everywhere, or at the end.. – Markus Lechner Jun 03 '18 at 09:33
  • Take a look at https://stackoverflow.com/questions/25219346/how-to-convert-from-x-y-screen-coordinates-to-latlng-google-maps – Anatolii Suhanov Jun 03 '18 at 19:24
  • and https://stackoverflow.com/questions/5471848/how-to-get-screen-xy-from-google-maps-v3-latlng – Anatolii Suhanov Jun 03 '18 at 19:26