Koch Curve Povray代码

时间:2014-04-07 00:13:16

标签: recursion draw fractals povray

这是我想要消化的povray代码:

#include "colors.inc"

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


background{White}

light_source { <10, 10, 5> color White} 
light_source { <0, 20, 0> color White} 

//********************** Turtle Position ****************************      
// These represent the position and angle of the turtle
#declare cylinderWidth=.1;           
#declare locx = 0;
#declare locy = 0;
#declare myAngle = 0;   

//************************* Macros **********************************  
// This is a macro that moves the turtle forward by a distance s.
#macro kmove(s)    
   #declare locx_new = locx + s*cos(radians(myAngle));
   #declare locy_new = locy + s*sin(radians(myAngle));  
   cylinder { <locx, locy, 0>,    <locx_new, locy_new, 0>, s*cylinderWidth}
   #declare locx = locx_new;
   #declare locy = locy_new;   
#end


// This is a macro that is recursive for moving the turtle around. 
#macro koch(s, recursion_depth)
    #if (recursion_depth > 0)
        union {       
            object { koch(s/3,  recursion_depth-1)  }
            object {
                 #declare myAngle = myAngle + 60;
                 koch(s/3,  recursion_depth-1)   
                }    
            object {
                #declare myAngle = myAngle -120; 
                koch(s/3,  recursion_depth-1)    
            }
            object {   
                #declare myAngle = myAngle + 60;   
                koch(s/3,  recursion_depth-1)  
            } 

        }
    #else
        kmove(s);
    #end
#end  


//************************* Parameters  **********************************  
// This sets the number of levels of recursion
#declare myDepth = 0;                      

// This is the distance along x that the turtle moves. 
#declare mySize = 8; 

//*********************** DRAW THE TURTLE!!  ******************************* 
// This is the command for actually drawing the Turtle's path based in the
// distance and levels of recursion.
object{
  koch(mySize,myDepth) 
  pigment { color Blue }   
} 

我不明白的是宏观&#39; koch&#39;涉及if语句。它是如何制造气缸的,因为它不涉及kmove(s)功能。似乎每次迭代都会产生三个线段,每个线段的长度为s / 3;然后它将它们旋转一定角度。但它怎么做呢,什么时候甚至不涉及kmove?

另外,为什么没有任何翻译命令?每次迭代都不会产生重叠的线段吗?

编辑:显然,我在发布此帖子之前运行了代码,事实上确实有效。但是,正如我上面所说的那样,我想了解这段代码是如何工作的。

1 个答案:

答案 0 :(得分:0)

  

它如何制作圆柱体,因为它不涉及kmove(s)函数。

它确实涉及#else分支中的宏kmove,它在这里作为递归终止符。

  

为什么没有任何翻译命令

圆柱的位置在创建过程中直接控制:

cylinder { <locx, locy, 0>,    <locx_new, locy_new, 0>, s*cylinderWidth}

由于使用了locxlocy变量(每次调用kmove时都会更新!),圆柱体被放置在不同的位置。

要了解此处发生的事情,请尝试手动执行POV-Ray解析器所执行的操作,即将宏调用替换为同一宏的主体,插入引用形式参数的实际参数的值,删除{ {1}}条件不符合的情况等;最终这会导致if-then-else缩减为koch(mySize,myDepth)个序列,即

序列
kmove

使用实际值而不是#declare locx_new = locx + s*cos(radians(myAngle)); #declare locy_new = locy + s*sin(radians(myAngle)); cylinder { <locx, locy, 0>, <locx_new, locy_new, 0>, s*cylinderWidth} #declare locx = locx_new; #declare locy = locy_new; 形式参数引用。请注意,通过在此(非递归)序列上跟踪sloc变量的值,您甚至可以删除其引用的序列,从而导致其中一个

angle

声明(当然,引用再次被值替换)。