-1

I want to create X number of points by 30 degrees on a circle. Something like this:

enter image description here

The code I have right now looks like this:

func pointsOnCircle(num: Int, radius:Float, center:CGPoint) {

        let _by = 360/num
        for i in stride(from: 0, to: 360, by: _by) {

            print("Stride: \(i)")

            let x = radius * cos(Float(i))
            let y = radius * sin(Float(i))
            let pos = CGPoint(x: CGFloat(x)+center.x, y: CGFloat(y)+center.y)

            var node: SKSpriteNode!
            if(i == 0){
                node = SKSpriteNode(color: SKColor.blue, size: CGSize(width: 10, height: 10))
            } else if (i == 90){
                 node = SKSpriteNode(color: SKColor.orange, size: CGSize(width: 10, height: 10))
            } else if (i == 180){
                node = SKSpriteNode(color: SKColor.purple, size: CGSize(width: 10, height: 10))
            } else {
                 node = SKSpriteNode(color: SKColor.red, size: CGSize(width: 10, height: 10))
            }
            node.alpha = 0.5
            node.position = pos
            node.zPosition = 20
            self.addChild(node)

        }

}

Since 360 divided by 12 is 30.. I call the function like this:

pointsOnCircle(num: 12, radius: 100, center: CGPoint(x: frame.midX, y: frame.midY))

I don't get the result I'm expecting though as you can see on the image below:

enter image description here

The blue rectangle is 0 degrees which is correct

The purple rectangle is 180 degrees which is incorrect

The orange rectangle is 90 degrees which is incorrect as well..

Eve
  • 61
  • 2
  • 9
  • You need to be in radians, not degrees – Knight0fDragon Jul 09 '19 at 18:22
  • var degreesToRadians: CGFloat { return CGFloat(self) * .pi / 180 } and works like charm! Do you know how to rotate the rectangles to be outside the circle having anchorPoint = 0.5, 0 ? – Eve Jul 09 '19 at 18:25

1 Answers1

-2

Helper functions to convert degrees to radians

EDIT by Knight0fDragon - source: How can I convert from degrees to radians? Thank You @LeoDabus for providing a wonderful extension.

extension BinaryInteger {
    var degreesToRadians: CGFloat { return CGFloat(self) * .pi / 180 }
}

extension FloatingPoint {
    var degreesToRadians: Self { return self * .pi / 180 }
    var radiansToDegrees: Self { return self * 180 / .pi }
}

Results

45.degreesToRadians         // 0.785398163397448

Int(45).degreesToRadians    // 0.785398163397448
Int8(45).degreesToRadians   // 0.785398163397448
Int16(45).degreesToRadians  // 0.785398163397448
Int32(45).degreesToRadians  // 0.785398163397448
Int64(45).degreesToRadians  // 0.785398163397448

UInt(45).degreesToRadians   // 0.785398163397448
UInt8(45).degreesToRadians  // 0.785398163397448
UInt16(45).degreesToRadians // 0.785398163397448
UInt32(45).degreesToRadians // 0.785398163397448
UInt64(45).degreesToRadians // 0.785398163397448

Double(45).degreesToRadians    // 0.7853981633974483
CGFloat(45).degreesToRadians   // 0.785398163397448
Float(45).degreesToRadians     // 0.7853981
Float80(45).degreesToRadians   // 0.785398163397448278999
Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44
Eve
  • 61
  • 2
  • 9