Oliver Dixon
Oliver Dixon

Reputation: 7405

Splitting up an angle between two degree values

I have two angles given to me; a starting one and an ending one. I'm also in a loop with a specified numbers of loops.

I'm trying to split an angle up so at each loop iteration I create something at that angle.

Using 3 particles as an example

Here is the code (within in the loop, degrees are 90 to 180)

for (int i = 0; i < numberOfParticles(3); i++)
{
    float percentage = 1f / numberOfParticles;
    percentage *= index;
    float angle = startingAngle + ((endingAngle - startingAngle) * percentage);
}

My problem is this produces : (instead of 90 (0), 135 (0.5), 180 (1))

Log: 90.0 | percentage: 0.0
Log: 120.0 | percentage: 0.33333334
Log: 149.99998 | percentage: 0.6666667

How would I get this to work with any number (including 7?)

Upvotes: 0

Views: 58

Answers (1)

John Kugelman
John Kugelman

Reputation: 361564

You've got an off-by-one error. If you have 3 particles, you're going to start at 0% and then add 50% 2 times, not 3.

float percentage = 1f / (numberOfParticles - 1);

Make sure you also handle the edge case where numberOfParticles is 1. You don't want to divide by zero.

Upvotes: 1

Related Questions