2014-04-07 31 views
1

这里是我试图消化代码的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 } 
} 

我不明白的是,涉及的if语句宏“科赫”。它是如何制造气缸的,因为它不涉及kmove(s)的功能。似乎每次迭代都会产生三条线段,每条线段的长度均为s/3;然后通过某个角度旋转它们。但是它如何做到这一点,甚至不涉及kmove(s)?

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

编辑:很明显,我做这个帖子之前运行的代码,它确实工作。但是,正如我上面所说的那样,我想了解这些代码是如何工作的。

回答

0

它是如何使气缸,因为它不涉及函数kmove(s)。

它确实涉及#else分支中的宏kmove,它在此处用作递归终止符。

为什么没有任何翻译指令

缸的位置是其创建期间直接控制:

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

作为locxlocy变量用于(其是每当kmove被调用时更新!),气缸被放置在不同的位置。

为了理解这里发生了什么,请尝试手动完成POV-Ray分析器的工作,即用同一个宏的主体替换宏调用,插入参考形式参数的实际参数值,删除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; 

与实际值,而不是s形式参数的引用的序列。请注意,在您甚至可以与导致

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

声明(当然是一个由替代的参考序列摆脱他们引用的这个(非递归)序列跟踪locangle变量的值值再次)。