So beendete ich ein Programm, das rekursiv Linien zeichnet, die ein Argument "n" verwendet, um die Tiefe der Rekursion zu definieren. Ich habe 2 Funktionen, eine, die die relativ linke Linie zeichnet und eine andere, die die relativ richtige zeichnet. Ich habe es getestet und es scheint für die ersten 4 Ebenen zu funktionieren, aber dann werden entweder die Zeilen zu klein, um genau darzustellen, oder irgendetwas stimmt nicht mit meinem Code, weil die Unterbrechungen zwischen den Zeilen scheinbar willkürlich werden. Hatte gehofft, jemand könnte meinen Code testen und sehen, ob sie das Problem finden könnten.Überprüfen Sie das Programm debug
Das folgende Bild ist der Tiefe 10.
EDIT: ein Teil des Codes festen, immer noch Hilfe benötigen, obwohl
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);
}
}
Ich bin eigentlich nicht erlaubt, mit der Leinwand Größe für dieses Projekt, aber ich werde versuchen, dies in wenigen Minuten zu implementieren. Es sieht gut aus und es ist auch großartig zu hören, dass es sich wahrscheinlich um einen grafischen Fehler handelt. Du bist der Mann! Vielen Dank! – user2782981