首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java中正方形的递归二分法

Java中正方形的递归二分法
EN

Stack Overflow用户
提问于 2016-07-10 18:38:52
回答 0查看 772关注 0票数 0

我基本上是试着将左下角的正方形一分为二,直到零,然后用递归将剩余的方块左上角、右上角和右下角平分,直到所有的方块都填满为止。然而,我的代码执行底部正方形平分,但当我添加新行试图使右下角平分时停止。你知道为什么抽签会停止吗?我似乎不能在左下角的第一个平分之后实现一个简单的"+“形状的绘制。

代码语言:javascript
复制
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);
        //
    }

}
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38295098

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档