POV-Ray : Newsgroups : povray.general : HELP Using macro to visualise mathematical functions Server Time
5 Nov 2024 20:18:34 EST (-0500)
  HELP Using macro to visualise mathematical functions (Message 1 to 6 of 6)  
From: The Ugly
Subject: HELP Using macro to visualise mathematical functions
Date: 30 Nov 2004 12:30:01
Message: <web.41acadff91225dd755f15cf90@news.povray.org>
I would like to make a macro that visualises mathematical functions.
Something like this would be nice.




#macro MyFunc(Length,Formula)

#local N=0;

#while (N<=Length)

sphere { 0, 0.02
        pigment { rgb <N,0,N*-1+1> }
        translate <N,Formula,0>
        }

#local N=N+0.1;

#end

#end




But as you might see this is of little use.
If this is to become usefull you have to be able to use it like this:

MyFunc(N*N)


In other words, you will have to be able to refere to the local N when you
call it, but it hasnt been defined yet.
If you try to define it before you call the macro it will calculate the
value before passing it into the macro.
Do I make any sense?

Is there any way you could get around this limitation?


Robb


Post a reply to this message

From: Thorsten Froehlich
Subject: Re: HELP Using macro to visualise mathematical functions
Date: 30 Nov 2004 13:04:57
Message: <41acb649$1@news.povray.org>
In article <web.41acadff91225dd755f15cf90@news.povray.org> , "The Ugly" 
<nomail@nomail> wrote:

> But as you might see this is of little use.
> If this is to become usefull you have to be able to use it like this:
>
> MyFunc(N*N)

See <http://www.povray.org/documentation/view/3.6.1/231/>, in particular
2.2.1.6.2 Functions and Macros.

    Thorsten


Post a reply to this message

From: Shay
Subject: Re: HELP Using macro to visualise mathematical functions
Date: 30 Nov 2004 13:34:26
Message: <41acbd32$1@news.povray.org>
The Ugly wrote:

> #macro MyFunc(Length,Formula)
> 
> <SNIP>
> 
> But as you might see this is of little use.
> If this is to become usefull you have to be able to use it like this:
> 
> MyFunc(N*N)
> 
> In other words, you will have to be able to refere to the local N
> when you call it, but it hasnt been defined yet. If you try to define
> it before you call the macro it will calculate the value before passing
> it into the macro.
>
> Do I make any sense?

No, you do not. Your MyFunc macro takes two parameters. What is 'N*N' in 
MyFunc(N*N)? Are you trying to pass this as a 'Length' or a 'Formula'?

Looks like your 'Formula' just returns a 'y' value, so this might get 
you started:

#macro MyFunc(Length,Formula)
   #local N=0;
   #while (N<=Length)
     sphere { 0, 0.02
       pigment { rgb <N,0,N*-1+1> }
       translate <N,Formula(N),0> // this line is changed
     }
     #local N=N+0.1;
   #end
#end

MyFunc ( 10, function(x){x*x} )

// or try
// union{ MyFunc( 10, function(x){sin(x)} ) translate <-5,-3,0>}
// union{ MyFunc( 10, function(x){cos(x)} ) translate <-5,0,0>}
// union{ MyFunc( 10, function(x){sin(cos(x))} ) translate <-5,3,0>}

camera {
	location <0,0,-10>
	look_at 0
}

  -Shay


Post a reply to this message

From: Mike Williams
Subject: Re: HELP Using macro to visualise mathematical functions
Date: 30 Nov 2004 13:49:07
Message: <uCYjsDAMCMrBFwjX@econym.demon.co.uk>
Wasn't it The Ugly who wrote:
>I would like to make a macro that visualises mathematical functions.
>Something like this would be nice.
>
>
>
>
>#macro MyFunc(Length,Formula)
>
>#local N=0;
>
>#while (N<=Length)
>
>sphere { 0, 0.02
>        pigment { rgb <N,0,N*-1+1> }
>        translate <N,Formula,0>
>        }
>
>#local N=N+0.1;
>
>#end
>
>#end


You've just made one tiny syntax error in that macro. 
Here's a complete scene that works.

camera {location  <5,0,-20> look_at <5,0,0> angle 30}
background {rgb 1}
light_source {<-30, 100, -30> color rgb 1}

#macro MyFunc(Length,Formula)
 #local N=0;
 #while (N<=Length)
  sphere { 0, 0.02
    pigment { rgb <N,0,N*-1+1> }
    translate <N,Formula(N),0>          // I changed this line
  }
  #local N=N+0.1;
 #end
#end

// Call it with length and one-dimensional function
MyFunc(10, function(n){sin(n)} )

-- 
Mike Williams
Gentleman of Leisure


Post a reply to this message

From: The Ugly
Subject: Re: HELP Using macro to visualise mathematical functions
Date: 30 Nov 2004 14:25:01
Message: <web.41acc8c098a169fb55f15cf90@news.povray.org>
Sorry, Im quite new to POV-Ray, and I know that I should read the
Documentation more. But I dont live in an English speaking country and Im
only 14 years old, all these mathematical terms can be quite hard to
undertand (lame excuse :p ).

Anyway, I made some last minute modifications before i posted the original
letter and I forgot to change "MyFunc(N*N)" to "MyFunc(1,N*N)" (or
whatever).

Thanks for the code Shay and Mike...this function-thing will simplify a lot
of my other scenes.

Just to set things straight: There is no way to sort of build in the
"function(n) { ... }" bit into the macro so that the user just has to type
"X*X" instead of "function(x) { x*x }" when calling it?


Post a reply to this message

From: Shay
Subject: Re: HELP Using macro to visualise mathematical functions
Date: 30 Nov 2004 15:09:48
Message: <41acd38c$1@news.povray.org>
The Ugly wrote:

> 
> Just to set things straight: There is no way to sort of build in the
> "function(n) { ... }" bit into the macro so that the user just has to type
> "X*X" instead of "function(x) { x*x }" when calling it?
> 
> 

There is, but the method is a bit ugly. There is a macro in
"strings.inc" called Parse_String. You can see how it works in the docs,
but in effect it turns a string (which can be passed as a macro
parameter) into the code inside the string. You can use it like this:

#include "strings.inc"
#local Formula = "x*x"; // notice that this is a string
#local TempFunction = function(x){ Parse_String(Formula) }

Another thing to watch out for is that function names cannot be reused
unless they are undefined first. So, if your macro is called more than
once, you will need to make sure that the declaration from the first
time is undeclared. This looks like this in POV-Ray:

#ifdef ( TempFunction )
   #undef TempFunction
#end

Now the name TempFunction can be used again.

I'll let you figure out how to rewrite the macro. Let us know if you
need any help. Remember that the macro call will need the Formula as a
string. That is: MyFunc(1,"x*x")

  -Shay


Post a reply to this message

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