<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.povray.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Zemoxian</id>
	<title>POV-Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.povray.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Zemoxian"/>
	<link rel="alternate" type="text/html" href="https://wiki.povray.org/content/Special:Contributions/Zemoxian"/>
	<updated>2026-04-22T12:14:44Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.35.1</generator>
	<entry>
		<id>https://wiki.povray.org/content?title=HowTo:Use_macros_and_loops&amp;diff=485</id>
		<title>HowTo:Use macros and loops</title>
		<link rel="alternate" type="text/html" href="https://wiki.povray.org/content?title=HowTo:Use_macros_and_loops&amp;diff=485"/>
		<updated>2008-04-01T03:52:13Z</updated>

		<summary type="html">&lt;p&gt;Zemoxian: Added a macro example that expands on the previous loop example&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Loops and Macros are usually created to do one set of code thousands of times. The beauty of the loop is that the code can reference an array or a spline to write a rollercoaster track or a roadway such that very complicated scenes can be created with the use of simple declared variables.&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
&lt;br /&gt;
Loops may be constructed through the use of the #while statement. A typical loop may be constructed as follows.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;pov&amp;quot;&amp;gt;&lt;br /&gt;
#declare step = 0;&lt;br /&gt;
#declare steps = 100;&lt;br /&gt;
#while (step &amp;lt;= steps)&lt;br /&gt;
  #sphere { step*x, 0.4 pigment {Blue} }&lt;br /&gt;
  #declare step = step + 1;&lt;br /&gt;
#end&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Nested loops may be created to generate a two or three dimensional aspect to the scene. Beware that creating extra nested loops will dramatically increase the parse time for the scene.&lt;br /&gt;
&lt;br /&gt;
The following nested loop will draw a series of Blue balls on the XZ plain&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;pov&amp;quot;&amp;gt;&lt;br /&gt;
#declare xpos = 0;&lt;br /&gt;
#declare zpos = 0;&lt;br /&gt;
#declare xfinal = 10;&lt;br /&gt;
#declare zfinal = 10;&lt;br /&gt;
#while (xpos &amp;lt;= xfinal)&lt;br /&gt;
  #while (zpos &amp;lt;= zfinal)&lt;br /&gt;
    #sphere { &amp;lt;xpos,0,zpos&amp;gt; 0.4 pigment {Blue} }&lt;br /&gt;
    #declare zpos = zpos + 1;&lt;br /&gt;
  #end&lt;br /&gt;
  #declare xpos = xpos + 1;&lt;br /&gt;
  #declare zpos = 0;&lt;br /&gt;
#end&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Macros ==&lt;br /&gt;
The essential custom functions of PoV-Ray. These are blocks of code that make calculations, or make objects, or if used in a series can make many objects with all of them different. &lt;br /&gt;
&lt;br /&gt;
Macros are declared like this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;pov&amp;quot;&amp;gt;#macro {Macro_Name}({Macro_Parameters})&lt;br /&gt;
  {Macro_Body}&lt;br /&gt;
#end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Macro Name===&lt;br /&gt;
The name of the macro. Can be as simple or as complex as you want it to be. From a programmer's perspective, it's best to name it in a semi-descriptive way of what you're trying to do.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;pov&amp;quot;&amp;gt;#macro MakeSpheres()&lt;br /&gt;
//Make a layer of spheres&lt;br /&gt;
#end&amp;lt;/source&amp;gt;&lt;br /&gt;
is much more understandable than&lt;br /&gt;
&amp;lt;source lang=&amp;quot;pov&amp;quot;&amp;gt;#macro MS()&lt;br /&gt;
//Make a layer of spheres&lt;br /&gt;
#end&amp;lt;/source&amp;gt;&lt;br /&gt;
Where as the latter of the two could be confused with &amp;quot;MakeSplines&amp;quot; or something. You never know.&lt;br /&gt;
&lt;br /&gt;
===Macro Parameters===&lt;br /&gt;
A comma delineated list of variables that you will use in the macro.&lt;br /&gt;
They can be changed if needed, but don't have to be always.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;pov&amp;quot;&amp;gt;#macro MakeSpheres(height, layer_color)&lt;br /&gt;
//Make a layer of spheres&lt;br /&gt;
//  height - height of layer&lt;br /&gt;
//  layer_color - pigment of the layer&lt;br /&gt;
#end&amp;lt;/source&amp;gt;&lt;br /&gt;
Here, parameters for height and color are added.  These can change the macro's behavior.&lt;br /&gt;
&lt;br /&gt;
===Macro Body===&lt;br /&gt;
The real juice of the macro. This is where you do loops or object creation or loops with object creation, whatever. This is where you get the job done.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;pov&amp;quot;&amp;gt;#macro MakeSpheres(height, layer_color)&lt;br /&gt;
//Make a layer of spheres&lt;br /&gt;
//  height - height of layer&lt;br /&gt;
//  layer_color - pigment of the layer&lt;br /&gt;
  #declare xpos = 0;&lt;br /&gt;
  #declare zpos = 0;&lt;br /&gt;
  #declare xfinal = 10;&lt;br /&gt;
  #declare zfinal = 10;&lt;br /&gt;
  #while (xpos &amp;lt;= xfinal)&lt;br /&gt;
    #while (zpos &amp;lt;= zfinal)&lt;br /&gt;
      // Notice that height and layer_color are inserted here.&lt;br /&gt;
      #sphere { &amp;lt;xpos,height,zpos&amp;gt; 0.4 pigment { layer_color } }&lt;br /&gt;
      #declare zpos = zpos + 1;&lt;br /&gt;
    #end&lt;br /&gt;
    #declare xpos = xpos + 1;&lt;br /&gt;
    #declare zpos = 0;&lt;br /&gt;
  #end&lt;br /&gt;
#end                &lt;br /&gt;
&lt;br /&gt;
// The macro is invoked three times with different parameters&lt;br /&gt;
MakeSpheres(0, Blue)&lt;br /&gt;
MakeSpheres(2, Red)&lt;br /&gt;
MakeSpheres(1, White)&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Zemoxian</name></author>
	</entry>
</feed>