简体   繁体   中英

SpriteKit : Draw line using angle and Length

I want to do some drawing based on angles and lengths instead of coordinance in SpriteKit (Swift)

I have the following function that draws from 2 points but I want to create one that draws from 1 point to another place based on angle and length of line

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 and cos are your friends here and this answer isn't specific to Swift.

Given angle and radius , if you define your angle in radians rather than degrees, the end point of your line should be:

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

Simon

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM