Swift 极坐标
我必须画12个点在一个不同辐射的圆圈里。从第1点到第2点,从第2点到第3点,等等,画一条线。这些线不会有问题 我找不到一个公式来求12*(x,y),但我认为它是有极坐标/圆的东西 是否有人在使用它,或者想与我分享 请看图片,这可能比我能解释的更好:Swift 极坐标,swift,geometry,polar-coordinates,Swift,Geometry,Polar Coordinates,我必须画12个点在一个不同辐射的圆圈里。从第1点到第2点,从第2点到第3点,等等,画一条线。这些线不会有问题 我找不到一个公式来求12*(x,y),但我认为它是有极坐标/圆的东西 是否有人在使用它,或者想与我分享 请看图片,这可能比我能解释的更好: 这是我得到的结果: 这是我的游乐场: //: Playground - noun: a place where people can play import Foundation import UIKit class DemoView: UIV
这是我得到的结果: 这是我的游乐场:
//: 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))
你好,艾伦,你能添加图片吗谢谢,你现在能看到图片了吗?你需要阅读数学知识,用极坐标系把你的点放到(x,y)一个经典点上。得到两点后画一条线,很容易。谢谢,这就是我需要的:-)如何添加平滑曲线/样条曲线?谢谢@Toldy,我试图创建一个textLayer,但在半径的5/6处的圆圈内。但我希望它在弧内,而不是在直线上,我希望得到弧内的点