我有一个圆心 CGPoint
和半径 Float
,我需要得到围绕圆的 N 个点,例如下图中如何得到对应的 12 个点红点。
这是我的不完整函数:
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/