java koch雪花java递归
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13000994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
koch snowflake java recursion
提问by user1533012
I am trying to make a koch snowflake using recursion and gpdraw in java. I can make the actual koch curve itself but i dont know how to make it come all the way around and make a snowflake.
我想在java中使用递归和gpdraw制作koch雪花。我可以自己制作实际的科赫曲线,但我不知道如何让它一路走来并制作雪花。
import gpdraw.*;
public class KochCurve
{
private SketchPad myPaper;
private DrawingTool myPencil;
public KochCurve()
{
myPaper = new SketchPad(600,600);
myPencil = new DrawingTool(myPaper);
}
public void draw()
{
drawKochCurve(6, 300);
}
private void drawKochCurve(double level, double sideLength)
{
if(level < 1)
myPencil.forward(sideLength);
else
{
drawKochCurve(level - 1, (sideLength)/3);
myPencil.turnLeft(60);
drawKochCurve(level - 1, (sideLength)/3);
myPencil.turnRight(120);
drawKochCurve(level - 1, (sideLength)/3);
myPencil.turnLeft(60);
drawKochCurve(level - 1, (sideLength)/3);
}
}
}
采纳答案by RParadox
You will have to paint the same curve in each of the three directions of a triangle. You can do this by writing a function drawKochCurve(double level, double sideLength, double additionalAngle) and calling it three times, adding the additionalAngle.
您必须在三角形的三个方向中的每一个方向上绘制相同的曲线。您可以通过编写一个函数 drawKochCurve(double level, double sideLength, double additionalAngle) 并调用它三次并添加 additionalAngle 来做到这一点。
Wikipedia has more details: The Koch curve originally described by Koch is constructed with only one of the three sides of the original triangle. In other words, three Koch curves make a Koch snowflake. http://en.wikipedia.org/wiki/Koch_snowflake
维基百科有更多细节: Koch 最初描述的 Koch 曲线仅由原始三角形的三个边之一构成。换句话说,三个科赫曲线构成了科赫雪花。http://en.wikipedia.org/wiki/Koch_snowflake