2016-07-10 3 views
0

Я в основном пытаюсь делить пополам квадрат на нижнем левом квадрате до нуля, затем с рекурсией делите пополам оставшиеся квадраты вверху слева, справа вверху и внизу справа, пока все квадраты не будут заполнены. Тем не менее, мой код выполняет нижнее квадратное деление пополам, но просто останавливается, когда я добавляю новую строку, пытаясь сделать нижнюю правую биссеку. Любые идеи, почему ничья останавливается? Кажется, я не могу реализовать рисунок простой формы «+» после первой деления пополам в левом нижнем углу.Рекурсивное деление пополам квадрата в 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

Result of fixing the tolerance to 0.005

recursivepatterns пакет;

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

}

Смежные вопросы