4

Im trying to cover a radius of, lets say 5 KM on the map. I have the coordinates of the middle of it.

I need a method to calculate and give me the coordinates of the points that are ON that circle.

Explanation:

enter image description here

(I need it to get coordinate points in a __Km radius from my server, If there's a better way to do that, please share. ty!)

I'll check whether if the coordinates are inside the circle or not like that:

(lan,lon) is a point.

if ((lat<x1 && lat>x2) && (lon<y1 && lon>y2))

    then the point is inside the circle.

but then theres the problem that in some parts of the world it doesnt work...

Im confused. how do I make a radar?

(android app)

Thanks!

mn6vdv23g
  • 734
  • 2
  • 10
  • 33
  • 1
    There are an infinite number of points on the circle. Which ones do you want? – Oliver Charlesworth Sep 25 '13 at 18:09
  • I need them in the area where they are in the drawing. – mn6vdv23g Sep 25 '13 at 18:15
  • also, now when I think about it, Im not sure this is going to work for me. Does anybody knows how to make a "radar" that will check whether a point is inside a circle or not? – mn6vdv23g Sep 25 '13 at 18:16
  • yes you can say that but I need a better solution. updated the question. – mn6vdv23g Sep 25 '13 at 18:17
  • 1
    You seem to have a wrong understanding of longitute and latitude as rectangular grid coordinates. Thats very wrong. Take a look at a globe. The nearer you are to the poles, the tighter the grid becomes, until right at the pole the distance between longitutes becomes *zero*. Determination of distances without taking these properties into account will be fruitless. – Durandal Sep 25 '13 at 18:24
  • @Durandal, I'd say, that in radius of 5 km the error will be quite tolerable, unless it's the very pole. – svz Sep 25 '13 at 18:52
  • @svz That would depend on your definiton of *tolerable*. Don't fool yourself into assuming you can just correct with a few factors and have it work almost globally - the difference in size of a long/lat "rectangle" compared between norway and italy is already quite huge, and those are both in europe. And europe is almost the smallest continent. – Durandal Sep 26 '13 at 17:37

2 Answers2

5

When you are not too near to a pole you can approximate a smallish true circle on the surface of the Earth with an ellipse in the latitude-longitude space. The ellipse is a circle on the equator, but becomes more and more squished as you approach the poles.

Assuming the coordinates of the center of the circle you are interested in is (lat0, lon0) and the radius is r = 5km, set:

C = 40075.04km                - Earth circumference
A = 360*r/C                   - semi-minor in north-south direction 
B = 360*r/(C*cos(lat0))       - semi-major in east-west direction

Now the points (lat,lon) that within the ellipse fulfill this condition:

(lat-lat0)²/A² + (lon-lon0)²/B² < 1

In Java:

boolean pointInCircle(double lat0, double lon0, double r, double lat, double lon) {
    double C = 40075.04, A = 360*r/C, B = A/Math.cos(Math.toRadians(lat0));
    return Math.pow((lat-lat0)/A, 2) + Math.pow((lon-lon0)/B, 2) < 1;
}

For more accurate results you can compute the distance between the center and the given point (typically using the haversine formula), but the formulas are a lot more complex and can become expensive to compute if you have to calculate the distance for many points.

Community
  • 1
  • 1
Joni
  • 108,737
  • 14
  • 143
  • 193
  • Thanks you so much! Would you please also write me how to check `(lat-lat0)²/A² + (lon-lon0)²/B² < 1` in PHP? - because Im downloading only the needed points. @Joni – mn6vdv23g Sep 25 '13 at 19:33
  • Use `pow` instead of `Math.pow`, `cos` instead of `Math.cos`, and `deg2rad` instead of `Math.toRadians`. – Joni Sep 25 '13 at 19:35
  • And actually you were supposed to use the circumference, not the radius (fixed).. – Joni Sep 25 '13 at 19:45
1
double dx = x - x0;
double dy = y - y0;
if (dx * dx + dy * dy < r * r) {
    // point (x, y) is inside the circle centered (x0, y0) with radius r
}
Piotr Kołaczkowski
  • 2,601
  • 12
  • 14