I want to create X number of points by 30 degrees on a circle. Something like this:
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:
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..