How to turn this algorithm into something without a loop

I had not been certain if this was a far better area than Stack Overflow to ask this inquiry. On a standard degree, I have an algorithm I've created in Java to calculate a number, yet I seem like there is a far better means to write it without a loop that simply entails use floor and also points like that.

So below is my algorithm:

float startAngle = getRotation();
double dAngle = (startAngle < 0 ? -Math.PI : Math.PI);
double endAngle = (float)((destination % 2 == 0) ? 0 : dAngle/2);
while (Math.abs(endAngle - startAngle) > Math.PI/2)
  endAngle += dAngle;

In less complex terms it does the following:

  1. Get the existing angle of the object
  2. Get a delta angle based upon whether the object has actually been revolved left or appropriate (more left or better appropriate)
  3. Define an angle to revolve the challenge, based upon whether the location needs to be revolved identical or vertical to 0
  4. As long as the distinction in between completion angle and also start angle is more than 90 levels, transform completion angle by the delta from action 2

The factor behind all this is I've obtained playing cards on the screen randomly angles, which any one of 4 gamers can take (where location 0 is all-time low of the screen, 1 is left, 2 is top, and also 3 is appropriate). I intend to revolve the having fun cards the fastest range to encounter that gamer as they take them. This algorithm functions appropriately, yet like I claimed in the past, it feels like there need to be a means to do this without a loop. Exists?

2022-06-07 14:38:34
Source Share
Answers: 1

Once you have an angle you intend to revolve, you are attempting to lower it to the array $(-\pi,\pi)$. I do not recognize if Java will certainly do modulo on reals, neither what it returns if you do a% $\pi$ for $a \lt 0$. It resembles dAngle remains in the array $(-2\pi,2\pi)$ in which instance you can do it with if declarations:

if dAngle < - PI: hang+= 2PI

elseif hang > PI: hang - = 2PI


2022-06-07 14:54:23