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