Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is clockwise of Path.addArc in SwiftUI wrong?

Tags:

path

swiftui

Below is from the official Apple document:

This method creates an open subpath. The created arc lies on the perimeter of the specified circle. When drawn in the default coordinate system, the start and end angles are based on the unit circle shown in Figure 1. For example, specifying a start angle of 0 radians, an end angle of π radians, and setting the clockwise parameter to true draws the bottom half of the circle. However, specifying the same start and end angles but setting the clockwise parameter set to false draws the top half of the circle.

But I found that the result seemed is just opposite. Below is my code

var body: some View {
    Path { path in
        path.addArc(center: CGPoint(x: 200, y: 370), radius: 50, startAngle: Angle(degrees: 0), endAngle: Angle(degrees: 180.0), clockwise: true)
        path.closeSubpath()
    }
}

I set the clockwise parameter to true but the result is the top half of the circle, not the bottom half

enter image description here

Did I understand wrong about Apple's document? My Xcode Version is 11.0 beta 4 (11M374r)

like image 753
echo Avatar asked Jul 26 '19 19:07

echo


1 Answers

The meaning of the clockwise parameter exists in a quantum superposition that collapses when you examine the result. Unfortunately, it always collapses to the opposite meaning from what you wanted. 😅

More seriously, there are some flaws in Apple's documentation:

  • The “default coordinate system” really means the standard Cartesian coordinate system, in which the y axis increases toward the top of the canvas. But both SwiftUI and UIKit always set up the coordinate system with the y axis “flipped” so that y values increase toward the bottom of the canvas.

  • clockwise is accurate only in the standard Cartesian coordinate system. What it really means is “the direction of rotation that goes from the positive y axis toward the positive x axis”. So when you're working in a flipped coordinate system, clockwise means the opposite direction!

The diagrams below may clarify what I mean. The top diagram shows a standard Cartesian coordinate system, where the direction of rotation from the positive y axis to the positive x axis is indeed clockwise. The bottom diagram shows a flipped coordinate system, which is what SwiftUI and UIKit provide, and where the direction of rotation from the positive y axis to the positive x axis is actually counterclockwise, but which the APIs call “clockwise”.

diagram of a standard coordinate system and a flipped coordinate system, with arrows labeled “clockwise” drawn correctly for each

like image 175
rob mayoff Avatar answered Nov 09 '22 12:11

rob mayoff