How to find intersection point between circle and line(2 points) using Mapbox(Turf)

536 Views Asked by At

I'm finding how to find intersection point between circle and line(2 points) using Mapbox(Turf).

Turfjs provides "intersect" function but Turf in java doesn't provide that function.

I wonder how to calculate intersection point(longitude and latitude).

My code below:

public void circleIntersectionPointTest() {
        Point circleCenter = Point.fromLngLat(1.0, 1.0);
        double circleRadius = 35; //nautical mile
        Polygon circle = TurfTransformation.circle(circleCenter, circleRadius, TurfConstants.UNIT_NAUTICAL_MILES);

        Point innerPoint = Point.fromLngLat(1.0, 0.5);
        Point outerPoint = Point.fromLngLat(2.0, 1.5);

        //how to find between circle and line with innerPoint and outerPoint
        Point intersectionPoint;
    }
1

There are 1 best solutions below

0
ControlAltDel On

So here's how you do it:

  1. Find the angle: The angle (in radians) is

     double xdiff = x1 - x2;
     double ydiff = y1 - y2;
     //double tan = xdiff / ydiff;
     double atan = Math.atan2(ydiff, xdiff);
     return atan;
    
  2. Now you can find the point on the circle, via

    circleCenter.x + sin(angle) * circleRadius, circleCenter.y + cos(angle) * circleRadius 
    

It's been a while and you should check that I am invoking atan2 the right way, and cosine should be applied to the x parameter and sin to the y parameter - the way trigonometry is taught in school, angles go the opposite way as how we think of them on a compass, so there's some confusion here. But this should give you the idea of how to solve this.

For more, you can check

https://sourceforge.net/p/tus/code/HEAD/tree/tjacobs/MathUtils.java