-1

I'm working on a homework to create a program that takes one argument "n" and creates a cantor line of that depth, but it does not seem to be working. I think there's a problem with variable scope and functions. This is the first time I work with functions and recursion, so I'm a little confused still. I have three functions, one to draw the relative left line, the relative right line, and the cantor function which calls itself twice and calls both draw functions.

public class Art
{
public static void drawLeftLine(double x0, double y0, double x1, double y1)
{
    double x2 = (1/3.0)*(x1 - x0);
    double x3 = x0;
    double y2 = y0 - 0.1;
    double y3 = y1 - 0.1;

    StdDraw.line(x2, y2, x3, y3);

}

public static void drawRightLine(double x0, double y0, double x1, double y1)
{

    double x2 = (2/3.0)*(x1 - x0);
    double x3 = x1;
    double y2 = y0 - 0.1;
    double y3 = y1 - 0.1;

    StdDraw.line(x2, y2, x3, y3);

}

public static void cantor(int n, double x0, double y0, double x1, double y1)
{
    if (n == 0)
        return;

    drawLeftLine(x0, y0, x1, y1);
    drawRightLine(x0, y0, x1, y1);



    cantor(n-1, x0, y0, x1, y1); //left
    cantor(n-1, x0, y0, x1, y1); //right



}

public static void main(String[] args)
{ 
    int n = Integer.parseInt(args[0]);

    double x0 = 0;
    double y0 = 0.9;
    double x1 = 0.9;
    double y1 = 0.9;

    StdDraw.line(x0, y0, x1, y1);

    cantor(n, x0, y0, x1, y1);

}
}
4

1 回答 1

0

你一遍又一遍地画同一条线。除了深度(n)之外,递归调用的参数需要改变。在行尾发表评论并没有做到这一点。

于 2013-10-04T00:51:08.713 回答