ios - 在 UIView 上围绕一个圆圈获取 N 个 CGPoints

标签 ios swift geometry

我有一个圆心 CGPoint 和半径 Float,我需要得到围绕圆的 N 个点,例如下图中如何得到对应的 12 个点红点。

enter image description here

这是我的不完整函数:

func getCirclePoints(centerPoint point: CGPoint, and radius: CGFloat, n: Int) [CGPoint] {
    let result: [CGPoint] = stride(from: 0.0, to: 360.0, by: CGFloat(360 / n)).map {
        let bearing = $0 * .pi / 180
        // NO IDEA WHERE TO MOVE FURTHER
    }
    return result
}

getCirclePoints(centerPoint: CGPoint(x: 160, y: 240), radius: 120.0, n: 12)

最佳答案

你快到了!

func getCirclePoints(centerPoint point: CGPoint, radius: CGFloat, n: Int)->[CGPoint] {
    let result: [CGPoint] = stride(from: 0.0, to: 360.0, by: Double(360 / n)).map {
        let bearing = CGFloat($0) * .pi / 180
        let x = point.x + radius * cos(bearing)
        let y = point.y + radius * sin(bearing)
        return CGPoint(x: x, y: y)
    }
    return result
}
let points = getCirclePoints(centerPoint: CGPoint(x: 160, y: 240), radius: 120.0, n: 12)

我认为 and 作为参数名称不是很清楚,所以我删除了它。

关于ios - 在 UIView 上围绕一个圆圈获取 N 个 CGPoints,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50150734/

相关文章:

ios - 从对象到JSON的转换不正确?

ios - 添加/删除其他 UIBar 按钮时如何使 UIBarButtonItem 居中?

swift - 加载 ViewController Swift - 黑屏

android - 像在谷歌地图应用程序中一样在安卓中画圈

objective-c - 从 UIViewController 绘制虚线

ios - 在数组上追加元素后,它只显示在追加方法上,当我们从另一个方法调用时为空 swift 3

ios - UITableViewCell 的高度不起作用

ios - DidSelectRowAtIndexPath SWIFT

c# - 有没有一种简单快捷的方法来检查多边形是否自相交?

algorithm - 凸包或给定点集的两个凸包具有尽可能低的周长