POV-Ray : Newsgroups : povray.advanced-users : Calculating planet positions : Re: Calculating planet positions Server Time
25 Apr 2024 04:44:18 EDT (-0400)
  Re: Calculating planet positions  
From: clipka
Date: 15 Sep 2018 19:03:19
Message: <5b9d8fb7$1@news.povray.org>
Am 15.09.2018 um 22:12 schrieb Bald Eagle:

>> "Modulos the mean anomaly so that -180 <= M <= +180 ..."
...

> I do believe that this is typically done with a macro, and occurs in the POV-Ray
> source code.
> 
> #macro Modulate (_X)
>      #while (_X < 180) #local _X = _X+360; #end
>      #While (_X > 180) #local _X = _X-360; #end
>      _X
> #end


That would be way too time-consuming for very large or very small values.


> Then of course, there's
> http://www.povray.org/documentation/view/3.6.2/458/
> 
> clamp(V, Min, Max). A function that limits a value to a specific range, if it
> goes outside that range it is "clamped" to this range, wrapping around. As the
> input increases or decreases outside the given range, the output will repeatedly
> sweep through that range, making a "sawtooth" waveform.
> Parameters:

That'll indeed do it:

    #include "math.inc"
    #local Foo = clamp(M,-180,180);


Alternatively:

    #local Foo = mod(M+180,360)-180;
    #if (Foo < -180)
      #local Foo = Foo + 260;
    #end

The post-processing `#if` branch is necessary because mod(X,Y) wraps
positive values into the range [0..Y), but negative values into the
range (-Y..0].

[At least on platforms where converting a floating-point number to an
integer rounds towards 0. That's the case for all contemporary platform
I'm aware of, and is an official prerequisite for compiling POV-Ray, but
the C++ standard would also allow for rounding towards negative infinity
instead.]

`clamp()` effectively does the same, but is implemented as a function,
which probably makes it faster than a macro or "in-line" code.


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.