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 ...
随机推荐
- SCU3312 Stockholm Knights(最大流)
题目大概说一个n×m的格子中,'.'代表空地,'#'代表障碍,'K'代表骑士,'D'代表目的地.骑士每走一步花一条,每一步可以往(+2,+3)(-2,+3)...八个方向走,问占领所有目的地最少要几天 ...
- Sky数[HDU2097]
Sky数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 删除mysql
我以前遇到过删除mysql如果删除不干净的话,下次无法安装,也无法启动服务. 如今找到了解决方案 前言:只适用在Windows下--在控制面板中删除MySQL程序--然后在C盘中删除MySQL文件夹- ...
- COJ885 LCS???
试题描述 输入两个字符串A.B,输出他们的最长连续公共子串长度. 输入 第一行为一个字符串A. 第二行为一个字符串B. 输出 输出他们的最长连续公共子串长度. 输入示例 ababab bababbab ...
- 【问题&解决】手机网页Html代码实现(解决显示页面很小的问题)
工作需要,要做一个手机自适应的网页效果,终于搞定,先分享并记录! 其实主要就是改掉HTML页面声明: 在网页中加入以下代码,就可以正常显示了: <meta name="viewport ...
- javascript事件大全4
javascript事件列表解说 事件 浏览器支持 解说 一般事件 onclick IE3.N2 鼠标点击时触发此事件 ondblclick IE4.N4 鼠标双击时触发此事件 onmousedown ...
- C# - JSON操作
Newtonsoft.dll插件 http://download.csdn.net/detail/xinping_168/4710720 洪大师二次封装: using System; using Sy ...
- X-Japan
听X Japan这么久,几位大叔还是没有认清,真是惭愧. X-Japan是日本著名的视觉系摇滚乐队.原来叫X,在1992年8月HEATH入团的同时改名为X JAPAN. 乐队成立于1982年1月,19 ...
- [IT新应用]存储入门-文件级存储及块级别存储的选择
http://www.techrepublic.com/blog/the-enterprise-cloud/block-level-storage-vs-file-level-storage-a-co ...
- pox目录中的交换机mac地址学习模块 l2_multi源码
# Copyright 2012-2013 James McCauley # # Licensed under the Apache License, Version 2.0 (the "L ...