斯威夫特,极坐标

时间:2017-05-11 08:31:52

标签: swift geometry polar-coordinates

我必须在一个不同光芒的圆圈中画出12个点。从第1点开始,画一条线到点2,从第2点到第3点等。线条不会有问题。

我找不到找到12 *(x,y)的公式,但我觉得它是极坐标/圆的东西?

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

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

Circle

1 个答案:

答案 0 :(得分:1)

这是我得到的结果:

enter image description here

这是我的游乐场:

//: 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))