1

I've an object whose rotation degrees are locked to be in the interval $[0, 360)$. I want to calculate the distance in two different directions, so that I can decide which is the shorter route for a spinning. That is, in a -1 scale (anticlockwise), or 1 scale (clockwise), while rotating (or "spinning") my object until the final angle.

For example, say I've ${ x = 0, y = 90 }$. My final (target) angle is $x$. If I go from $y$ to $x$ by incrementing the current object's angle by +1, how many increments will it take to reach $x$? If I instead increment the current object's angle by $-1$, how many increments will it take to reach $x$?

The delta $y - x$ only tells one direction. I need to know the delta if I spin until $x$ from left and the delta if I spin until $x$ from right.

Illustration:

Arc


Thanks @Souparna and @RossMillikan. I tried this equation in JavaScript:

{
  let x = 50, y = 359;
  let xy = x - y, yx = y - x;
  xy = xy < 0 ? xy + 360 : xy;
  yx = yx < 0 ? yx + 360 : yx;
  xy < yx // if true, go clockwise
}

That should yield true. It looks like it works.

Hydroper
  • 123
  • y to x by incrementing +1 = +270 degrees (+ means anticlockwise rotation). y to x by decrementing -1 = -90 degrees (- means clockwise rotation). Here, $y−x=360n-90$ , where $n$ is any integer. – Souparna Jun 04 '23 at 13:18
  • @Souparna $360 \times -1 - 90$ yields $-450$. Also, I don't think it'd work to map $x = 0$ to $x = 360$, besides 360 isn't even included. – Hydroper Jun 04 '23 at 13:22
  • In your given interval, -90 and 270 are the only solutions. But in general, any angle $x=360n+x$. So, $-90=360n-90$. For $x=0$ to $x=360$, angle$= 360n-360$ or simply $360n$ as 0 and 360 mean the same. – Souparna Jun 04 '23 at 13:27

1 Answers1

3

The two angles are $x-y$ and $y-x$. One of those will be negative, so add $360$ to it. Then compare the two values and take the smaller. For example, if $x=10, y=200, y-x=190, x-y+360=170$ and you should go clockwise.

Ross Millikan
  • 374,822
  • Sorry, I thought I got it working. I tried an equation based on that, any idea? I updated the question. – Hydroper Jun 04 '23 at 13:59
  • I don't speak JS but you should just go through your example line by line to see what is wrong. Is your final comparison backwards? Remember angles usually increase counterclockwise. – Ross Millikan Jun 04 '23 at 14:02
  • Right, it looks like I messed with the less-than and greater-than operators... – Hydroper Jun 04 '23 at 14:04