POV-Ray : Newsgroups : povray.text.scene-files : My first #macro Server Time
26 Dec 2024 12:54:06 EST (-0500)
  My first #macro (Message 1 to 1 of 1)  
From: Nieminen Mika
Subject: My first #macro
Date: 24 Jan 1999 05:14:40
Message: <36aaf290.0@news.povray.org>
Just started to play with #macros. They surely are handy!
	This is my first "serious" macro. It's far from complete, but
	at least you can do something with it.

#macro Function(MinX,MaxX,MinY,MaxY,GridX,GridY)
  #ifndef(f) #macro f(X,Y) 0 #end #end
  #local FVal=array[GridX+1][GridY+1]
  #local StepX=(MaxX-MinX)/GridX;
  #local StepY=(MaxY-MinY)/GridY;
  #local IndY=0;
  #while(IndY<=GridY)
    #local PosY=MinY+StepY*IndY;
    #local IndX=0;
    #while(IndX<=GridX)
      #local PosX=MinX+StepX*IndX;
      #local FVal[IndX][IndY]=f(PosX,PosY);
      #local IndX=IndX+1;
    #end
    #local IndY=IndY+1;
  #end
  #ifndef(CylinderRadius) #local CylinderRadius=.1; #end
  #local IndY=0;
  #while(IndY<=GridY)
    #local PosY=MinY+StepY*IndY;
    #local IndX=0;
    #while(IndX<=GridX)
      #local PosX=MinX+StepX*IndX;
      #if(IndX>0)
        cylinder { <PosX,PosY,FVal[IndX][IndY]>,
                   <PosX-StepX,PosY,FVal[IndX-1][IndY]>,CylinderRadius }
      #end
      #if(IndY>0)
        cylinder { <PosX,PosY,FVal[IndX][IndY]>,
                   <PosX,PosY-StepY,FVal[IndX][IndY-1]>,CylinderRadius }
      #end
      #local IndX=IndX+1;
    #end
    #local IndY=IndY+1;
  #end
#end


	  This macro calculates a mesh grid made of cylinders from another
	macro called f which have two parameters, ie. a 2-dimensional
	function.
	  This is an example of how to use it (supposing the macro is
	saved in a file called 'function.mcr'):

#include "function.mcr"
 
light_source { <100,200,-150> 1 }
 
camera { location <-5,15,-15>*2 look_at 0 angle 35 }
 
#macro f(X,Y) sin(sqrt(X*X+Y*Y)-2*pi*clock) #end
union
{ Function(-10,10,-10,10,30,30) rotate x*90
  pigment { rgb <1,0,0> } finish { specular .5 }
}


	  This makes a very nice animation (of course you don't have to
	make an animation from it; the default still image (with clock=0) is
	also cute). I posted a sample animation to povray.binaries.animations.
	  The radius of the cylinders can be specified declaring the
	identifier CylinderRadius.
	  And to show the power of #macros, here is another example:

#include "function.mcr"
 
light_source { <100,200,-150> 1 }
 
camera { location <-5,15,-15>*.3 look_at -x*.5 angle 35 }
 
#macro f(X,Y)
  #local Zr=PosX; #local Zi=PosY;
  #local n=0; #local a=0; #local b=0;
  #while(n<=20 & a+b<4)
    #local a=Zr*Zr; #local b=Zi*Zi;
    #local Zi=2*Zr*Zi+PosY; #local Zr=a-b+PosX;
    #local n=n+1;
  #end
  (n-1)/40
#end
 
#declare CylinderRadius=.015;
union
{ Function(-2.2,1,-1.5,1.5,50,50) rotate -x*90
  pigment
  { gradient y color_map { [0 rgb<.3,0,0>][.5 rgb<1,0,0>][1 rgb<1,0,0>] }
  }
  finish { specular .5 }
}


-- 
main(i){char*_="BdsyFBThhHFBThhHFRz]NFTITQF|DJIFHQhhF";while(i=
*_++)for(;i>1;printf("%s",i-70?i&1?"[]":" ":(i=0,"\n")),i/=2);} /*- Warp. -*/


Post a reply to this message

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