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

时间:2018-05-03 08:47:45

标签: 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)

3 个答案:

答案 0 :(得分:5)

你快到了!

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作为参数名称非常清楚,所以我已将其删除。

答案 1 :(得分:0)

使用弧度而不是度数。在三角函数内部需要它们

func getCirclePoints(centerPoint point: CGPoint, and radius: CGFloat, n: Int) -> [CGPoint] {
    return Array(repeating: 0, count: n).enumerated().map { offset, element in
        let cgFloatIndex = CGFloat(offset)
        let radiansStep = CGFloat.pi * CGFloat(2.0) / CGFloat(n)
        let radians = radiansStep * cgFloatIndex
        let x = cos(radians) * radius + point.x
        let y = sin(radians) * radius + point.y
        return CGPoint(x: x, y: y)
    }
}
func getCirclePoints1(centerPoint point: CGPoint, and radius: CGFloat, n: Int) -> [CGPoint] {
    var resultPoints: [CGPoint] = []
    let radianStep = CGFloat.pi * CGFloat(2.0) / CGFloat(n)
    for radians in stride(from: CGFloat(0.0), to: CGFloat.pi * CGFloat(2.0), by: radianStep) {
        let x = cos(radians) * radius + point.x
        let y = sin(radians) * radius + point.y
        resultPoints.append(CGPoint(x: x, y: y))
    }
    return resultPoints
}
func getCirclePoints2(centerPoint point: CGPoint, and radius: CGFloat, n: Int) -> [CGPoint] {
    let radianStep = CGFloat.pi * CGFloat(2.0) / CGFloat(n)
    return stride(from: CGFloat(0.0), to: CGFloat.pi * CGFloat(2.0), by: radianStep).map { element in
        let cgFloatIndex = CGFloat(element)
        let radiansStep = CGFloat.pi * CGFloat(2.0) / CGFloat(n)
        let radians = radiansStep * cgFloatIndex
        let x = cos(radians) * radius + point.x
        let y = sin(radians) * radius + point.y
        return CGPoint(x: x, y: y)
    }
}

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

答案 2 :(得分:0)

有抽奖参考

import UIKit

let numOfItems = 10

class customView : UIView {

    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.

    override func draw(_ rect: CGRect) {

        for i in 0...numOfItems
        {

            let angle   = 360/CGFloat(numOfItems)  * CGFloat(i) * .pi / 180

            let rad = self.bounds.size.width/2 - 10

            let x =  bounds.midX + cos(angle) * rad

            let y =  bounds.midY + sin(angle) * rad

            let circlePath = UIBezierPath(
                arcCenter: CGPoint(x:x,y:y),
                radius:10,
                startAngle:0,
                endAngle:360,
                clockwise: true)

            let shapeLayer = CAShapeLayer()

            shapeLayer.fillColor = UIColor.red.cgColor
            shapeLayer.strokeColor = UIColor.blue.cgColor
            shapeLayer.lineWidth = 3
            shapeLayer.path = circlePath.cgPath
            layer.addSublayer(shapeLayer)

        }

    }

}

enter image description here