java分形树
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*; /**
*
* @author http://javaflex.iteye.com/
*
*/
public class GraphicsTest extends JFrame implements ActionListener {
public static final double PI = Math.PI / 180;
JPanel panel;
JPanel pnlCtl;
JButton button;
JButton button2;
Graphics2D g2; public GraphicsTest(String string) {
super(string);
} public void init() {
panel = new JPanel();
pnlCtl = new JPanel();
button = new JButton("分形树");
button2 = new JButton("清除");
this.add(panel, BorderLayout.CENTER);
button.addActionListener(this);
button2.addActionListener(this);
pnlCtl.add(button);
pnlCtl.add(button2);
this.add(pnlCtl, BorderLayout.NORTH);
setSize(800, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
Dimension winSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((winSize.width - this.getWidth()) / 2,
(winSize.height - this.getHeight()) / 2);
g2 = (Graphics2D) panel.getGraphics();
} public static void main(String[] args) throws ClassNotFoundException,
InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
GraphicsTest testPanel = new GraphicsTest("分形树:QQ:三2824七676");
testPanel.init();
} @Override
public void actionPerformed(ActionEvent e) {
if ("分形树".equals(e.getActionCommand())) {
drawLeaf(g2, 400, 500, 100, 210 + random.nextInt(100));
} else if ("清除".equals(e.getActionCommand())) {
panel.getGraphics().clearRect(0, 0, 800, 800);
}
} Random random = new Random(); public void drawLeaf(Graphics g, double x, double y, double L, double a) {
// random=new Random();
// 可以方面速度画以了解其算法
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
int red = random.nextInt(127);
int green = random.nextInt(127);
int blue = random.nextInt(127);
// 随机颜色
g.setColor(new Color(red, green, blue));
double x1, x2, x1L, x2L, x2R, x1R, y1, y2, y1L, y2L, y2R, y1R;
float deflection = 50 - random.nextInt(20);// 侧干主干的夹角
float intersection = random.nextInt(40) - 20;// 主干偏转角度
float depth = 2 + random.nextInt(2);// 限制递归深度
float ratio = 3f;// 主干侧干长度比(可调整使其更茂密或稀疏)
float ratio2 = 1.2f;// 上级主干与本级主干长度比(可调整使其变高低)
if (L > depth) {
x2 = x + L * Math.cos(a * PI);
y2 = y + L * Math.sin(a * PI);
x2R = x2 + L / ratio * Math.cos((a + deflection) * PI);
y2R = y2 + L / ratio * Math.sin((a + deflection) * PI);
x2L = x2 + L / ratio * Math.cos((a - deflection) * PI);
y2L = y2 + L / ratio * Math.sin((a - deflection) * PI);
x1 = x + L / ratio * Math.cos(a * PI);
y1 = y + L / ratio * Math.sin(a * PI);
x1L = x1 + L / ratio * Math.cos((a - deflection) * PI);
y1L = y1 + L / ratio * Math.sin((a - deflection) * PI);
x1R = x1 + L / ratio * Math.cos((a + deflection) * PI);
y1R = y1 + L / ratio * Math.sin((a + deflection) * PI);
g.drawLine((int) x, (int) y, (int) x2, (int) y2);
g.drawLine((int) x2, (int) y2, (int) x2R, (int) y2R);
g.drawLine((int) x2, (int) y2, (int) x2L, (int) y2L);
g.drawLine((int) x1, (int) y1, (int) x1L, (int) y1L);
g.drawLine((int) x1, (int) y1, (int) x1R, (int) y1R);
drawLeaf(g, x2, y2, L / ratio2, a + intersection);
drawLeaf(g, x2R, y2R, L / ratio, a + deflection);
drawLeaf(g, x2L, y2L, L / ratio, a - deflection);
drawLeaf(g, x1L, y1L, L / ratio, a - deflection);
drawLeaf(g, x1R, y1R, L / ratio, a + deflection);
}
}
}
java分形树的更多相关文章
- Mysql存储引擎之TokuDB以及它的数据结构Fractal tree(分形树)
在目前的Mysql数据库中,使用最广泛的是innodb存储引擎.innodb确实是个很不错的存储引擎,就连高性能Mysql里都说了,如果不是有什么很特别的要求,innodb就是最好的选择.当然,这偏文 ...
- 分形树Fractal tree介绍——具体如何结合TokuDB还没有太懂,先记住其和LSM都是一样的适合写密集
在目前的Mysql数据库中,使用最广泛的是innodb存储引擎.innodb确实是个很不错的存储引擎,就连高性能Mysql里都说了,如果不是有什么很特别的要求,innodb就是最好的选择.当然,这偏文 ...
- TokuDB介绍——本质是分形树(一个叶子4MB)+缓存减少写操作
其性能特点见:http://www.cnblogs.com/billyxp/p/3567421.html TokuDB 是一个高性能.支持事务处理的 MySQL 和 MariaDB 的存储引擎.Tok ...
- java遍历树(深度遍历和广度遍历
java遍历树如现有以下一颗树:A B B1 B11 B2 B22 C C ...
- 【Python 16】分形树绘制4.0(利用递归函数绘制分形树fractal tree)
1.案例描述 树干为80,分叉角度为20,树枝长度小于5则停止.树枝长小于30,可以当作树叶了,树叶部分为绿色,其余为树干部分设为棕色. 2.案例分析 由于分形树具有对称性,自相似性,所以我们可以用 ...
- java集合树状结构及源码
java集合树状结构及源码 最近一直想看一下java集合的源码,毕竟平时用的比较多,但总是感觉是跟着习惯new出来一个对象,比如ArrayList,HashMap等等,所以就简单的看了一下,了解了一下 ...
- TokuDB的索引结构–分形树的实现
分形树简介 原文:http://www.bitstech.net/2015/12/15/tokudb-index-introduction/ 分形树是一种写优化的磁盘索引数据结构. 在一般情况下, 分 ...
- 用python的turtle画分形树
由于分形树具有对称性,自相似性,所以我们可以用递归来完成绘制.只要确定开始树枝长.每层树枝的减短长度和树枝分叉的角度,我们就可以把分形树画出来啦!! 代码如下: # -*- coding: utf-8 ...
- 随机L系统分形树 分类: 计算机图形学 2014-06-01 23:27 376人阅读 评论(0) 收藏
下面代码需要插入到MFC项目中运行,实现了计算机图形学中的L系统分形树. class Node { public: int x,y; double direction; Node(){} }; CSt ...
随机推荐
- SignalR —— Asp.net RealTime的春天
一般的例子:http://www.asp.net/signalr/overview/signalr-20/getting-started-with-signalr-20/tutorial-gettin ...
- 详解Adorner Layer(zz)
首先,千万不要觉得Adorner离你很远,因为最简单的WPF界面也会用到Adorner.在WPF中,下面的几个很常见的功能,都是用Adorner实现的. 1. 光标(caret) 2. ...
- 折线分割平面[HDU2050]
折线分割平面 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDU 4507 (鬼畜级别的数位DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4507 题目大意:求指定范围内与7不沾边的所有数的平方和.结果要mod 10^9+7(鬼畜の元凶) 解题 ...
- 更新Android SDK之后Eclipse提示ADT版本过低的一个简易解决办法
首先说明一下发表这一篇博文的“历史原因”吧,因为在更新SDK之后,进入Eclipse设置Android SDK目录的时候,会突然说我的版本低什么的,尝试自己解决但失败之后,我在搜索引擎上找了很多中文的 ...
- 【UOJ】【UR #2】猪猪侠再战括号序列(splay/贪心)
http://uoj.ac/problem/31 纪念伟大的没有调出来的splay... 竟然那个find那里写错了!!!!!!!!!!!!! 以后要记住:一定要好好想过! (正解的话我就不写了,太简 ...
- COJ977 WZJ的数据结构(负二十三)
试题描述 输入一个字符串S,输出S的最长连续回文子串长度. 输入 输入一个字符串S. 输出 输出S的最长连续回文子串长度 输入示例 abacbbc 输出示例 4 其他说明 1<=|S|<= ...
- java图片处理——多张图片合成一张Gif图片并播放或Gif拆分成多张图片
1.多张jpg图合成gif动画 /** * 把多张jpg图片合成一张 * @param pic String[] 多个jpg文件名 包含路径 * @param newPic String 生成的gif ...
- Servlet 编程 简单流程处理(重定向)
流程(来自jiekexueyuan) servlet sucess.jsp error.asp同理 运行结果:
- 如何在64位windows7上同时使用32位和64位的Eclipse
我用的是64位的windows7旗舰版,jdk1.7 64位机器上可以同时运行32位和64位的Eclipse,但是电脑中必须有相应的jdk.Eclipse虽然不需要安装,但是在启动时会检查系统中固定文 ...