POV-Ray : Newsgroups : povray.general : Function: Problem in Defining Server Time: 23 Mar 2019 06:29:49 GMT
 Function: Problem in Defining (Message 1 to 10 of 16)
 From: Sven Littkowski Subject: Function: Problem in Defining Date: 5 Apr 2018 14:21:21 Message: <5ac630e1\$1@news.povray.org>
```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.

Can you assist?

---------------------

#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
}

rotate < 0.0, Number(1.2), 0.0 >

ORIGINAL

#declare factorial = function(C)
{
prod(i, 1, C, i) // no #declare and no ;
}
#declare A = factorial(5);

---
Diese E-Mail wurde von AVG auf Viren geprÃ¼ft.
http://www.avg.com
```
 From: clipka Subject: Re: Function: Problem in Defining Date: 5 Apr 2018 15: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

> #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)
```
 From: Sven Littkowski Subject: Re: Function: Problem in Defining Date: 5 Apr 2018 15:57:34 Message: <5ac6476e\$1@news.povray.org>
```Thanks. Yes, I assumed that there are many errors in my function. But I
am still not sure, how to develop a code that does what I need to be done.

See at the very bottom (end) of this posting an excerpt of the scene
where it is important for me to create random numbers "right at that
spot" (Number(1.2) as otherwise too many extra lines of code would be
required.

How can I create random numbers, in the range from negative to positive,
within each line?

-------------------------

box { < 0.11,  0.0, -0.00 > < 2.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -4.0, 0.4, -7.50 > scale <
1.0, 1.0, -1.0 > } // Horizontal Planks Boiler Area
box { < 0.11,  0.0, -0.00 > < 2.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -4.0, 0.4, -7.50 > scale <
1.0, 1.0,  1.0 > } // Horizontal Planks
box { < 0.11,  0.0, -0.00 > < 2.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -4.0, 0.9, -7.50 > scale <
1.0, 1.0, -1.0 > } // Horizontal Planks
box { < 0.11,  0.0, -0.00 > < 2.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -4.0, 0.9, -7.50 > scale <
1.0, 1.0,  1.0 > } // Horizontal Planks
box { < 0.11,  0.0, -0.00 > < 2.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -4.0, 1.4, -7.50 > scale <
1.0, 1.0, -1.0 > } // Horizontal Planks
box { < 0.11,  0.0, -0.00 > < 2.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -4.0, 1.4, -7.50 > scale <
1.0, 1.0,  1.0 > } // Horizontal Planks
box { < 0.11,  0.0, -0.00 > < 2.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -4.0, 1.9, -7.50 > scale <
1.0, 1.0, -1.0 > } // Horizontal Planks
box { < 0.11,  0.0, -0.00 > < 2.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -4.0, 1.9, -7.50 > scale <
1.0, 1.0,  1.0 > } // Horizontal Planks
box { < 0.11,  0.0, -0.00 > < 2.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -4.0, 2.4, -7.50 > scale <
1.0, 1.0, -1.0 > } // Horizontal Planks
box { < 0.11,  0.0, -0.00 > < 2.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -4.0, 2.4, -7.50 > scale <
1.0, 1.0,  1.0 > } // Horizontal Planks
box { < 0.11,  0.0, -0.00 > < 2.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -4.0, 2.9, -7.50 > scale <
1.0, 1.0, -1.0 > } // Horizontal Planks
box { < 0.11,  0.0, -0.00 > < 2.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -4.0, 2.9, -7.50 > scale <
1.0, 1.0,  1.0 > } // Horizontal Planks
box { < 0.11,  0.0, -0.00 > < 2.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -4.0, 3.4, -7.50 > scale <
1.0, 1.0, -1.0 > } // Horizontal Planks
box { < 0.11,  0.0, -0.00 > < 2.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -4.0, 3.4, -7.50 > scale <
1.0, 1.0,  1.0 > } // Horizontal Planks
box { < 0.11,  0.0, -0.00 > < 2.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -4.0, 3.9, -7.50 > scale <
1.0, 1.0, -1.0 > } // Horizontal Planks
box { < 0.11,  0.0, -0.00 > < 2.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -4.0, 3.9, -7.50 > scale <
1.0, 1.0,  1.0 > } // Horizontal Planks
box { < 0.11,  0.0, -0.00 > < 2.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -4.0, 4.4, -7.50 > scale <
1.0, 1.0, -1.0 > } // Horizontal Planks
box { < 0.11,  0.0, -0.00 > < 2.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -4.0, 4.4, -7.50 > scale <
1.0, 1.0,  1.0 > } // Horizontal Planks

box { < 0.01,  0.0, -0.00 > < 1.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -1.0, 0.4, -7.50 > scale <
1.0, 1.0, -1.0 > } // Horizontal Planks
box { < 0.01,  0.0, -0.00 > < 1.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -1.0, 0.4, -7.50 > scale <
1.0, 1.0,  1.0 > } // Horizontal Planks
box { < 0.01,  0.0, -0.00 > < 1.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -1.0, 0.9, -7.50 > scale <
1.0, 1.0, -1.0 > } // Horizontal Planks
box { < 0.01,  0.0, -0.00 > < 1.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -1.0, 0.9, -7.50 > scale <
1.0, 1.0,  1.0 > } // Horizontal Planks
box { < 0.01,  0.0, -0.00 > < 1.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -1.0, 1.4, -7.50 > scale <
1.0, 1.0, -1.0 > } // Horizontal Planks
box { < 0.01,  0.0, -0.00 > < 1.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -1.0, 1.4, -7.50 > scale <
1.0, 1.0,  1.0 > } // Horizontal Planks
box { < 0.01,  0.0, -0.00 > < 1.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -1.0, 1.9, -7.50 > scale <
1.0, 1.0, -1.0 > } // Horizontal Planks
box { < 0.01,  0.0, -0.00 > < 1.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -1.0, 1.9, -7.50 > scale <
1.0, 1.0,  1.0 > } // Horizontal Planks
box { < 0.01,  0.0, -0.00 > < 1.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -1.0, 2.4, -7.50 > scale <
1.0, 1.0, -1.0 > } // Horizontal Planks
box { < 0.01,  0.0, -0.00 > < 1.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -1.0, 2.4, -7.50 > scale <
1.0, 1.0,  1.0 > } // Horizontal Planks
box { < 0.01,  0.0, -0.00 > < 1.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -1.0, 2.9, -7.50 > scale <
1.0, 1.0, -1.0 > } // Horizontal Planks
box { < 0.01,  0.0, -0.00 > < 1.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -1.0, 2.9, -7.50 > scale <
1.0, 1.0,  1.0 > } // Horizontal Planks
box { < 0.01,  0.0, -0.00 > < 1.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -1.0, 3.4, -7.50 > scale <
1.0, 1.0, -1.0 > } // Horizontal Planks
box { < 0.01,  0.0, -0.00 > < 1.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -1.0, 3.4, -7.50 > scale <
1.0, 1.0,  1.0 > } // Horizontal Planks
box { < 0.01,  0.0, -0.00 > < 1.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -1.0, 3.9, -7.50 > scale <
1.0, 1.0, -1.0 > } // Horizontal Planks
box { < 0.01,  0.0, -0.00 > < 1.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -1.0, 3.9, -7.50 > scale <
1.0, 1.0,  1.0 > } // Horizontal Planks
box { < 0.01,  0.0, -0.00 > < 1.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -1.0, 4.4, -7.50 > scale <
1.0, 1.0, -1.0 > } // Horizontal Planks
box { < 0.01,  0.0, -0.00 > < 1.99, 0.3, -0.05 > texture { MyYellow }
rotate <  0.0, 0.0, Number(1.2) > translate < -1.0, 4.4, -7.50 > scale <
1.0, 1.0,  1.0 > } // Horizontal Planks

---
Diese E-Mail wurde von AVG auf Viren geprÃ¼ft.
http://www.avg.com
```
 From: clipka Subject: Re: Function: Problem in Defining Date: 5 Apr 2018 16:21:25 Message: <5ac64d05\$1@news.povray.org>
```Am 05.04.2018 um 17:57 schrieb Sven Littkowski:
> Thanks. Yes, I assumed that there are many errors in my function. But I
> am still not sure, how to develop a code that does what I need to be done.

You can't do it with a function, because `rand()` cannot be called from
within a function.

However, since you're only using it to generate a scene (as opposed to
using it for something where it would have to be evaluated during
rendering, such asn in an isosurfaces or function pattern), the good
news is that you don't need a function anyway - a macro will do nicely.

For example, to define a "pseudo-function" that gives you a random
number in a specific range, you might use:

#declare MySeed = seed(4711);
#macro MyRand(Min,Max)
// compute stuff
#local Rand = rand(MySeed);
#local Result = Min + (Max-Min)*Rand;
// "return" the computed value
Result
#end
```
 From: Bald Eagle Subject: Re: Function: Problem in Defining Date: 5 Apr 2018 16:35:00 Message:
```> #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
> }
>
> rotate < 0.0, Number(1.2), 0.0 >

What you're doing is not a function, but a macro.

A function operates like this:

#declare PosOrNeg = function (N) {select(N, -1, 1)};
#declare Seed = seed(123);

#for (Num, 1, 10)
#declare N = rand (Seed)-0.5;
#debug concat( " Result = ", str(PosOrNeg(N), 3, 1), "\n")
#end

You can then incorporate that into a calculation to give you an angle multiplied
by 1 or -1.
```
 From: Bald Eagle Subject: Re: Function: Problem in Defining Date: 5 Apr 2018 21:00:00 Message:
```And you don't even really need a separate function or macro - just use

#include "rand.inc"

then use (IIRC)

RRand (0, 360)

or

RRand (-180, 180) to give you your rotation angles directly.
```
 From: Sven Littkowski Subject: Re: Function: Problem in Defining Date: 5 Apr 2018 22:40:59 Message: <5ac6a5fb\$1@news.povray.org>
```Tttttsssssssssssssssssssssssss.

That is insider knowledge! Thanks! I would never have discovered this
option on my own. Giving a try.   :-)

---
Diese E-Mail wurde von AVG auf Viren geprÃ¼ft.
http://www.avg.com
```
 From: dick balaska Subject: Re: Function: Problem in Defining Date: 6 Apr 2018 01:02:06 Message: <5ac6c70e\$1@news.povray.org>
```On 04/05/2018 12:21 PM, clipka wrote:

>      #declare MySeed = seed(4711);
>      #macro MyRand(Min,Max)
>        // compute stuff
>        #local Rand = rand(MySeed);
>        #local Result = Min + (Max-Min)*Rand;
>        // "return" the computed value
>        Result
>      #end
>

I wrap the return value in parenthesis. The intent is a little clearer
in the macro def and may avoid usage problems.

// "return" the computed value
(Result)

I know clipka, legally this is not required, because Result is a single
number. But I tend to do

#local V=<1,2,3>;
// "return" the computed value
(Result+V)

where it is morally (debuggingly?) required.

sven, this is because when you use it, think of the expansion.

#local R = MyRand(low, high) * 2;

which without parens, is
#local R = Result + V * 2

which of course evaluates as
#local R = Result + (V * 2)

which is not at all what you want.  With the parens, you get
#local R = (Result + V) * 2

which is the intent of your usage.

--
dik
Rendered 328976 of 330000 (99%)
```
 From: Thomas de Groot Subject: Re: Function: Problem in Defining Date: 6 Apr 2018 06:47:16 Message: <5ac717f4\$1@news.povray.org>
```On 5-4-2018 22:57, Bald Eagle wrote:
> And you don't even really need a separate function or macro - just use
>
> #include "rand.inc"
>
> then use (IIRC)
>
> RRand (0, 360)
>
> or
>
> RRand (-180, 180) to give you your rotation angles directly.
>

To be correct:

#declare R=seed(1234);

RRand (-180, 180, R)
```
 From: Thomas de Groot Subject: Re: Function: Problem in Defining Date: 6 Apr 2018 07:32:37 Message: <5ac72295@news.povray.org>
```On 6-4-2018 0:41, Sven Littkowski wrote:
> Tttttsssssssssssssssssssssssss.
>
> That is insider knowledge! Thanks! I would never have discovered this
> option on my own. Giving a try.   :-)
>

Everybody can discover it. No "insider knowledge" here at all imo. When
you need randomness, you naturally go and look at the content of
rand.inc and its description in the docs. Nothing arcane involved.

--
Thomas
```