java中hilberts空间填充曲线的算法

hgqdbh6s  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(329)

我试图创建一个简单的javaapplet,用java显示生成的hilberts曲线。我把我的代码和一本书中的一些代码弄混了,现在我被卡住了。我没有得到我想要的展示。
以下是全部代码:

import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;

    //Defining classes 
    public class HilbertCurves extends JApplet {
        //Declare data fields
        private JTextField OrderTextfield = new JTextField("0",5);
        private HilbertCurve  hilbertCurvePanel = new HilbertCurve();

        //Constructors
        public HilbertCurves() {
        //Creating a panel object
        JPanel panel = new JPanel();
        panel.add(new JLabel("Enter the order : "));
        panel.add(OrderTextfield);

        OrderTextfield.setHorizontalAlignment(SwingConstants.RIGHT);

        //Adding panel to the app
        add(hilbertCurvePanel);
        add(panel, BorderLayout.SOUTH);
        OrderTextfield.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
        hilbertCurvePanel.setOrder(Integer.parseInt(OrderTextfield.getText()));
            }
        });
        }

        private static class HilbertCurve extends JPanel {
        //Declare variables
            private int length;
            private int order=1;
            private int a,b;

        //For order
            public void setOrder(int order){
            this.order=order;
            repaint();
            }

            public void paintComponent(Graphics g){
            super.paintComponent(g);

        //Find the length
            length = Math.min(getWidth(),getHeight());
            for (int i = 0; i < order; i++)
                length/=2;

        //Reset
            a = length/2; b=length/2;
            Upper(g,order);
            }

        //Display upper
            private void Upper(Graphics g, int order){
                if (order >0){
                    Left(g, order-1);
                    lineNewPosition(g,0,length);
                    Upper(g,order-1);
                    lineNewPosition(g,0,-length);
                    right(g, order-1);

                }
            }

        //Display left
            private void Left(Graphics g, int order){
                if (order>0){
                    Upper(g,order-1);
                    lineNewPosition(g,length,0);
                    Left(g,order-1);
                    lineNewPosition(g,0,length);
                    Left(g,order-1);
                    lineNewPosition(g,-length,0);
                    Down(g,order-1);

                }
            }

        //Display right 
             private void right(Graphics g, int order){
                if (order>0){
                    Down(g,order-1);
                    lineNewPosition(g,-length,0);
                    right(g,order-1);
                    lineNewPosition(g,0,-length);
                    right(g,order-1);
                    lineNewPosition(g,length,0);
                    Upper(g,order-1);

                }
            }

        //Display down 
             private void Down(Graphics g, int order){
                if (order>0){
                    right(g,order-1);
                    lineNewPosition(g,0,-length);
                    Down(g,order-1);
                    lineNewPosition(g,-length,0);
                    Down(g,order-1);
                    lineNewPosition(g,0,length);
                    Left(g,order-1);

                }
            }     
        //Draw a line
             public void lineNewPosition(Graphics g, int deltaX, int deltaY){
             g.drawLine(a, b, a + deltaX, b + deltaY);
             a+=deltaX;
             b+=deltaY;
            }
        }

        public static void main(String[] args) {
        JApplet applet = new HilbertCurves();
        applet.init();
        applet.start();
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Visualising Hilbert Curves");
        frame.getContentPane().add(applet,BorderLayout.CENTER);
        frame.setSize(512,512);
        frame.setVisible(true);
        }
    }

在我看来,在定义绘图函数时有些错误,但在这一点上,我似乎无法找到解决方法。以下是我得到的结果(与我的目标不同):

sxissh06

sxissh061#

您的绘图代码似乎比需要的长得多。
由当前位置和方向以及这些基本操作组成的状态: turn(x) 将左转x*90度(负数右转) step() 会划一条线向前走一步
你可以做出这样的希尔伯特曲线:

// draw a hilbert curve.  Resulting total movement will be forward,
// and the current direction is left unchanged
// order: order of curve to draw
// side: -1 to draw on the left, 1 to draw on the right

hilbert(order, side):
    if (order > 0):
        turn(side)
        hilbert(order-1,-side)
        step()
        turn(-side)
        hilbert(order-1,side)
        step()
        hilbert(order-1,side)
        turn(-side)
        step()
        hilbert(order-1,-side)
        turn(side)

相关问题