Leila
Leila

Reputation: 41

Java recursion parameters

I'm stuck with my homework; does anyone have an idea?

Task: Implement a recursive method boxed(double[] center, double radius) which draws a figure like this:

enter image description here

(The outer square is the image edge.) The array center contains the x- and y-coordinates of the center of all squares whereas radius is half the length of a square. The inner squares are always rotated by 45°. The method should stop drawing when the radius is smaller than one pixel.

Other methods were given. This one draws a square rotated by the double angle:

private static void squareRotated(double[] center, double radius, double angle) {
        double[] upperLeft = {-radius, radius};
        double[] upperRight = {radius, radius};
        double[] lowerLeft = {-radius, -radius};
        double[] lowerRight = {radius, -radius};


        double[] rotUpperLeft = rotatePoint(upperLeft,angle);
        double[] rotUpperRight = rotatePoint(upperRight,angle);
        double[] rotLowerLeft = rotatePoint(lowerLeft,angle);
        double[] rotLowerRight = rotatePoint(lowerRight,angle);


        StdDraw.polygon(new double[]{rotUpperLeft[0]+center[0],rotUpperRight[0]+center[0],
                        rotLowerRight[0]+center[0],rotLowerLeft[0]+center[0]},
                        new double[]{rotUpperLeft[1]+center[1],rotUpperRight[1]+center[1],
                        rotLowerRight[1]+center[1],rotLowerLeft[1]+center[1]});
    }

This one rotates a point by the double angle:

private static double[] rotatePoint(double[] point, double angle) {
        double[] result = new double[2];
        result[0] = point[0]*Math.cos(angle) - point[1]*Math.sin(angle);
        result[1] = point[0]*Math.sin(angle) + point[1]*Math.cos(angle);
        return result;
    }
}

This is my code:

private static double angle = 0;

    private static void boxed(double[] center, double radius) {
        if (radius > (double) 1/512) {
            squareRotated(center, radius, angle);
            angle += Math.PI/4;
            boxed(center, Math.sqrt(radius*radius + radius*radius)/2);
        }
    }

It works, but is there a way I can avoid the private static double angle? I am not allowed to add a third parameter to the method and I have to solve it with recursion.

Upvotes: 2

Views: 1189

Answers (1)

OldCurmudgeon
OldCurmudgeon

Reputation: 65813

The normal technique here is to have one public method that exposes the API which then calls a private method to actually do the recursive process, passing initial conditions to it.

// The private one that actually does the recursive process.
private static void boxed(double[] center, double radius, double angle) {
    if (radius > 1.0 / 512.0) {
        squareRotated(center, radius, angle);
        angle += Math.PI / 4.0;
        boxed(center, Math.sqrt(radius * radius + radius * radius) / 2.0, angle);
    }

}

// The public one to provide the API.
public static void boxed(double[] center, double radius) {
    boxed(center, radius, 0.0);
}

NB: I have not checked this code - this is merely to demonstrate the technique.

Upvotes: 1

Related Questions