1つの引数「n」を取り、その深さのカントールラインを作成するプログラムを作成する宿題に取り組んでいますが、機能していないようです. 変数のスコープと関数に問題があると思います。関数と再帰を扱うのはこれが初めてなので、まだ少し混乱しています。3 つの関数があります。1 つは相対左線、相対右線を描画する関数で、cantor 関数は自分自身を 2 回呼び出して両方の描画関数を呼び出します。
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);
}
}