我想根据角度和长度而不是 SpriteKit (Swift) 中的坐标来做一些绘图
我有以下从 2 个点绘制的函数,但我想创建一个根据角度和线长从 1 个点绘制到另一个地方的函数
func drawLine( start: CGPoint, end: CGPoint)
{
CGPathMoveToPoint(ref, nil, start.x * 100, start.y * 100)
let line = SKShapeNode()
CGPathAddLineToPoint(ref, nil, end.x * 100, end.y * 100)
line.path = ref
line.lineWidth = 4
line.fillColor = UIColor.redColor()
line.strokeColor = UIColor.redColor()
self.addChild(line)
}
最佳答案
sin
和 cos
是你的 friend ,这个答案并不特定于 Swift。
给定 angle
和 radius
,如果您以弧度而不是度数定义角度,则直线的终点应为:
let endPoint = CGPoint(x: start.x + sin(angle) * radius,
y: start.y + cos(angle) * radius)
西蒙
关于ios - SpriteKit : Draw line using angle and Length,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34287724/