Java中正方形的递归平分

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

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);
        //
    }

}

将公差修正为0.005

包递归模式;

import java.awt.Color; 进口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);
    //
}

}

链接地址: http://www.djcxy.com/p/80711.html

上一篇: Recursive bisection of a square in Java

下一篇: Recursive Squares with Python