ordinary
ordinary

Reputation: 6133

how to recursively draw a line

Here is the code. I don't think it's having the effect we intend it to.

package lines;

import sedgewick.StdDraw;

public class Lines {


    public static boolean equals(double d1, double d2){
        if(Math.abs(d2-d1) < 0.00001)
            return true;
        return false;
    }

    public static void drawLine(double x1, double y1, double x2, double y2) {
        if(equals(x1,x2) && equals(y1,y2))
            return;

        double mid_x = (x2+x1)/2;
        double mid_y = (y2+y1)/2;

        drawLine(x1,y1,mid_x,mid_y);
        drawLine(mid_x+0.001,mid_y+0.001,x2,y2);

        StdDraw.point(mid_x, mid_y);

    }


    /**
     * Code to test the drawLine method visually
     */
    public static void main(String[] args) {
        StdDraw.setPenRadius(0.02);

        //
        // Test the drawing program
        //
        drawLine(0,0,1,1); // lower left to upper right
        drawLine(0,1,1,0); // upper left to lower right

        //
        // Draw rectangles of decreasing width and height
        //
        for (double r = 0.25; r < 0.5; r = r+.005) {
            double s = 1-r;
            drawLine(r,r, r, s);
            drawLine(r, s, s, s);
            drawLine(s, s, s, r);
            drawLine(s, r, r, r);
        }
        System.out.println("done drawing");
    }

}

Here is the result, which I cannot understand.

http://grab.by/rUM0

Upvotes: 2

Views: 4392

Answers (2)

dright_chao
dright_chao

Reputation: 1

public static void drawLine(double x1, double y1, double x2, double y2){         
        if(equals(x1,x2) && equals(y1,y2))
            return;
        double mid_x = (x2+x1)/2;
        double mid_y = (y2+y1)/2;
        StdDraw.point(mid_x, mid_y);
        drawLine(x1,y1,mid_x,mid_y);
        drawLine(mid_x,mid_y,x2,y2);
}

Upvotes: 0

Syd Kerckhove
Syd Kerckhove

Reputation: 833

First of all, I'd like to say I think what you're doing is a really cool.

For the actual answer, we need to look at how Swing draws stuff. A picture is just an array of pixels. However, the pixels aren't arranged how you would intuitively think. You would think that it would be something like this.

y
^
|
|
1
|
0--1-----> x

In actuality, it is something like this.

0--1-----> x
|
1
|
|
v
y

I hope this is clear, as my ASCII drawing isn't much. The y axis is inverted, but the x axis is not. Most people need to find this out through some kind of debugging the first time they use drawing in Java.

If we look back at your code, we see that it makes much more sense now, if we visualize it in the second 'picture' I drew.

EDIT: Apparently I was wrong, my apologies. There is a problem in this line.

drawLine(mid_x+0.001,mid_y+0.001,x2,y2);

I think you should remove the "+0.001"

However, this wasn't why I was wrong. I tried to run you code and I got stack overflow errors. After changing your equals method to something like this.

public static boolean equals(double d1, double d2){
    if(Math.abs(d2-d1) < 0.1)
        return true;
    return false;
}

I could now run the program, and it gave perfect results. I'm not sure what went wrong for you.

Upvotes: 1

Related Questions