POV-Ray : Newsgroups : povray.newusers : Newbie question about functions Server Time
1 Jul 2024 00:05:04 EDT (-0400)
  Newbie question about functions (Message 1 to 9 of 9)  
From: TheBigH
Subject: Newbie question about functions
Date: 20 Dec 2010 01:55:01
Message: <web.4d0efc974d34203e5656816c0@news.povray.org>
Why does this not work? It complains that I need to initialize X and Y before I
call them in the macro Fracp, but since I am passing them as parameters to the
function Gridd this would obviously be inappropriate. I have also tried changing
the macro into another function, but that doesn't work either.


#declare thick=0.05;
#macro Fracp(A)
 A-floor(A)
#end
#declare Gridd = function( X, Y ) {
 #if ( Fracp(X)<thick | Fracp(X)>(1-thick) | Fracp(Y)<thick | Fracp(Y)>(1-thick)
)
   1
 #else
   0
#end
}


Cheers,
H.


Post a reply to this message

From: clipka
Subject: Re: Newbie question about functions
Date: 20 Dec 2010 05:06:46
Message: <4d0f2ab6$1@news.povray.org>
Am 20.12.2010 07:49, schrieb TheBigH:
> Why does this not work? It complains that I need to initialize X and Y before I
> call them in the macro Fracp, but since I am passing them as parameters to the
> function Gridd this would obviously be inappropriate. I have also tried changing
> the macro into another function, but that doesn't work either.
>
>
> #declare thick=0.05;
> #macro Fracp(A)
>   A-floor(A)
> #end
> #declare Gridd = function( X, Y ) {
>   #if ( Fracp(X)<thick | Fracp(X)>(1-thick) | Fracp(Y)<thick | Fracp(Y)>(1-thick)
> )
>     1
>   #else
>     0
> #end
> }

Note that "#" statements are always evaluated at parsing time, even when 
used in a function. Therefore, "#if" statements cannot be used to 
perform tests inside functions at run-time.

You will need to use the "select" function to have the condition 
evaluated each time the function is called.


Post a reply to this message

From: TheBigH
Subject: Re: Newbie question about functions
Date: 20 Dec 2010 07:25:00
Message: <web.4d0f4a45234b9e105656816c0@news.povray.org>
clipka <ano### [at] anonymousorg> wrote:
> Am 20.12.2010 07:49, schrieb TheBigH:
> > Why does this not work? It complains that I need to initialize X and Y before I
> > call them in the macro Fracp, but since I am passing them as parameters to the
> > function Gridd this would obviously be inappropriate. I have also tried changing
> > the macro into another function, but that doesn't work either.
> >
> >
> > #declare thick=0.05;
> > #macro Fracp(A)
> >   A-floor(A)
> > #end
> > #declare Gridd = function( X, Y ) {
> >   #if ( Fracp(X)<thick | Fracp(X)>(1-thick) | Fracp(Y)<thick | Fracp(Y)>(1-thick)
> > )
> >     1
> >   #else
> >     0
> > #end
> > }
>
> Note that "#" statements are always evaluated at parsing time, even when
> used in a function. Therefore, "#if" statements cannot be used to
> perform tests inside functions at run-time.
>
> You will need to use the "select" function to have the condition
> evaluated each time the function is called.

I see, thank for the heads up. I'm beginning to see that many things that are
straightforward and logical in other programming languages require awkward
contortions in POV-ray. Of course I could just do something like

#declare Gridd = function(X,Y) {
  (( X-floor(X)<thick )|(X-floor(X)>(1-thick))|( Y-floor(Y)<thick
)|(Y-floor(Y)>(1-thick)))
  }

because the function I want only takes the values zero and one, but if I ever
want other values, or more than two different values, I need proper selection of
cases. And I would definitely have preferred a nice orderly succession of #ifs
to the horror of nested selects. Oh well.


Post a reply to this message

From: Thorsten Froehlich
Subject: Re: Newbie question about functions
Date: 20 Dec 2010 08:00:00
Message: <web.4d0f52a6234b9e10e619b42c0@news.povray.org>
"TheBigH" <nomail@nomail> wrote:
> I see, thank for the heads up. I'm beginning to see that many things that are
> straightforward and logical in other programming languages require awkward
> contortions in POV-ray. Of course I could just do something like
>
> #declare Gridd = function(X,Y) {
>   (( X-floor(X)<thick )|(X-floor(X)>(1-thick))|( Y-floor(Y)<thick
> )|(Y-floor(Y)>(1-thick)))
>   }
>
> because the function I want only takes the values zero and one, but if I ever
> want other values, or more than two different values, I need proper selection of
> cases. And I would definitely have preferred a nice orderly succession of #ifs
> to the horror of nested selects. Oh well.

The real question you should ask yourself is why you need a function at all and
if another macro won't suffice. The primary use of functions is for objects that
require runtime evaluation, i.e. isosurfaces, and a binary selection is not the
best solution. Another user for functions is in user-defined patterns, where
your problem mightbbe much better solved using a map, i.e. a color_map.

Thorsten


Post a reply to this message

From: Sherry Shaw
Subject: Re: Newbie question about functions
Date: 20 Dec 2010 11:05:44
Message: <4d0f7ed8$1@news.povray.org>
And a couple of additional trivial thoughts...

(1) Expressions of the type "x * (x > someDangNumber)" can be used in 
functions--might something like this be useful?

(2) I notice that "X" and "Y" are capitalized--might this be a problem?

--Sherry Shaw

-- 
#macro T(E,N)sphere{x,.4rotate z*E*60translate y*N pigment{wrinkles scale
.3}finish{ambient 1}}#end#local I=0;#while(I<5)T(I,1)T(1-I,-1)#local I=I+
1;#end camera{location-5*z}plane{z,37 pigment{granite color_map{[.7rgb 0]
[1rgb 1]}}finish{ambient 2}}//                                   TenMoons


Post a reply to this message

From: Warp
Subject: Re: Newbie question about functions
Date: 20 Dec 2010 11:30:43
Message: <4d0f84b3@news.povray.org>
Sherry Shaw <ten### [at] aolcom> wrote:
> (2) I notice that "X" and "Y" are capitalized--might this be a problem?

  You can name function parameters as you like.

-- 
                                                          - Warp


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: Newbie question about functions
Date: 20 Dec 2010 14:20:00
Message: <web.4d0fab97234b9e1047ba06bb0@news.povray.org>
Warp <war### [at] tagpovrayorg> wrote:
> Sherry Shaw <ten### [at] aolcom> wrote:
> > (2) I notice that "X" and "Y" are capitalized--might this be a problem?
>
>   You can name function parameters as you like.

Are you sure ?

I do not have access to POV-Ray right now to check, but IIRC one (or more) is
problematic to use; like "t".

--
Tor Olav
http://subcube.com


Post a reply to this message

From: Warp
Subject: Re: Newbie question about functions
Date: 20 Dec 2010 14:28:31
Message: <4d0fae5e@news.povray.org>
Tor Olav Kristensen <tor### [at] toberemovedgmailcom> wrote:
> Warp <war### [at] tagpovrayorg> wrote:
> > Sherry Shaw <ten### [at] aolcom> wrote:
> > > (2) I notice that "X" and "Y" are capitalized--might this be a problem?
> >
> >   You can name function parameters as you like.

> Are you sure ?

> I do not have access to POV-Ray right now to check, but IIRC one (or more) is
> problematic to use; like "t".

  Ok, 't' is special for some reason. But eg. 'T' is fine.

-- 
                                                          - Warp


Post a reply to this message

From: Sherry Shaw
Subject: Re: Newbie question about functions
Date: 20 Dec 2010 16:19:07
Message: <4d0fc84b$1@news.povray.org>
Warp wrote:
> 
>   You can name function parameters as you like.
> 

Just wondered if perhaps he was using the function in a context where x 
and y might be expected (as in an isosurface, for example), as opposed 
to explicitly passing two parameters.

--Sherry Shaw

-- 
#macro T(E,N)sphere{x,.4rotate z*E*60translate y*N pigment{wrinkles scale
.3}finish{ambient 1}}#end#local I=0;#while(I<5)T(I,1)T(1-I,-1)#local I=I+
1;#end camera{location-5*z}plane{z,37 pigment{granite color_map{[.7rgb 0]
[1rgb 1]}}finish{ambient 2}}//                                   TenMoons


Post a reply to this message

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