Swift,极坐标

Swift, polar coordinates

我要在一个圆上画 12 个不同辐射点的点。从点 1 画一条线到点 2,从点 2 到点 3,等等。线不会有问题。

我找不到计算 12 * (x,y) 的公式,但我认为它与极坐标/圆有关?

是否有人在使用它并且可能想与我分享?

看图片可能比我解释得更好:

这是我得到的结果:

这是我的游乐场:

//: Playground - noun: a place where people can play

import Foundation
import UIKit

class DemoView: UIView {

    override func draw(_ rect: CGRect) {
        let origin = CGPoint(x: frame.size.width / 2, y: frame.size.height / 2)
        let radius = frame.size.width / 2

        self.createCircle(origin: origin, radius: radius)
        self.addLinesInCircle(origin: origin, radius: radius)
    }

    func createCircle(origin: CGPoint, radius: CGFloat) {
        let path = UIBezierPath()
        path.addArc(withCenter: origin, radius: radius, startAngle: 0, endAngle: CGFloat(2 * Double.pi), clockwise: true)
        path.close()
        UIColor.orange.setFill()
        path.fill()
    }

    func addLinesInCircle(origin: CGPoint, radius: CGFloat) {
        let path = UIBezierPath()
        let incrementAngle: CGFloat = CGFloat.pi / 6
        let ratios: [CGFloat] = [3/6, 5/6, 3/6, 1/6, 5/6, 2/6, 4/6, 2/6, 4/6, 4/6, 4/6, 4/6, 3/6]

        for (index, ratio) in ratios.enumerated() {
            let point = CGPoint(x: origin.x + cos(CGFloat(index) * incrementAngle) * radius * ratio,
                                y: origin.y + sin(CGFloat(index) * incrementAngle) * radius * ratio)
            if index == 0 {
                path.move(to: point)
            } else {
                path.addLine(to: point)
            }
        }
        path.close()
        UIColor.black.set()
        path.stroke()
    }

}

let demoView = DemoView(frame: CGRect(x: 0, y: 0, width: 320, height: 320))