We are about to switch to a new forum software. Until then we have removed the registration on this forum.

- All Categories 25.7K
- Announcements & Guidelines 13
- Common Questions 30
- Using Processing 22.1K
- Programming Questions 12.2K
- Questions about Code 6.4K
- How To... 4.2K
- Hello Processing 72
- GLSL / Shaders 292
- Library Questions 4K
- Hardware, Integration & Other Languages 2.7K
- Kinect 668
- Arduino 1K
- Raspberry PI 188
- Questions about Modes 2K
- Android Mode 1.3K
- JavaScript Mode 413
- Python Mode 205
- Questions about Tools 100
- Espanol 5
- Developing Processing 548
- Create & Announce Libraries 211
- Create & Announce Modes 19
- Create & Announce Tools 29
- Summer of Code 2018 93
- Rails Girls Summer of Code 2017 3
- Summer of Code 2017 49
- Summer of Code 2016 4
- Summer of Code 2015 40
- Summer of Code 2014 22
- p5.js 1.6K
- p5.js Programming Questions 947
- p5.js Library Questions 315
- p5.js Development Questions 31
- General 1.4K
- Events & Opportunities 288
- General Discussion 365

Hi. I'm trying to constrain an angle. , measured in radians. But radians takes negative values so constrain() method will not work.

So, i tried converting radians to angles, constrain degrees and convert to radians again. But the result is the same.

`a = radians(constrain(degrees(a), 90, 270));`

Any clue?

Tagged:

## Answers

Note: I'm using this to contrain arms an legs in a character. And code fails as soon as angle takes 180º value. It just jumps to 0 suddenly.

You mean this?

Thanks. Your code works just fine. I don't know why mine is failing!

See, this is the simplified version:

Try moving the yellow point to 180º

Sorry, I tried posting my code with "<"code">" tags but it's a mess. How do you do it?

...

Nevermind about code block posting. Indenting did the trick.

So, the problem with my code is this line, throwing negative numbers:

Not sure how should i do it without atan2

I'm not understanding what atan2 does. According to docs, it "Calculates the angle (in radians)...". But radians are not supposed to take negative numbers, am I right?:

While atan2 returns somthing else (don't know what)

Anyway, how can I get numbers like in the graphic?

atan2 returns an angle -PI to +PI or -180 to 180 degrees i.e

East = 0

South = PI/2

West = PI and -PI

North = 3*PI/2

to get convert this to 0-2

PI simple add 2PI if atan2 returns a negative value like thisThat's clever. Thanks.

If anyone (like me) ignores the negative values of radians, check this: http://www.dummies.com/how-to/content/negative-and-positive-angles-cutting-a-circle.html

There was a mistake in my previous code at line 9 I have corrected it. Sorry about that :)

I know, don't worry.

Just another question. Now it's easy to constrain value from 0 to 360 (thinking in degrees). But what if I need to constrain values from, let's say, 270 to 40? I tried setting 40º as 400 (360+40) but that didn't work. Same would happen with radian, going from 0 to 6.x right?

I'm a dummy. thanks for the link :)

Constraining an angle to a sub range such as 270-40 is much more challenging and it would be good to specify the solution requirements from which we can design a reusable function to do the job.

Function requirements1) The function requires three angles to be passed as parameters. The angle to constrain as well as the start and end angles for the range.

2) All 3 angles to be stored as radians (more efficient)

3) All 3 angles to be in the range 0 - 2π (i.e. 0 - 360 degrees)

4) Visualized on a circle the constraning-arc is the area swept by a line rotating clockwise from the range start angle to the range end angle.

5) if the range start angle is greater than the end angle it means the constraining-arc includes the angle 0 (zero)

6) If the angle is outside the constraining-arc it returns the nearest of the 2 range limits, otherwise it retuns the angle unchanged.

The following sketch demonstrates the function I have just described.