简体   繁体   English

在 cgPath 上的两个坐标之间添加中间点

[英]Add the middle point between two coordinates on a cgPath

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:我们有一个 function,它基于 CLLocationCoordinate2D 数组创建路径,该数组包含 4 个坐标,并将创建一个正方形的路径:

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:我们还有一个 function 计算两个 CGPoint 之间的中间点:

   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.我们想要的是每边获得 4 分,然后移动到下边。 I tried combining functions like this, but it returns around 90 points:我试过像这样组合函数,但它返回大约 90 分:

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:如果坐标是硬编码的,这就是它的样子,每边返回 2 个点:

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?知道如何每边只得到 4 分吗?

Your path.addLine statements are inverted.您的path.addLine语句是倒置的。 First you have to add the midpoint, then the next point.首先,您必须添加中点,然后是下一个点。 Also your getMiddlePoint is called with the same point twice.您的getMiddlePoint也被调用了两次相同的点。

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.另请注意,GPS 坐标是球面坐标,通常不应像在平面上那样计算中点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM