因此,我完成了一個程序,遞歸繪製線,其中需要一個參數「n」來定義遞歸的深度。我有兩個功能,一個繪製相對較左的線,另一個繪製相對正確的線。我測試了它,它似乎適用於前4個級別,但隨後線條變得太小而無法準確表示,或者代碼有問題,因爲線條之間的間隔似乎變得隨意。希望有人能測試我的代碼,看看他們是否能找到問題所在。檢查程序調試
下圖是深度的10
編輯:固定部分代碼,仍然需要幫助,雖然
public class Art
{
//draws the relatively left line
public static void drawLeftLine(double x0, double y0, double x1, double y1)
{
//define new x coordinate for line
//double x2 = (1/3.0)*(x1 - x0);
//color of line
StdDraw.setPenColor(StdDraw.BLUE);
//draw line by adding new x coord to original
StdDraw.line(x0, y0, x1, y1);
}
//draw relatively right line
public static void drawRightLine(double x0, double y0, double x1, double y1)
{
//define new x coord for line
//double x2 = (2/3.0)*(x1 - x0);
//color of line
StdDraw.setPenColor(StdDraw.BLUE);
//draw line by adding new x coord to original
StdDraw.line(x0, y0, x1, y1);
}
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);
y0 = y0 - 0.1;
y1 = y1 - 0.1;
cantor(n-1, x0, y0, x0 + ((x1 - x0))/3.0, y1); //left
cantor(n-1, (2.0/ 3) * (x1 - x0) + x0, y0, x1, y1); //right
}
public static void main(String[] args)
{
//change n into integer (depth)
int n = Integer.parseInt(args[0]);
//specify inital values for line
double x0 = 0;
double y0 = 0.9;
double x1 = 0.9;
double y1 = 0.9;
//recursive function cantor
cantor(n, x0, y0, x1, y1);
}
}
我實際上不允許混淆這個項目的畫布大小,但我會嘗試在幾分鐘內實現這個。它看起來很棒,聽到這可能是一個圖形錯誤也很棒。你是男人!謝謝! – user2782981