【问题标题】:SpriteKit : Draw line using angle and LengthSpriteKit:使用角度和长度画线
【发布时间】:2015-12-15 19:29:13
【问题描述】:

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

    }

【问题讨论】:

    标签: ios swift sprite-kit


    【解决方案1】:

    sincos 是你在这里的朋友,这个答案并不特定于 Swift。

    鉴于angleradius,如果您以弧度而不是度数定义角度,则线的终点应该是:

    let endPoint = CGPoint(x: start.x + sin(angle) * radius, 
                           y: start.y + cos(angle) * radius)
    

    西蒙

    【讨论】:

    • Δx 应该是 cos(angle) * 半径?
    猜你喜欢
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多