分形之概率学下的green tree
Posted 夜空中最亮的星
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分形之概率学下的green tree相关的知识,希望对你有一定的参考价值。
今天做的是分形之随机概率,可以和以前做的那个抛色子的做法非常相似,抛色子是用随机点数控制图形,今天做的树叶图形只是用概率的做法去控制图形而已,做法是如出一辙的:
//图形界面
package tree0618;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
public class treeJF extends JFrame {
public static void main(String[] args) {
treeJF fl=new treeJF(
fl.initUI();
}
public void initUI(){
getContentPane().setBackground(Color.BLACK);
setTitle("treeFlower");
setSize(600,500);
setDefaultCloseOperation(3);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
setVisible(true);
Graphics g = getGraphics();
((Graphics2D) g).setColor(Color.green);
treeListener tl = new treeListener();
addMouseListener(tl);
tl.setGraphics(g);
}
}
//具体的实现
package tree0618;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
public class treeListener implements MouseListener {
float x=0, y=0;
float a, b, c, d, e = 0, f;
private Graphics g;
public Color color;
public int i;
public float x1, y1;
public void setGraphics(Graphics gra) {
g = (Graphics2D) gra;
}
public void mouseClicked(MouseEvent e) {
Random rand = new Random();
for (int i = 0; i < 10000; i++) {
int s = rand.nextInt(100); //表示总共有100个随机数
if (s < 10) { //即0-9,表示概率是十分之一
a = 0;b = 0;c = 0;d = (float) 0.16;f = 0;
} else if (s < 18) { //表示的是10-17,概率为0.18
a = (float) 0.5;b = (float) -0.26;c = (float) 0.23;d = (float) 0.22;f = (float) 1.6;
} else if (s < 26) { //19-25,表示概率为0.07
a = (float) -0.15;b = (float) 0.28;c = (float) 0.26;d = (float) 0.24;f = (float) 0.14;
} else { //其他的则为26-100,概率是0.75
a = (float) 0.35;b = (float) 0.04;c = (float) -0.04;d = (float) 0.85;f = (float) 1.6;
}
//图形的运算公式
x1 = a * x + b * y;
y1 = c * x + d * y + f;
x = x1;
y = y1;
//g.drawLine((int)(x1*(-50)+300),(int)(y1*(-50)+400),(int)(x1*(-50)+500),(int)(y1*(-50)+500));这种写法是错误的,注意:随机数表示的是无数个点,所以不是画线是画点
g.drawLine((int)(x1*(-50)+300),(int)(y1*(-50)+500),(int)(x1*(-50)+300),(int)(y1*(-50)+500));
}
x=0;
y=0;
}
//对比抛色子:if (i < 4) {
// 画四个点
t = e.getX();
h = e.getY();
g.drawLine(t, h, t, h);
// 分别存储四个点
if (i == 0) {
t1 = t;
h1 = h;
}
else if (i == 1) {
t2 = t;
h2 = h;
}
else if (i == 2) {
t3 = t;
h3 = h;
}
else if (i == 3) {
t4 = t;
h4 = h;
}
i++;
} else {
Random rand = new Random();
for (j = 0; j < 200000; j++) {
int s = rand.nextInt(3);
if (s == 0) {// 如果选中的是A点
t4 = (t1 + t4) / 2;
h4 = (h1 + h4) / 2;
} else if (s == 1) {// 如果选中的是B点
t4 = (t2 + t4) / 2;
h4 = (h2 + h4) / 2;
} else {// 如果选中的是C点
t4 = (t3 + t4) / 2;
h4 = (h3 + h4) / 2;
}
g.drawLine(t4, h4, t4, h4);
i = 0;
}
}
// System.out.println(m+" "+x);
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public static void main(String[] args) {
}
}
运行结果为:
以上是关于分形之概率学下的green tree的主要内容,如果未能解决你的问题,请参考以下文章