【问题标题】:Koch Curve Povray CodeKoch 曲线 Povray 码
【发布时间】:2014-04-07 00:13:15
【问题描述】:

这是我正在尝试消化的 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 语句的宏“koch”。它是如何制造圆柱体的,因为它不涉及函数 kmove(s)。似乎每次迭代都会产生三个线段,每个线段的长度为 s/3;然后它将它们旋转某个角度。但是当它甚至不涉及 kmove(s) 时,它是如何做到的呢?

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

编辑:显然,我在发表这篇文章之前运行了代码,它确实有效。但是,从我上面所说的很明显,我想了解这段代码是如何工作的。

【问题讨论】:

    标签: recursion draw fractals povray


    【解决方案1】:

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

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

    为什么没有翻译命令

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

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

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

    要了解这里发生了什么,请尝试手动执行 POV-Ray 解析器所做的操作,即将宏调用替换为同一宏的主体,在引用形式参数的位置插入实际参数的值,删除 @ 987654325@不符合条件的案件等;最终这将导致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 形式参数引用。请注意,通过在此(非递归)序列上跟踪 locangle 变量的值,您甚至可以使用序列消除它们的引用,从而产生一个

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-23
      相关资源
      最近更新 更多