ios - SpriteKit : Draw line using angle and Length

标签 ios swift sprite-kit

我想根据角度和长度而不是 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)

    }

最佳答案

sincos 是你的 friend ,这个答案并不特定于 Swift。

给定 angleradius,如果您以弧度而不是度数定义角度,则直线的终点应为:

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/

相关文章:

swift - 从两个对象检测 SpriteKit 上的碰撞

ios - 2节点 Sprite 套件的异步移动

ios - UITabBarController 上的项目显示不正确

android - 如何通过wifi在iOS和android之间进行通信?

ios - KeychainItemWrapper 未在 iOS 9 OSStatus -34018 上加载(errSecMissingEntitlement)

swift - 以编程方式创建 TabBar 但不在 rootViewController 上

swift - WKWebView 必须停止检测电话号码

iphone - 为 iPhone 5 加载不同的 xib

ios - 防止重复存储到 Realm

swift - SpriteKit 中的方向键