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?