3

We have a function that creates a path based on an array of CLLocationCoordinate2D, this array contains 4 coordinates, and will create a path with the shape of a square:

func createPath(coordinates: [CLLocationCoordinate2D]) {
    for (index, coordinate) in coordinates.enumerated() {
        if index == 0 {
            path.move(to: CGPoint(x: coordinate.latitude, y:coordinate.longitude))
        } else {
            path.addLine(to: CGPoint(x: coordinate.latitude, y: coordinate.longitude))
        }
    }
}

We also have a function that calculates the middle point between two CGPoints:

   func getMiddlePoint(firstPoint: CGPoint, secondPoint: CGPoint) -> CGPoint {
        
        var middlePoint = CGPoint()
        
        middlePoint.x = (firstPoint.x + secondPoint.x)/2
        middlePoint.y = (firstPoint.y + secondPoint.y)/2
        return middlePoint
    }

What we want is to get four points per side and then move to the next side. I tried combining functions like this, but it returns around 90 points:

func createPath(coordinates: [CLLocationCoordinate2D]) {
    for (index, coordinate) in coordinates.enumerated() {
        if index == 0 {
            path.move(to: CGPoint(x: coordinate.latitude, y:coordinate.longitude))
        } else {
            path.addLine(to: CGPoint(x: coordinate.latitude, y: coordinate.longitude))//get the first point
            path.addLine(to:  getMiddlePoint(firstPoint: CGPoint(x: coordinate.latitude, y: coordinate.longitude), secondPoint: CGPoint(x: coordinate.latitude, y: coordinate.longitude))) // get the middle point
        }
    }
}

This is how it would look if the coordinates are hardcoded, this returns 2 points per side:

func createPath(){
    path.move(to: CGPoint(x: 32.7915055, y: -96.8028408))//1
    path.addLine(to: CGPoint(x: 32.79174845, y: -96.80252195))//midpoint between 1 and 2
    path.addLine(to: CGPoint(x: 32.7919914, y: -96.8022031))//2
    path.addLine(to: CGPoint(x: 32.791501100000005, y: -96.80235195))////midpoint between 2 and 3
    path.addLine(to: CGPoint(x: 32.7910108, y: -96.8025008))//3
    path.addLine(to: CGPoint(x: 32.791301700000005, y: -96.8020985))//midpoint between 3 and 4
    path.addLine(to: CGPoint(x: 32.7915926, y: -96.8016962))//4
    path.addLine(to: CGPoint(x: 32.79154905, y: -96.8022685))//midpoint between 4 and 1
    
}

Any idea how to only get 4 points per side?

SwiftyJD
  • 5,257
  • 7
  • 41
  • 92
  • What does "get four points per side"? Do you mean you wish you had drawn the side as three straight line segments going in the same direction, rather than as one straight line segment? – matt Aug 31 '20 at 18:18
  • I'm trying to draw one straight line but get the points on that line, I added some more code to the question that may clear it up a bit. – SwiftyJD Aug 31 '20 at 18:21
  • You're not answering my question, namely, what the ultimate goal is. You don't want merely to "get the points on that line". You want to _do_ something with the extra points, and I'm trying to find out what it is. It looks like what you want to do with them is draw. What are we really trying to draw here? – matt Aug 31 '20 at 18:27

1 Answers1

0

Your path.addLine statements are inverted. First you have to add the midpoint, then the next point. Also your getMiddlePoint is called with the same point twice.

Written differently:

var prevPoint: CGPoint?

for coordinate in coordinates {
    let point = CGPoint(x: coordinate.latitude, y: coordinate.longitude)

    if let prevPoint = prevPoint {
       path.addLine(to: getMiddlePoint(firstPoint: prevPoint, secondPoint: point))
       path.addLine(to: point)
    } else {
       path.move(to: point)
    }

    prevPoint = point
}

Also note that GPS coordinates are spherical, usually the midpoint should not be calculated the same way as on a plane.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • so what would be a good way to calculate the mid point? – SwiftyJD Aug 31 '20 at 18:36
  • @SwiftyJD There is a special algorithm, see for example https://stackoverflow.com/questions/10559219/determining-midpoint-between-2-coordinates – Sulthan Aug 31 '20 at 18:45