2

I am drawing a MKCircle on MKMapView, I need to get all points in the Circle border (not inside it) to send the points coordinates to server and get the information that related to the region which is inside the circle.

Also as I have the center coordinate and radius of circle So maybe points could be calculated by this variables.

How can I get the points coordinates ?

Morteza Soleimani
  • 2,652
  • 3
  • 25
  • 44
  • 1
    Aren't there infinite points on the circumference of a circle? – James Webster Aug 27 '15 at 08:37
  • @JamesWebster is right, Maths says _"Circle, line or any shape are combination of infinite points"._ These are micro co-ordinates, you can not get all of them. How ever from certain arc or part of circle contains point of interest can be derived. – Kampai Aug 27 '15 at 10:23

2 Answers2

3

I calculated the points, But as you know we can't get ALL the points, But this method returns about 40 points of the circle that is enough.

internal func radiusSearchPoints(coordinate: CLLocationCoordinate2D, radius: CLLocationDistance) -> [CLLocationCoordinate2D] {
    var points: [CLLocationCoordinate2D] = []
    let earthRadius = 6_378_100.0
    let π = Double.pi
    let lat = coordinate.latitude * π / 180.0
    let lng = coordinate.longitude * π / 180.0

    var t: Double = 0
    while t <= 2 * π {
        let pointLat = lat + (radius / earthRadius) * sin(t)
        let pointLng = lng + (radius / earthRadius) * cos(t)

        let point = CLLocationCoordinate2D(latitude: pointLat * 180 / π, longitude: pointLng * 180 / π)
        points.append(point)
        t += 0.3
    }

    return points
}
Axel Guilmin
  • 11,454
  • 9
  • 54
  • 64
Morteza Soleimani
  • 2,652
  • 3
  • 25
  • 44
  • It would be better to send a centre point and radius. That way the server (or whoever) can calculate as many points as they require. – Fogmeister Aug 27 '15 at 10:53
  • @Fogmeister yes it is better, But I can't change the server side code because it is not just using by my app, I should develop the app based on server files structures ;) – Morteza Soleimani Aug 27 '15 at 22:34
  • Thanks for the answer. How would you set it up to have 4 coordinates, right, top, left and bottom? Thanks again – Septronic Aug 22 '18 at 23:51
  • 1
    @Septronic look at my code, If you use 0, π/2, π and 3π/2 for t variable, Points should be right, top, left, bottom – Morteza Soleimani Aug 23 '18 at 15:42
1

There are infinite points on the circumference of a circle, so it's quite impossible to get all of them.

However, it is possible to determine if a given point is on the circle's border using just your centre coordinate and your radius

The equation of a circle at the origin is given by the parametric equation:

x = cos(angle) * radius
y = sin(angle) * radius

For the example circle: centre = (1, 1) radius = 2 and the example point (3, 1)

dx = point.x - centre.x
dy = point.y - centre.y

dx = 3 - 1 = 2
dy = 1 - 1 - 0

angle = atan2(dy/dx) = 0

dx = cos(angle) * radius
rearranging gives:

radius = dx/cos(angle)
radius = 2/cos(0)
radius = 2/1
radius = 2 

The distance between this point and the centre of the circle is the same as the radius, therefore the point is on the circumference.

James Webster
  • 31,873
  • 11
  • 70
  • 114