繁体   English   中英

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

[英]Get N number of CGPoints around a circle on UIView

我得到了一个中心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非常清楚作为一个参数名称所以我已经删除了它。

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

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)

有抽奖参考

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)

        }

    }

}

在此输入图像描述

暂无
暂无

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

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