我正在用Java编写一个程序来制作递归Sierpinski三角形,它在三角形的中间和左侧像主笔画一样递归绘制。
但是,与三角形右侧无关的任何事情都不会绘制。
这是一些代码,我将解释它的工作原理,以便您更轻松地阅读它。
首先要做的是在高度函数中为三角形建立高度。
在fillTriangle中调用高度,该高度使用length作为参数(为1),然后传递给变量hei。
然后我使用一组简单的坐标。
x0,x1,x2。 y0,y1,y2。
此后,我设置了中点,因为Sierpinski三角形中的所有中点都是2点之和除以2。
然后,我将中点X和Y传递到数组中,瞧,三叉戟就制成了!
自然地,我尝试为左侧创建一个递归三角形。左边很熟练。
然后我将参数插入右边,这简直是行不通的。
/*************************************************************************
* Compilation: javac Sierpinski.java
* Execution: java Sierpinski
*
* @author:
*
*************************************************************************/
public class Sierpinski {
// Height of an equilateral triangle whose sides are of the specified length.
public static double height(double length) {
return (Math.sqrt(3.0)*length)/2.0;
// WRITE YOUR CODE HERE
}
// Draws a filled equilateral triangle whose bottom vertex is (x, y)
// of the specified side length.
public static void filledTriangle(double x, double y, double length) {
double hei = height(length);
double x0 = x-x;
double x1 = x;
double x2 = x/2;
double y0 = y;
double y1 = y;
double y2 = hei;
double ACx = (x0 + x1)/2;
double ACy = (y0 + y1)/2;
double BCx = (x1 + x2)/2;
double BCy = (y1 + y2)/2;
double BAx = (x0 + x2)/2;
double BAy = (y0 + y2)/2;
double [] X ={ACx, BCx, BAx};
double [] Y ={ACy, BCy, BAy};
//Lines 39-42 Draw our main triangle.
StdDraw.line(x0,y0,x1,y1);
StdDraw.line(x0,y0,x2,y2);
StdDraw.line(x2,y2,x1,y1);
//This fills the triangles displaced by X and Y..
//StdDraw.filledPolygon(X, Y);
//StdDraw.line(BCx, BCy ,ACx ,ACy);
//StdDraw.line(ACx, ACy, BAx, BAy);
//StdDraw.line(BAx, BAy, BCx, BCy);
StdDraw.filledPolygon(X,Y);
//WRITE YOUR CODE HERE
}
// Draws a Sierpinski triangle of order n, such that the largest filled
// triangle has bottom vertex (x, y) and sides of the specified length.
public static void sierpinski(int n, double x, double y, double length) {
filledTriangle(x, y, length);
if(n <= 1)
filledTriangle(x, y, length);
else{
//sierpinski(n--,x/2,y,length/2);
sierpinski(n--, x+x/2,y,length/2);
//sierpinski(n--, x+0.5,y, length/2);
}
// WRITE YOUR CODE HERE
}
// Takes an integer command-line argument n;
// draws the outline of an equilateral triangle (pointed upwards) of length 1;
// whose bottom-left vertex is (0, 0) and bottom-right vertex is (1, 0); and
// draws a Sierpinski triangle of order n that fits snugly inside the outline.
public static void main(String[] args) {
//StdDraw.setScale(-1.5, +1.5);
filledTriangle(1, 0, 1);
sierpinski(Integer.parseInt(args[0]), 1, 0, 1);
//sierpinski(Integer.parseInt(args[0]),1.0,1.0,1);
// WRITE YOUR CODE HERE
}
}
我注释掉了左三角形和顶部三角形,只专注于右三角形,我为右sierpinski三角形所做的只是对x坐标进行x + x / 2。
我认为答辩者应该在右递归侧是:sierpinski(n--,x + x / 2,y,length / 2);
我不仅考虑了这个问题,而且将其写下来,这肯定可以工作,但是它只是将(0,0)坐标中最左边的三角形绘制到一个奇怪的高度,而在一个奇怪的地方绘制了最右边的三角形超出范围的角度。我越是烦躁不安,我就越会意识到它不起作用,并且我的某个地方的数学关闭了。但是,我不确定在哪里。
任何人都可以帮忙吗?
答案 0 :(得分:0)
您的代码中似乎有几个错误:
n--
不返回n-1,而是返回n。您应该将其替换为n-1
。filledTriangle
的问题很少。您创建的(x,y)配对为:(x-x,y),(x,y)和(x / 2,height)。 (x,y)配对是正确的点,但其他两个不是。相反,您应该对三角形的顶部和左侧使用(x-length / 2,y + height)和(x-length,y)。sierpinski
时,应将其传递给x
而不是x+x/2
。这是因为在此程序中,您将右下角的三角形顶点用作主要顶点。绘制Sierpinski三角形的右侧时,此顶点保持不变。答案 1 :(得分:-1)