Hilbert空间递归演示

简介: Hilbert空间填充曲线在图像采样等方面十分有用关于什么希尔伯特 空间填充曲线看这里:http://en.wikipedia.org/wiki/Hilbert_curve 程序效果: 模拟Hilbert空间填充曲线效果,点击鼠标自动叠加!运行效果截图 Hilbert源程序代码: package com.

Hilbert空间填充曲线在图像采样等方面十分有用关于什么希尔伯特

空间填充曲线看这里:http://en.wikipedia.org/wiki/Hilbert_curve

程序效果:

模拟Hilbert空间填充曲线效果,点击鼠标自动叠加!运行效果截图


Hilbert源程序代码:

package com.gloomyfish.image.hilbert;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

public class Hilbert {
	
	public static final int WHEELSIZE = 1024;
	
	// four edges
	public static final int NORTH = 0;
	public static final int EAST = 90;
	public static final int SOUTH = 180;
	public static final int WEST = 270;

	// four corners
	public static final int NE = 45;
	public static final int SE = 135;
	public static final int SW = 225;
	public static final int NW = 315;
	
	// attributes
	private Point location;
	private Color[] colorWheel;
	private int colorIdx;

	public Hilbert() {
		// build color lookup table
		this.colorWheel = new Color[1024];
		for (int i = 0; i < 128; ++i)
			this.colorWheel[i] = new Color(0, 255 - i, i);
		for (int j = 128; j < 256; ++j)
			this.colorWheel[j] = new Color(0, j, j);
		for (int k = 0; k < 256; ++k)
			this.colorWheel[(k + 256)] = new Color(0, 255 - k, 255);
		for (int l = 0; l < 128; ++l)
			this.colorWheel[(l + 512)] = new Color(0, l, 255 - l);
		for (int i1 = 0; i1 < 128; ++i1)
			this.colorWheel[(i1 + 640)] = new Color(0, 127 - i1, 127 - i1);
		for (int i2 = 0; i2 < 256; ++i2)
			this.colorWheel[(i2 + 768)] = new Color(0, i2, 0);
		this.colorIdx = 0;
	}

	public void process(Graphics graphic, int level, int width, int height) {
		this.location = null;
		if(level > 32 ) 
		{
			graphic.drawString("could get max depth is 32!", 40, 40);
			return;
		}
		hilbert(graphic, level, 0, 0, width, height, 0, 225);
	}

	public void hilbert(Graphics g, int depth, int startx, int starty, int width, int height, int startgray, int endgray) {
		int centerX = width / 2;
		int centerY = height / 2;
		if (depth == 0) {
			if (this.location != null) {
				g.setColor(this.colorWheel[this.colorIdx]);
				g.drawLine(this.location.x, this.location.y,
						startx + centerX, starty + centerY);
				if (++this.colorIdx >= 1024)
					this.colorIdx = 0;
			}
			this.location = new Point(startx + centerX, starty + centerY);
			return;
		}

		switch (startgray) {
		case 0:
			if (endgray == 225) {
				hilbert(g, depth - 1, startx, starty + centerY, centerX, centerY, 90, 225); // bottom-left
				hilbert(g, depth - 1, startx, starty, centerX, centerY, 0, 225); // upper-left
				hilbert(g, depth - 1, startx + centerX, starty, centerX, centerY, 0, 225); // upper-right
				hilbert(g, depth - 1, startx + centerX, starty + centerY, centerX, centerY, 270, 45); // bottom-right
				return;
			}

			if (endgray != 135)
				return;
			hilbert(g, depth - 1, startx + centerX, starty + centerY, centerX, centerY, 270, 135);
			hilbert(g, depth - 1, startx + centerX, starty, centerX, centerY, 0, 135);
			hilbert(g, depth - 1, startx, starty, centerX, centerY, 0, 135);
			hilbert(g, depth - 1, startx, starty + centerY, centerX, centerY, 90, 315);

			return;
		case 90:
			if (endgray == 315) {
				hilbert(g, depth - 1, startx, starty, centerX,
						centerY, 180, 315);
				hilbert(g, depth - 1, startx + centerX, starty,
						centerX, centerY, 90, 315);
				hilbert(g, depth - 1, startx + centerX, starty
						+ centerY, centerX, centerY, 90, 315);
				hilbert(g, depth - 1, startx, starty + centerY,
						centerX, centerY, 0, 135);

				return;
			}

			if (endgray != 225)
				return;
			hilbert(g, depth - 1, startx, starty + centerY, centerX,
					centerY, 0, 225);
			hilbert(g, depth - 1, startx + centerX, starty + centerY,
					centerX, centerY, 90, 225);
			hilbert(g, depth - 1, startx + centerX, starty, centerX,
					centerY, 90, 225);
			hilbert(g, depth - 1, startx, starty, centerX, centerY,
					180, 45);

			return;
		case 180:
			if (endgray == 45) {
				hilbert(g, depth - 1, startx + centerX, starty,
						centerX, centerY, 270, 45);
				hilbert(g, depth - 1, startx + centerX, starty
						+ centerY, centerX, centerY, 180, 45);
				hilbert(g, depth - 1, startx, starty + centerY,
						centerX, centerY, 180, 45);
				hilbert(g, depth - 1, startx, starty, centerX,
						centerY, 90, 225);

				return;
			}

			if (endgray != 315)
				return;
			hilbert(g, depth - 1, startx, starty, centerX, centerY,
					90, 315);
			hilbert(g, depth - 1, startx, starty + centerY, centerX,
					centerY, 180, 315);
			hilbert(g, depth - 1, startx + centerX, starty + centerY,
					centerX, centerY, 180, 315);
			hilbert(g, depth - 1, startx + centerX, starty, centerX,
					centerY, 270, 135);

			return;
		case 270:
			if (endgray == 45) {
				hilbert(g, depth - 1, startx + centerX, starty,
						centerX, centerY, 180, 45);
				hilbert(g, depth - 1, startx, starty, centerX,
						centerY, 270, 45);
				hilbert(g, depth - 1, startx, starty + centerY,
						centerX, centerY, 270, 45);
				hilbert(g, depth - 1, startx + centerX, starty
						+ centerY, centerX, centerY, 0, 225);

				return;
			}

			if (endgray != 135)
				return;
			hilbert(g, depth - 1, startx + centerX, starty + centerY,
					centerX, centerY, 0, 135);
			hilbert(g, depth - 1, startx, starty + centerY, centerX,
					centerY, 270, 135);
			hilbert(g, depth - 1, startx, starty, centerX, centerY,
					270, 135);
			hilbert(g, depth - 1, startx + centerX, starty, centerX,
					centerY, 180, 315);

			return;
		}
	}
}
UI部分代码:

package com.gloomyfish.image.hilbert;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class HilbertUI extends JComponent implements MouseListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int level;
	private Hilbert hilbert;
	private Dimension size;

	public HilbertUI() {
		this.level = 1;
		size = new Dimension(405, 405);
		this.hilbert = new Hilbert();
		addMouseListener(this);
	}

	@Override
	protected void paintComponent(Graphics g) {
		g.setColor(Color.BLACK);
		g.clearRect(0, 0, (int)size.getWidth(), (int)size.getHeight());
		this.hilbert.process(g, this.level, (int)size.getWidth()-5, (int)size.getHeight()-5);
	}

	@Override
	public Dimension getPreferredSize() {
		return size;
	}

	@Override
	public Dimension getMaximumSize() {
		return size;
	}

	@Override
	public Dimension getMinimumSize() {
		return size;
	}

	public void mousePressed(MouseEvent paramMouseEvent) {
	}

	public void mouseEntered(MouseEvent paramMouseEvent) {
	}

	public void mouseExited(MouseEvent paramMouseEvent) {
	}

	public void mouseReleased(MouseEvent paramMouseEvent) {
	}

	public void mouseClicked(MouseEvent event) {
		int i = event.getModifiers();
		if ((i & 0x10) > 0)
			this.level += 1;
		else if (((i & 0xC) > 0) && (this.level > 0))
			this.level -= 1;
		repaint();
	}
	
	public static void main(String[] args)
	{
        JFrame mainFrame = new JFrame("Hilbert Demo - Gloomyfish");
        mainFrame.getContentPane().setLayout(new BorderLayout());
        mainFrame.getContentPane().add(new HilbertUI(), BorderLayout.CENTER);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        mainFrame.pack();  
        mainFrame.setVisible(true);  
	}
}

目录
相关文章
|
3月前
Hilbert空间递归演示
Hilbert空间递归演示
18 0
|
4月前
|
机器学习/深度学习 编译器 C语言
关于函数递归的基础
关于函数递归的基础
47 5
【C++&数据结构】超详细一文带小白轻松全面理解 [ 二叉搜索树 ]—— [从零实现&逐过程分析&代码演示&简练易懂](23)
【C++&数据结构】超详细一文带小白轻松全面理解 [ 二叉搜索树 ]—— [从零实现&逐过程分析&代码演示&简练易懂](23)
|
4月前
|
存储 监控
【初阶解法-数据结构】包含min函数的栈(代码+图示)
【初阶解法-数据结构】包含min函数的栈(代码+图示)
50 0
|
11月前
链表翻转循环和递归写法(画图分析)
链表翻转循环和递归写法(画图分析)
28 0
|
算法 C++
复制带随机指针的链表:奇妙的拷贝之旅
题目要求对一个带有随机指针的链表进行深拷贝,复制链表中的节点值、next 指针和 random 指针都应指向复制链表中的新节点。我们需要设计一个算法,满足这些条件,输入为原链表的头节点 head。
84 0
如何使用递归及注意事项
如何使用递归及注意事项
70 0
动态初始化 + 杨辉三角
动态初始化 + 杨辉三角
35 0
|
前端开发
前端学习案例5-深拷贝的递归3数组判断
前端学习案例5-深拷贝的递归3数组判断
64 0
前端学习案例5-深拷贝的递归3数组判断