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 ...
随机推荐
- 操作properties文件,注意抹掉最前面的"file:"
package com.xiewanzhi.property; import java.io.BufferedInputStream; import java.io.File; import java ...
- 解决ibus图标为红圈(图标丢失)
修正IBUS图标丢失gconftool –type boolean -s /desktop/ibus/panel/show_icon_on_systray truegconftool –type bo ...
- 分享到QQ空间代码(一)
如何给自己的网站添上"分享到QQ空间"的功能? 只要选择以下代码嵌入自己的网页,即可将网站的信息分享到QQ空间
- 关于UIWebView的总结
关于UIWebView的总结 前言 今天参加了 Adobe 和 CSDN 组织的一个关于 PhoneGap 的开发讲座 ,而 PhoneGap 在 iOS 设备上的实现就是通过 UIWebView 控 ...
- SPOJ 3267 D-query(离散化+主席树求区间内不同数的个数)
DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...
- 如何解包,编辑,重新打包boot images
HOWTO: Unpack, Edit, and Repack Boot Images http://forum.xda-developers.com/showthread.php?t=443994 ...
- MySQL 创建用户 与 授权
例,需要给 121.52.215.100 连接添加一个用户 dee,密码是 123456,他只能对数据库 vshop 有 select 权限: CREATE USER '; GRANT SELECT ...
- PDO 学习与使用 ( 一 ) :PDO 对象、exec 方法、query 方法与防 SQL 注入
1.安装 PDO 数据库抽象层 PDO - PHP Data Object 扩展类库为 PHP 访问数据库定义了一个轻量级的.一致性的接口,它提供了一个数据访问抽象层,针对不同的数据库服务器使用特定的 ...
- 西川善司【神秘海域(Uncharted)】的图形分析
本文是为传播0月8日发售的[神秘海域 合集]魅力而短篇连载的第2回,这次主要集中在神秘海域系列的图形的技术方面.原文链接在 http://weekly.ascii.jp/elem/000/ ...
- 【翻译】Kinect v2程序设计(C++) BodyIndex篇
通过Kinect SDK v2预览版,取得BodyIndex(人体区域)的方法和示例代码. 上一节,介绍了从Kinect v2预览版用Kinect SDK v2预览版获取Depth数据的方法. 这 ...