递归Sierpinski';s三角Java
我试图用Java递归地绘制Sierpinski的三角形,但它不起作用,尽管对我来说逻辑似乎很好。基本情况是当三角形彼此在2个像素范围内时,因此使用距离公式递归Sierpinski';s三角Java,java,recursion,Java,Recursion,我试图用Java递归地绘制Sierpinski的三角形,但它不起作用,尽管对我来说逻辑似乎很好。基本情况是当三角形彼此在2个像素范围内时,因此使用距离公式 import java.awt.Graphics; import java.awt.Color; import java.awt.Font; import java.awt.Canvas; public class Triangle extends Canvas implements Runnable { private stati
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
import java.awt.Canvas;
public class Triangle extends Canvas implements Runnable
{
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
public Triangle()
{
setBackground(Color.WHITE);
}
public void paint( Graphics window )
{
window.setColor(Color.BLUE);
window.setFont(new Font("ARIAL",Font.BOLD,24));
window.drawString("Serpinski's Gasket", 25, 50);
triangle(window, (WIDTH-10)/2, 20, WIDTH-40, HEIGHT-20, 40, HEIGHT-20, 4);
}
public void triangle(Graphics window, int x1, int y1, int x2, int y2, int x3, int y3, int r)
{
//if statement base case
//midpoint = (x1 + x2 / 2), (y1 + y2/ 2)
if(Math.sqrt((double)(Math.pow(x2-x1, 2)) + (double)(Math.pow(y2-y1, 2))) > 2)
//if(r==0)
{
window.drawLine(x1, y1, x2, y2);
window.drawLine(x2, y2, x3, y3);
window.drawLine(x3, y3, x1, y1);
}
int xa, ya, xb, yb, xc, yc; // make 3 new triangles by connecting the midpoints of
xa = (x1 + x2) / 2; //. the previous triangle
ya = (y1 + y2) / 2;
xb = (x1 + x3) / 2;
yb = (y1 + y3) / 2;
xc = (x2 + x3) / 2;
yc = (y2 + y3) / 2;
triangle(window, x1, y1, xa, ya, xb, yb, r-1); // recursively call the function using the 3 triangles
triangle(window, xa, ya, x2, y2, xc, yc, r-1);
triangle(window, xb, yb, xc, yc, x3, y3, r-1);
}
public void run()
{
try{
Thread.currentThread().sleep(3);
}
catch(Exception e)
{
}
}
}
跑步者正在跑步
import javax.swing.JFrame;
public class FractalRunner extends JFrame
{
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
public FractalRunner()
{
super("Fractal Runner");
setSize(WIDTH+40,HEIGHT+40);
getContentPane().add(new Triangle());
setVisible(true);
}
public static void main( String args[] )
{
FractalRunner run = new FractalRunner();
}
}
对我来说,这应该可以工作,但它会导致运行时/堆栈溢出错误,我不知道如何更正。有什么帮助吗?您需要在分离的条件检查中,将递归调用移动到triangle以及相关的数学。现在,它总是调用它,因此堆栈溢出。基本情况可能无法正常工作-如果两个三角形之间的距离从来都不是两个像素怎么办?假设我们用y1和x1分别表示0和200。其中点为100,然后50、25、12、6、3、1“StdDraw”取自:
盖伊但为什么它总是这么叫它?当三角形之间的距离小于2像素时,它应该停止,但调用不在条件范围内-因此总是进行调用。取而代之的是,将if块上的结束部分移到函数的末尾,以便递归调用接受相同的检查。@jassisika如果这个答案对您有效,那么您可能应该这样做。@JoshuaTaylor我不确定如何做。成功了。再次感谢!
public class Sierpinski {
public static void sierpinski(int n) {
sierpinski(n, 0, 0, 1);
}
public static void sierpinski(int n, double x, double y, double size) {
if (n == 0) return;
//compute triangle points
double x0 = x;
double y0 = y;
double x1 = x0 + size;
double y1 = y0;
double x2 = x0 + size / 2;
double y2 = y0 + (Math.sqrt(3)) * size / 2;
// draw the triangle
StdDraw.line(x0, y0, x1, y1);
StdDraw.line(x0, y0, x2, y2);
StdDraw.line(x1, y1, x2, y2);
StdDraw.show(100);
//recursive calls
sierpinski(n-1, x0, y0, size / 2);
sierpinski(n-1, (x0 + x1) / 2, (y0 + y1) / 2, size / 2);
sierpinski(n-1, (x0 + x2) / 2, (y0 + y2) / 2, size / 2);
}
// read in a command-line argument n and plot an order Sierpinski Triangle
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
StdDraw.setPenRadius(0.005);
sierpinski(n);
}
}