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 ...
 
随机推荐
- 贪心 HDOJ 4726 Kia's Calculation
			
题目传送门 /* 这题交给队友做,做了一个多小时,全排列,RE数组越界,赛后发现读题读错了,囧! 贪心:先确定最高位的数字,然后用贪心的方法,越高位数字越大 注意:1. Both A and B wi ...
 - sqlserver linkserver
			
--创建链接服务器exec sp_addlinkedserver 'srv_lnk','','SQLOLEDB','远程服务器名或ip地址'exec sp_addlinkedsrvlogin ' ...
 - JAVA生成RSA非对称型加密的公钥和私钥(利用JAVA API)
			
非对称型加密非常适合多个客户端和服务器之间的秘密通讯,客户端使用同一个公钥将明文加密,而这个公钥不能逆向的解密,密文发送到服务器后有服务器端用私钥解密,这样就做到了明文的加密传送. 非对称型加密也有它 ...
 - 【C语言】05-printf和scanf函数
			
一.printf函数 这是在stdio.h中声明的一个函数,因此使用前必须加入#include <stdio.h>,使用它可以向标准输出设备(比如屏幕)输出数据 1.用法 1> pr ...
 - C++ and Java template class and function 模板类和模板函数
			
在C++和Java的泛式编程中,模板template的使用是必不可少的,但是Java中没有template关键字,所以两者的写法还是有些许区别的,请参见如下代码: Java的模板 // Java pu ...
 - thinkphp模板中foreach循环没数据的错误解决
			
从控制器方法中$this->assign();函数将值传递给html模板 但是模板不显示数据,直接出来的是代码,效果就和html中写了php代码不能解析一样. 原来是我将thinkphp框架的引 ...
 - fibonacci数列 java
			
public class Fibonacci { public static void main(String agrs[]) { ;j<=;j++) System.out.println(fo ...
 - Html - Iframe
			
父页面调用子页面 //用这个对象调用子页面的函数或者dom var myiframe = $("#right_iframe")[0].contentWindow; 子页面调用父页面 ...
 - SQLLite 可以通过SQL语言来访问的文件型SQL数据库
			
Web Storage分为两类: - sessionStorage:数据保存在session 对象中(临时) - localStorage:数据保存在本地硬件设备中(永久) sessionStorag ...
 - 虚拟机下CentOS找不到网卡eth0的解决方法
			
1. vi /etc/sysconfig/network-scripts/ifcfg-eth0 2. 将其中的ONBOOT属性改成yes即可 2015-8-3更新: 今天发现通过VirtualBo ...