开发者

Trying to determine if a movement vector is CW or CCW around a point

开发者 https://www.devze.com 2023-03-20 10:37 出处:网络
I\'m writing some Touch input code for an iOS de开发者_如何学Pythonvice and need to determine whether the movement of a swipe is clockwise or counterclockwise relative to the center of the screen.

I'm writing some Touch input code for an iOS de开发者_如何学Pythonvice and need to determine whether the movement of a swipe is clockwise or counterclockwise relative to the center of the screen.

Using the position and deltaPosition of the touch, I can calculate an angle between vectors from the center of the screen to those points using the dot product but that doesn't give me a signed angle to decide whether it's CW or not.

How can I tell which direction around the center the swipe has moved?


Let A denote the swipe vector, and B the vector from the start of the swipe to the center of the screen. You can then calculate the determinant of the components of A and B:

| A.x A.y |
| B.x B.y |

= A.x * B.y - B.x * A.y

The sign of the determinant will tell you whether B points to the right side or to the left side of A.

As @Jeremy W. Sherman correctly notes, which sign corresponds to which direction depends on how the coordinate system of the screen is laid out. If the x axis runs to the right and the y axis runs downward, positive means CW and negative means CCW (if I got the mental math right). Zero always means movement directly towards or directly away from the center.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号