Java中正方形的递归二分法

时间:2016-07-10 10:38:52

标签: java recursion

我基本上试图将左下方的正方形平分为零,然后将递归平分左上方,右上方和右下方的剩余方块,直到所有方格都被填满。但是,我的代码执行底部正方形二分,但只是在我添加新行时尝试使右下角二等分停止。任何想法为什么抽奖停止?在左下角的第一次二分之后,我似乎无法实现简单的“+”形状的绘制。

package recursivepatterns;

import java.awt.Color;
import sedgewick.*;

public class PersianRug {


    public static void subDiv(double S, Color C, double x, double y) {
        //
        StdDraw.line(x+S/2, y, x+S/2, y+S);
        StdDraw.line(x, y+S/2, x+S, y+S/2);
    }


    /**
     * 
     * @param palette an array of Colors to choose from
     * @param llx lower left x coordinate of this rug square
     * @param lly lower left y coordinate of this rug square
     * @param size width (and therefore also height) of this rug square
     * @param north color index of the north side of this rug square
     * @param east color index of the east side of this rug square
     * @param south color index of the south side of this rug square
     * @param west color index of the west side of this rug square
     */

    private static void persianRug( 
            Color[] palette, 
            double llx, double lly, 
            double size, 
            int north, int east, int south, int west){
        //
        int c = 0; 
        Color C = palette[c];   

        subDiv(size,C,llx,lly); 

        if (size>0){
            // ll
            persianRug(palette,llx,lly,size/2, c, c, south, west); // north and east are fixed
            // lr
            StdDraw.setPenColor(StdDraw.RED);
            StdDraw.circle(0.5,0.5,size/2); // this tests that circles do in fact recursively get bigger
            persianRug(palette,llx+size/2,lly,size/2, c, c, south, west); // this line does not work at all

        }
        else return;
    }


    public static void main(String args[]) {
        //
        // Leave the following line commented out, but once you
        //   have things working, uncomment it, and also uncomment
        //   the similar line at the end of this method.
        // Uncommenting those lines will run the graphics code
        //   in double-buffering mode, so that your image will appear
        //   almost instantaneously, instead of being drawn one line
        //   at a time.
        //
        //  Here is the line to uncomment:
        //
        //StdDraw.show(10);   // don't forget to uncomment the other line at the end
        //


        //
        // Generate a palette of colors
        //
        Color[] palette = { StdDraw.BLUE, StdDraw.CYAN, StdDraw.DARK_GRAY,
                StdDraw.GRAY, StdDraw.GREEN, StdDraw.LIGHT_GRAY,
                StdDraw.MAGENTA, StdDraw.ORANGE, StdDraw.PINK, StdDraw.RED,
                StdDraw.WHITE, StdDraw.YELLOW };
        //
        // Draw the outermost square as a special case
        // Use color 0 for that
        //
        StdDraw.setPenColor(palette[0]);
        StdDraw.line(0, 0, 1, 0);
        StdDraw.line(1, 0, 1, 1);
        StdDraw.line(1, 1, 0, 1);
        StdDraw.line(0, 1, 0, 0);


        //
        // Kick off the recursion
        // Lower left is point (0,0)
        // Size of the square is 1
        // The color index of each surrounding side is 0
        //
        persianRug(palette, 0, 0, 1, 0, 0, 0, 0);
        //
        // Also uncomment this line when you have things working
        //   to speed up the drawing:
        //
        //StdDraw.show(10);
        //
    }

}

1 个答案:

答案 0 :(得分:0)

Result of fixing the tolerance to 0.005

package recursivepatterns;

import java.awt.Color; import sedgewick。*;

公共类PersianRug {

public static int pickColor(Color[] palette, int n, int e, int s, int w) {
    // USES MODULAR ARITHMETIC AND BOUNDS OF THE PALETTE RING TO ASSIGN A COLOR
    if ((n+s+w+e+2)%palette.length<palette.length/2-1)
        return (n+e+s+w+8)%palette.length; // VARYING MODULAR COLOR
    else if ((n+s+w+e+2)%palette.length>=palette.length/2-1 && (n+s+w+e)%palette.length<palette.length-5){
        return palette.length-8;} // FIXED COLOR OF PALETTE
    else 
        return 0; // "BACKGROUND" COLOR
}

public static void subDiv(double S, double x, double y) {
    // DRAWS A CROSS GIVEN THE LOWER LEFT CORNER COORDINATES
    StdDraw.line(x+S/2, y, x+S/2, y+S); // vertical
    StdDraw.line(x, y+S/2, x+S, y+S/2); // horizontal
}

/**
 * 
 * @param palette an array of Colors to choose from
 * @param llx lower left x coordinate of this rug square
 * @param lly lower left y coordinate of this rug square
 * @param size width (and therefore also height) of this rug square
 * @param n color index of the north side of this rug square
 * @param e color index of the east side of this rug square
 * @param s color index of the south side of this rug square
 * @param w color index of the west side of this rug square
 */
private static void persianRug( 
        Color[] palette, 
        double llx, double lly, 
        double size, 
        int n, int e, int s, int w){
    // 
    int c = pickColor(palette,n,e,s,w); 
    StdDraw.setPenColor(palette[c]);
    subDiv(size,llx,lly); 
    n = (n+1)%palette.length; // ITERATE THE PALETTE FOR DIFFERENT COLOR NEXT ITER
    s = (s+1)%palette.length; //
    e = (e+1)%palette.length; // 
    w = (w+1)%palette.length; //

    if (size>.0025){ // THIS BOUND DICTATES HOW DETAILED AND HOW QUICKLY THE RUG IS DRAWN
        lly = lly+size/2; // TOP LEFT
        persianRug(palette,llx,lly,size/2, n, c, c, w);
        llx = llx+size/2; // TOP RIGHT
        persianRug(palette,llx,lly,size/2, n, e, c, c);
        llx=llx-size/2; // BOTTOM LEFT
        lly = lly-size/2;
        persianRug(palette,llx,lly,size/2, c, c, s, w);
        llx = llx+(size/2); // BOTTOM RIGHT
        persianRug(palette,llx,lly,size/2, c, e, s, c); 

    }
    else return;
}


public static void main(String args[]) {
    //
    // Leave the following line commented out, but once you
    //   have things working, uncomment it, and also uncomment
    //   the similar line at the end of this method.
    // Uncommenting those lines will run the graphics code
    //   in double-buffering mode, so that your image will appear
    //   almost instantaneously, instead of being drawn one line
    //   at a time.
    //
    //  Here is the line to uncomment:
    //
    StdDraw.show(10);   // don't forget to uncomment the other line at the end
    //


    //
    // Generate a palette of colors
    Color[] palette = { StdDraw.BLACK, StdDraw.RED, StdDraw.ORANGE,
            StdDraw.PINK, StdDraw.BLUE, StdDraw.YELLOW,
            StdDraw.RED, StdDraw.ORANGE, StdDraw.ORANGE, StdDraw.RED,
            StdDraw.PINK, StdDraw.RED};
    //
    // Draw the outermost square as a special case
    // Use color 0 for that
    //
    StdDraw.setPenColor(palette[0]);
    StdDraw.line(0, 0, 1, 0);
    StdDraw.line(1, 0, 1, 1);
    StdDraw.line(1, 1, 0, 1);
    StdDraw.line(0, 1, 0, 0);



    //
    // Kick off the recursion
    // Lower left is point (0,0)
    // Size of the square is 1
    // The color index of each surrounding side is 0
    //
    persianRug(palette, 0, 0, 1, 0, 0, 0, 0);
    //
    // Also uncomment this line when you have things working
    //   to speed up the drawing:
    //
    StdDraw.show(10);
    //
}

}