I'm working on an app in Xcode using swift and sprite-kit, and I have an array of points that are arranged in an ellipse. I'm wondering how I can draw a smooth white curve that connects the points of the array. I'm pretty sure it has something to do with SKShapeNode, but I'm uncertain about how to do it. Thanks in advance for any help.
Asked
Active
Viewed 1,881 times
0
-
Is it a perfect ellipse? You could just draw an ellipse that fits the points. – Noah Witherspoon Jan 03 '15 at 18:02
-
@NoahWitherspoon It is, but the points will be different every time depending on how the user interacts with the app, so I won't have any way of knowing what ellipse fits the points ahead of time. – Nightman Jan 03 '15 at 18:13
-
That’s what math is for :) See [this answer](http://math.stackexchange.com/questions/632442/calculate-ellipse-from-points). – Noah Witherspoon Jan 03 '15 at 18:16
-
@NoahWitherspoon Also, if possible (not sure if it is for you) if you can just calculate the max/min x and the max/min y you can make a CGRect with those points, and create an SKShapeNode using an ellipse in a rectangle. However, if the points are not at the max/min x and y, then you will have to do the math as previously stated. – Kendel Jan 03 '15 at 18:27
1 Answers
1
If you want a perfect ellipse you can use:
convenience init(ellipseInRect rect: CGRect)
or
convenience init(ellipseOfSize size: CGSize)
If you need to just use those points you can create a CGPath, and make an SKShapeNode with:
convenience init(path path: CGPath!)
or
convenience init(path path: CGPath!, centered centered: Bool)
More info can be found at these links:
CGPath: https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CGPath/index.html
And finally here are all 4 in action:
let rect = CGRect(x: 10, y: 10, width: 30, height: 40)
let size = rect.size
//for convenience init(ellipseInRect rect: CGRect)
let ellipse1 = SKShapeNode(ellipseInRect: rect)
//for convenience init(ellipseOfSize size: CGSize)
let ellipse2 = SKShapeNode(ellipseOfSize: size)
//path is a little harder to explain without more context
let path = CGPathCreateWithRect(rect, nil)
//for convenience init(path path: CGPath!)
let ellipse3 = SKShapeNode(path: path)
//for convenience init(path path: CGPath!, centered centered: Bool)
let ellipse4 = SKShapeNode(path: path, centered: true)

Kendel
- 1,698
- 2
- 17
- 33