【问题标题】:Recursive changing variables - Sierpinski carpet递归变化变量 - 谢尔宾斯基地毯
【发布时间】:2022-05-19 02:41:37
【问题描述】:

我在绘制谢尔宾斯基地毯时遇到了一些问题,希望能提供任何帮助。

我能够定义停止条件,绘制中心矩形,并递归地绘制图像的下一层,同时保持计数。

碰巧我只能在左上角画画。我会说我混淆了变量,但我似乎无法弄清楚。会感谢任何帮助

这是我遇到问题的代码部分。

    int smallerWidth = newWidth / 3;
    int smallerHeight = newHeight / 3;

    int sX = 0;
    int sY = 0;
    if (currentDeep > 1) {
        for (int i = 0; i < 3; i++) {
            sX = width / 9 + (i * 3 * (width / 9));
            sY = height / 9;
            g.fillRect(sX, sY, smallerWidth, smallerHeight);
            for (int j = 0; j < 3; j++) {
                sY = height / 9 + (j * 3 * (height / 9));
                g.fillRect(sX, sY, smallerWidth, smallerHeight);
            }
        }
        return 1 + printSquares(g, sX, sY, newWidth, newHeight, currentDeep 
                                                  - 1);
    } else
        return 1;
}

这是完整的代码 https://pastebin.com/WPJ5tG8w

总之我的问题是。为了让我的程序绘制剩余的 7 个正方形,我应该更改/创建什么?

【问题讨论】:

    标签: java image recursion


    【解决方案1】:

    您的代码的问题是,您正试图一次对多层递归执行操作。通常,在递归中,您只会绘制 Quadrado 中心,计算较小矩形的大小和坐标,然后递归调用该方法。这样可以确保递归调用不会影响已经存在的内容。

    private int printSquares(Graphics g, int xi, int yi, int width, int height, int currentDeep) {
        //Quadrado central
        int newWidth = width / 3;
        int newHeight = height / 3;
        int x = (width / 3) + xi;
        int y = (height / 3) + yi;
        g.fillRect(x, y, newWidth, newHeight);
    
        int sX = 0;
        int sY = 0;
        if (currentDeep > 1) {
            int sum = 0;
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    //This is the position of each of the small rectangles
                    sX = i * (width / 3) + xi;
                    sY = j * (height / 3) + yi;
    
                    // Call the method recursively in order to draw the smaller rectangles
                    sum += printSquares(g, sX, sY, newWidth, newHeight, currentDeep - 1);
                }
            }
            return 1 + sum;
        } else
            return 1;
    }
    

    希望这能解决你的问题。

    【讨论】:

    • 那么,让我看看我是否理解正确。通过行 sum += printSquares(g, sX, sY, newWidth, newHeight, currentDeep - 1);您所做的是在每个新方块周围绘制一个新方块,然后,稍后,您返回绘制的方块数,但就在所有方块都被绘制之后。看来我也有很多变数,Ty 的帮助,我很感激
    • 不客气。如果我的回答解决了您的问题,请记住将问题标记为已解决。否则其他人也可能会尝试解决它。​​
    【解决方案2】:

    我希望这没问题。这是惊人的代码。我冒昧地使用问题中提供的原始代码完成了此操作,并添加了 Illedhar 推荐的修复它的代码作为添加方法。这里是。谢谢你分享这个。

    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingUtilities;
    import javax.swing.WindowConstants;
    
    public class sierpinskicarpet {
     
       public static Color BACKGROUNDCOLOR = new Color(0, 0, 150);
       public static Color FOREGROUNDCOLOR = new Color(255, 180, 0);
     
        // Padrao = 5, alterado
       public static int DEEP = 10;
     
        /**
         * Build the frame and shows it
         */
       public sierpinskicarpet(int deep) {
       
            // the frame and title
          JFrame frame = new JFrame();
          frame.setTitle("...: Recursive Squares with deep " + deep + " :...");
       
            // Dispose frame on click on close button
          frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
       
            // set size and center frame on screen
          frame.setSize(400, 400);
          frame.setLocationRelativeTo(null);
       
            // add print area occupying all the frame content area
          frame.add(new PrintArea(deep));
       
            // put frame visible
          frame.setVisible(true);
       }
     
        /**
         * Main method
         */
       public static void main(String[] args) 
       {
          SwingUtilities.invokeLater(
             new Runnable() {
                public void run() {
                    // launch for 1 to DEEP squares frames
                   for (int i = DEEP; i >= 1; --i) {
                        // build a new object each time: objects will run
                        // independently
                      new sierpinskicarpet(i);
                   }
                
                }
             });
       }
    }
     
    /**
     * Our print area is, in fact, a label extended with the paint squares behavior
     */
    class PrintArea extends JLabel {
       private static final long serialVersionUID = 1L;
     
        // local deep variable, will keep the registered deep for this the print
        // area
       int deep;
     
        /**
         * constructor
         */
       public PrintArea(int deep) {
            // call super, that is JLabel, constructor
          super();
       
            // set background color and set as well opaque to allow the background
            // to be visible
          setBackground(sierpinskicarpet.BACKGROUNDCOLOR);
          setOpaque(true);
       
            // save the deep
          this.deep = deep;
       }
     
        /**
         * paint method, called by JVM, when it is needed to update the PrintArea
         */
       public void paint(Graphics g) {
            // call paint from the JLABEL, draws the background of the PrintArea
          super.paint(g);
       
            // set drawing color
          g.setColor(sierpinskicarpet.FOREGROUNDCOLOR);
       
            // call the amazing print square method
          int n = printSquares(g, 0, 0, getWidth(), getHeight(), this.deep);
       
            // put to the world how much squares we printed
          System.out.println("Deep = " + deep + ", squares painted: " + n);
       }
     
        /**
         * Auxiliary method that will to the work. It must print a square with 1/3
         * of the length of the frame and at the center and if not the bottom level
         * ask to do the same for each of the other 8 square with 1/3 of length but
         * called with the new deep
         */
       private int printSquares(Graphics g, int xi, int yi, int width, int height, int currentDeep) {
        //Quadrado central
          int newWidth = width / 3;
          int newHeight = height / 3;
          int x = (width / 3) + xi;
          int y = (height / 3) + yi;
          g.fillRect(x, y, newWidth, newHeight);
       
          int sX = 0;
          int sY = 0;
          if (currentDeep > 1) {
             int sum = 0;
             for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    //This is the position of each of the small rectangles
                   sX = i * (width / 3) + xi;
                   sY = j * (height / 3) + yi;
                
                    // Call the method recursively in order to draw the smaller rectangles
                   sum += printSquares(g, sX, sY, newWidth, newHeight, currentDeep - 1);
                }
             }
             return 1 + sum;
          } else
             return 1;
       
       }
    }
    
    /*
    Works Cited:
    Recursive changing variables - sierpinski carpet. Stack Overflow. Retrieved May 4, 2022, 
    from https://stackoverflow.com/questions/49945862/recursive-changing-variables-sierpinski-carpet
    */
    

    【讨论】:

      猜你喜欢
      • 2018-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-30
      • 2015-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多