POV-Ray : Newsgroups : povray.general : Function: Problem in Defining : Re: Function: Problem in Defining Server Time
23 Apr 2024 17:51:43 EDT (-0400)
  Re: Function: Problem in Defining  
From: clipka
Date: 5 Apr 2018 11:36:22
Message: <5ac64276$1@news.povray.org>
Am 05.04.2018 um 16:21 schrieb Sven Littkowski:
> Hi,
> 
> I try to create (for the very first time) an own function, after i saw
> it inside the POV-Ray online help files. Well, i do something wrong.
> 
> Basically, the purpose of my function is to create a negative or
> positive float number, that I then can use to rotate something clockwise
> or counter-clockwise. At the moment, it is my brain that is rotating
> counter-clockwise.

You can NOT use directives (such as #if..#else..#end) in functions.

Well, technically you can, but they do something entirely different than
one would naively expect - because they are evaluated only once, when
the function is initially defined.

For example, the scene code

    #declare MySeed = seed(4711);
    ...
    #declare MyFn = function(x)
    {
      #if(rand(MySeed) > 0.5)
        x
      #else
        -x
      #end
    }

does NOT construct a function that randomly returns its parameter or the
negative thereof, but rather

randomly constructs EITHER a function that returns its parameter, OR a
function that returns the negative of the parameter.

In other words, it is completely equivalent to:

    #declare MySeed = seed(4711);
    ...
    #if(rand(MySeed) > 0.5)
      #declare MyFn = function(x)
      {
        x
      }
    #else
      #declare MyFn = function(x)
      {
        -x
      }
    #end


Likewise, your scene code

> #declare Number = function(MyNumber)
> {
>  #declare MyAngle = rand(NySeed)*MyNumber;
>  #declare MyPositive = rand(MySeed)*1.0;
>  #if(MyPositive<0.5)
>   MyAngle*(+1) // no #declare and no ;
>  #else
>   MyAngle*(-1) // no #declare and no ;
>  #end
> }

is completely equivalent to

    #declare MyAngle = rand(NySeed)*MyNumber;
    #declare MyPositive = rand(MySeed)*1.0;
    #if(MyPositive<0.5)
      #declare Number = function(MyNumber)
      {
       MyAngle*(+1) // no #declare and no ;
      }
    #else
      #declare Number = function(MyNumber)
      {
       MyAngle*(-1) // no #declare and no ;
      }
    #end

(which is broken in multiple ways)


Post a reply to this message

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