Search code examples
ioscore-graphics

How to start & end a path at top of the circle?


Given the following code:

        let startAngle: CGFloat = CGFloat( (3 * M_PI) / 2 )
        let endAngle:CGFloat = CGFloat( (3 * M_PI) / 2 )
        let path = UIBezierPath(arcCenter: theCenter, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true).CGPath

And knowing that this is how iOS draws its circles as such:

https://i.sstatic.net/1yJo6.png

What do I have to specify to start my circle at top (12:00) & end it at 90 degrees? If I wanted to start it & end it at 0, I would set start & end to 0 & 2π respectively.


Solution

  • You'll want to go from -π/2 to 3π/2 (in a clockwise arc).

    You can't go from 3π/2 to 3π/2, as you tried in the question, as iOS will see that as the same angle - and therefore won't generate any arc. You'll therefore want to use the negative representation of 3π/2 (-π/2).

    Although note that the unit circle is continuous. Therefore 3π/2 to 7π/2 is just as valid a range.

    Also note that the unit circle's positive angle direction is actually anti-clockwise. iOS does it in a clockwise manner as the coordinate system's y-axis is flipped.