Recursion 科赫曲线波夫雷码
以下是我正在尝试消化的povray代码:Recursion 科赫曲线波夫雷码,recursion,draw,fractals,povray,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} //****************
#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 }
}
#包括“colors.inc”
摄像机{
位置
看看
}
背景{白色}
光源{白色}
光源{白色}
//**********************海龟位置******************************************
//这些代表海龟的位置和角度
#声明cylinderWidth=.1;
#宣布locx=0;
#声明locy=0;
#声明myAngle=0;
//*************************宏***************************************************
//这是一个将海龟向前移动s距离的宏。
#宏kmove(s)
#声明locx_new=locx+s*cos(弧度(myAngle));
#声明locy_new=locy+s*sin(弧度(myAngle));
圆柱体{,s*圆柱体宽度}
#宣布locx=locx_为新;
#宣布locy=locy_为新;
#结束
//这是一个用于移动海龟的递归宏。
#宏科赫(s,递归深度)
#如果(递归深度>0)
联合{
对象{koch(s/3,递归\u depth-1)}
反对{
#声明myAngle=myAngle+60;
科赫(s/3,递归深度-1)
}
反对{
#声明myAngle=myAngle-120;
科赫(s/3,递归深度-1)
}
对象{
#声明myAngle=myAngle+60;
科赫(s/3,递归深度-1)
}
}
#否则
科摩夫(s);
#结束
#结束
//*************************参数************************************************
//这将设置递归的级别数
#声明myDepth=0;
//这是海龟沿着x移动的距离。
#声明mySize=8;
//***********************画乌龟!!***************************************************
//这是一个命令,用于基于
//距离和递归级别。
反对{
科赫(mySize,myDepth)
颜料{颜色蓝}
}
我不理解的是涉及if语句的宏“koch”。它如何制造气缸,因为它不涉及功能kmove(s)。似乎每次迭代都会生成三条线段,每条线段的长度为s/3;然后它将它们旋转一个角度。但当它甚至不涉及kmove时,它是如何做到这一点的
还有,为什么没有任何翻译命令?不是每次迭代都会产生重叠的线段吗
编辑:很明显,我在写这篇文章之前运行了代码,而且它确实有效。然而,正如我上面所说的,我想了解这段代码是如何工作的
它如何制造气缸,因为它不涉及功能kmove(s)
它确实涉及#else
分支中的宏kmove,它在这里充当递归终止符
为什么没有任何翻译命令
圆柱体的位置在其创建过程中直接控制:
cylinder { <locx, locy, 0>, <locx_new, locy_new, 0>, s*cylinderWidth}
使用实际值而不是s
形式参数引用。请注意,通过在该(非递归)序列上跟踪loc
和angle
变量的值,您甚至可以通过该序列消除它们的引用,从而产生以下其中一个
cylinder { <locx, locy, 0>, <locx_new, locy_new, 0>, s*cylinderWidth}
圆柱体{,s*圆柱体宽度}
声明(当然,引用再次被值替换)
cylinder { <locx, locy, 0>, <locx_new, locy_new, 0>, s*cylinderWidth}