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分形树的更多相关文章

  1. Mysql存储引擎之TokuDB以及它的数据结构Fractal tree(分形树)

    在目前的Mysql数据库中,使用最广泛的是innodb存储引擎.innodb确实是个很不错的存储引擎,就连高性能Mysql里都说了,如果不是有什么很特别的要求,innodb就是最好的选择.当然,这偏文 ...

  2. 分形树Fractal tree介绍——具体如何结合TokuDB还没有太懂,先记住其和LSM都是一样的适合写密集

    在目前的Mysql数据库中,使用最广泛的是innodb存储引擎.innodb确实是个很不错的存储引擎,就连高性能Mysql里都说了,如果不是有什么很特别的要求,innodb就是最好的选择.当然,这偏文 ...

  3. TokuDB介绍——本质是分形树(一个叶子4MB)+缓存减少写操作

    其性能特点见:http://www.cnblogs.com/billyxp/p/3567421.html TokuDB 是一个高性能.支持事务处理的 MySQL 和 MariaDB 的存储引擎.Tok ...

  4. java遍历树(深度遍历和广度遍历

    java遍历树如现有以下一颗树:A     B          B1               B11          B2               B22     C          C ...

  5. 【Python 16】分形树绘制4.0(利用递归函数绘制分形树fractal tree)

     1.案例描述 树干为80,分叉角度为20,树枝长度小于5则停止.树枝长小于30,可以当作树叶了,树叶部分为绿色,其余为树干部分设为棕色. 2.案例分析 由于分形树具有对称性,自相似性,所以我们可以用 ...

  6. java集合树状结构及源码

    java集合树状结构及源码 最近一直想看一下java集合的源码,毕竟平时用的比较多,但总是感觉是跟着习惯new出来一个对象,比如ArrayList,HashMap等等,所以就简单的看了一下,了解了一下 ...

  7. TokuDB的索引结构–分形树的实现

    分形树简介 原文:http://www.bitstech.net/2015/12/15/tokudb-index-introduction/ 分形树是一种写优化的磁盘索引数据结构. 在一般情况下, 分 ...

  8. 用python的turtle画分形树

    由于分形树具有对称性,自相似性,所以我们可以用递归来完成绘制.只要确定开始树枝长.每层树枝的减短长度和树枝分叉的角度,我们就可以把分形树画出来啦!! 代码如下: # -*- coding: utf-8 ...

  9. 随机L系统分形树 分类: 计算机图形学 2014-06-01 23:27 376人阅读 评论(0) 收藏

    下面代码需要插入到MFC项目中运行,实现了计算机图形学中的L系统分形树. class Node { public: int x,y; double direction; Node(){} }; CSt ...

随机推荐

  1. 思维 UVALive 3708 Graveyard

    题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...

  2. asp.net中导出Excel的方法

    一.asp.net中导出Excel的方法: 本文转载 在asp.net中导出Excel有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址输出在浏览器上:一种是将文件直接将文件输出 ...

  3. ural 1437. Gasoline Station

    1437. Gasoline Station Time limit: 1.0 secondMemory limit: 64 MB Once a gasoline meter broke at a fi ...

  4. Square Coins[HDU1398]

    Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  5. strerror

    #include<stdio.h> #include<string.h> #include<errno.h> void main(void ) { printf(& ...

  6. BZOJ1444 : [Jsoi2009]有趣的游戏

    建立AC自动机,并求出转移矩阵. 再用$\sum E(终止节点)=1$去替换第一个方程,高斯消元即可. 时间复杂度$O(n^3l^3)$. 注意精度问题,要特判0.00的情况. #include< ...

  7. android 全屏视频播放(SurfaceView + MediaPlayer)

    介绍个第三方: JieCaoVideoPlayer 实现Android的全屏视频播放,支持完全自定义UI.手势修改进度和音量.hls.rtsp,设置http头信息,也能在ListView.ViewPa ...

  8. C#创建桌面快捷方式 和 开机启动

              /// <summary>         /// 创建桌面快捷方式 2010-11-25         /// </summary>         p ...

  9. 【BZOJ】 1007: [HNOI2008]水平可见直线(凸壳)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1007 一开始我贪心的写了下,当然全wa了.. 这题看了题解感觉很简单. 首先什么情况才能看到呢? w ...

  10. Mysql 解决left join 数据重复的问题

    select p.*,g.roleName,pg.srcType from t_gold_pay_add p left join gRole g on p.roleID=g.roleID left j ...