Snowman
Snowman

Reputation: 32071

Limiting value of angle in objective c

I have an angle defined as such:

 accelerationX = acceleration.x * kFilteringFactor + accelerationX * (1.0 - kFilteringFactor);
 accelerationY = acceleration.y * kFilteringFactor + accelerationY * (1.0 - kFilteringFactor);
 double angle = atan2(accelerationY, -accelerationX) + M_PI/2;

I want to limit the value of angle to minimum -pi/3 and max pi/3. So if the value goes over pi/3, it should just stay at pi/3. How can I do this?

Upvotes: 0

Views: 131

Answers (2)

andyvn22
andyvn22

Reputation: 14824

if (angle < M_PI/-3)
{
    angle = M_PI/-3;
}
else if (angle > M_PI/3)
{
    angle = M_PI/3;
}

Upvotes: 1

Simon
Simon

Reputation: 32893

You can add:

angle = (angle < -M_PI/3 ? -M_PI/3 : (angle > M_PI/3 ? M_PI/3 : angle));

Upvotes: 1

Related Questions