Pikchr

Modulo-Angle
Login

Modulo-Angle

(1) By Ryan (RyanSmith) on 2022-07-28 13:05:59 [link] [source]

With some images it's not possible to know the range of angles they will experience during define time.

My request is to please make Angle parameters work modulo 360 (Euclidian).

In most script-graphics engines (all of the ones I've worked with) it is common to accept any angle and apply modulo 360 to it in that 370 degrees simply means 10 degrees, -20 degrees simply means 340 degrees and similarly 10,000 degrees simply means 10000 % 360 = 280 degrees.

If one defines a Macro to draw a little arrow or flag, for instance, it's convenient to say:

    CP: dot invisible;

    define flag { 
        line go 1.5 heading $flagAngle \
        then go 0.5 heading $flagAngle + 110 \
        then go 0.5 heading $flagAngle + 250;
        move to CP;
    }
    $flagAngle = 00;  flag;
    $flagAngle = 30;  flag;
    $flagAngle = 60;  flag;

Which works beautifully for any $flagAngle up to 110, but on 111 it fails with an error because obviously the last leg overflows 360deg, which technically should not be a problem, and there is no way for the programmer to modulo 360 the value, nor any "if $angle < 0 ..." type program-flow. (At least not any way evident in the docs)

I just can't figure out how to reliably increase or decrease an angle and avoid errors without range-checking or modulo ability.

(2) By drh on 2022-07-28 14:42:20 in reply to 1 [link] [source]

(3) By Ryan (RyanSmith) on 2022-07-28 22:55:32 in reply to 2 [link] [source]

Thank you very much! Seeing how I can leverage this newfound abilities as we speak.

(4) By Zellyn Hunter (zellyn) on 2022-12-15 02:51:20 in reply to 2 [link] [source]

Not sure how to comment on a check-in, so I'll comment here. The check-in comment says “Convert it to 0..360 before use.”

It actually converts it to (-360..360): fmod's result takes the sign of the first argument.

(5) By Stephan Beal (stephan) on 2022-12-15 03:12:36 in reply to 4 [source]

Not sure how to comment on a check-in, so I'll comment here.

Just FYI: Fossil doesn't have the ability to comment directly on a checkin - this is the right place to do so.