下面是我正在尝试消化的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)时,它如何做到这一点?
另外,为什么没有任何翻译命令?难道不是每个迭代都会产生重叠的线段吗?
编辑:很明显,我在写这篇文章之前运行了代码,它确实起作用了。然而,正如我上面所说的,很明显,我想了解这段代码是如何工作的。
发布于 2014-09-04 23:05:57
如何制作柱面,因为它不涉及函数kmove(s)。
它确实涉及到#else分支中的宏kmove,在这里它充当递归终止符。
为什么没有任何翻译命令
圆柱体的位置在其创建过程中直接控制:
cylinder { <locx, locy, 0>, <locx_new, locy_new, 0>, s*cylinderWidth}由于使用了locx和locy变量(这两个变量在每次调用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形参引用。请注意,通过跟踪这个(非递归)序列中的loc和angle变量的值,您甚至可以在序列中去掉它们的引用,结果是
cylinder { <locx, locy, 0>, <locx_new, locy_new, 0>, s*cylinderWidth}声明(当然,再次用值替换引用)。
https://stackoverflow.com/questions/22901632
复制相似问题