几何分形-树

2024-05-25 12:08
文章标签 几何 分形

本文主要是介绍几何分形-树,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

写了个几何分形,准备在下次数媒课上展示

import io.netty.util.concurrent.DefaultThreadFactory;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.swing.*;class TreeThread implements Runnable {private FractalTree J;private Random random = new Random();private double ratio1;//调整树的稀疏度;private double ratio2;//调整树的高度;public TreeThread(FractalTree _J, double _ratio1, double _ratio2) {J = _J;ratio1 = _ratio1;ratio2 = _ratio2;}public void run() {int x = 598 + random.nextInt(3);int y = 600;J.get_g().setColor(new Color(1, 60, 10));J.get_g().setStroke(new BasicStroke(4.0f));J.get_g().drawLine(x, y, x, 450);J.get_g().setStroke(new BasicStroke(1.0f));J.drawTree(J.get_g(), x, 450, 100, 230 + random.nextInt(80), ratio1, ratio2);}
}class GrassThread implements Runnable {private FractalTree J;private Random random = new Random();private double ratio1;private double ratio2;public GrassThread(FractalTree _J, double _ratio1, double _ratio2) {J = _J;ratio1 = _ratio1;ratio2 = _ratio2;}public void run() {for (int i = 0; i < 10; ++i) {int x = random.nextInt(1200);int y = 600;J.drawTree(J.get_g(), x, y, 8, 270, ratio1, ratio2);}}
}class FractalTree extends JFrame implements ActionListener {private ThreadPoolExecutor executor;private static final double PI = Math.PI / 180;private Random random = new Random();private JPanel panel = new JPanel();private JPanel pnlCtl = new JPanel();private JButton button1 = new JButton("New Tree");private JButton button2 = new JButton("Clear");private JButton button3 = new JButton("Exit");private JButton button4 = new JButton("New Grass");private Graphics2D g;private boolean drawing = false;public FractalTree(String title) {super(title);this.executor = new ThreadPoolExecutor(4, 4, 30, TimeUnit.SECONDS, new LinkedBlockingQueue<>(),new DefaultThreadFactory("draw"));setSize(1200, 700);this.add(panel, BorderLayout.CENTER);pnlCtl.add(button1);pnlCtl.add(button4);pnlCtl.add(button2);pnlCtl.add(button3);button1.addActionListener(this);button2.addActionListener(this);button3.addActionListener(this);button4.addActionListener(this); ;this.add(pnlCtl, BorderLayout.SOUTH);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);this.setLocationRelativeTo(null);g = (Graphics2D) panel.getGraphics();}final public Graphics2D get_g() {return g;}final public void actionPerformed(ActionEvent e) {if (e.getSource() == button1) {//开启新线程绘制树;drawing = true;TreeThread tree = new TreeThread(this, 2.0, 1.3);executor.submit(tree);}if (e.getSource() == button4) {drawing = true;GrassThread grass = new GrassThread(this, 1.8, 2.0);executor.submit(grass);}if (e.getSource() == button2) {drawing = false;panel.getGraphics().clearRect(0, 0, 1200, 700);}if (e.getSource() == button3) {System.exit(0);}}final public void drawTree(Graphics g, double x, double y, double L, double a,double ratio, double ratio2) {int red = 0;int green = 0;int blue = 0;double x1, x2, x1L, x2L, x2R, x1R, y1, y2, y1L, y2L, y2R, y1R;double deflection = 40 - random.nextInt(20);//侧干主干的夹角double intersection = random.nextInt(40) - 20;//主干偏转角度double depth = 2 + random.nextInt(2);//限制递归深度if (!drawing) {return;}if (L > depth) {if (L < 8 && L > 2.5) {red = random.nextInt(10);green = random.nextInt(100) + 60;blue = 0;} else if (L < (ratio == 2.0 ? 2.3 : 2.468)) {red = 15;green = 230;blue = 0;} else {red = 1;green = 60;blue = 10;}g.setColor(new Color(red, green, blue));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);//绘制下级主干;drawTree(g, x2, y2, L / ratio2, a + intersection, ratio, ratio2);//绘制侧杆;drawTree(g, x2R, y2R, L / ratio, a + deflection, ratio, ratio2);drawTree(g, x2L, y2L, L / ratio, a - deflection, ratio, ratio2);drawTree(g, x1L, y1L, L / ratio, a - deflection, ratio, ratio2);drawTree(g, x1R, y1R, L / ratio, a + deflection, ratio, ratio2);}}
}public class TreeTest {public static void main(String args[]) {FractalTree t = new FractalTree("Fractal Tree");}
}

这是跑出来的图
[外链图片转存中...(img-aERwbO32-1653444842036)]

这篇关于几何分形-树的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1001460

相关文章

uva 10387 Billiard(简单几何)

题意是一个球从矩形的中点出发,告诉你小球与矩形两条边的碰撞次数与小球回到原点的时间,求小球出发时的角度和小球的速度。 简单的几何问题,小球每与竖边碰撞一次,向右扩展一个相同的矩形;每与横边碰撞一次,向上扩展一个相同的矩形。 可以发现,扩展矩形的路径和在当前矩形中的每一段路径相同,当小球回到出发点时,一条直线的路径刚好经过最后一个扩展矩形的中心点。 最后扩展的路径和横边竖边恰好组成一个直

poj 1113 凸包+简单几何计算

题意: 给N个平面上的点,现在要在离点外L米处建城墙,使得城墙把所有点都包含进去且城墙的长度最短。 解析: 韬哥出的某次训练赛上A出的第一道计算几何,算是大水题吧。 用convexhull算法把凸包求出来,然后加加减减就A了。 计算见下图: 好久没玩画图了啊好开心。 代码: #include <iostream>#include <cstdio>#inclu

uva 1342 欧拉定理(计算几何模板)

题意: 给几个点,把这几个点用直线连起来,求这些直线把平面分成了几个。 解析: 欧拉定理: 顶点数 + 面数 - 边数= 2。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#inc

XTU 1237 计算几何

题面: Magic Triangle Problem Description: Huangriq is a respectful acmer in ACM team of XTU because he brought the best place in regional contest in history of XTU. Huangriq works in a big compa

poj 3304 几何

题目大意:给出n条线段两个端点的坐标,问所有线段投影到一条直线上,如果这些所有投影至少相交于一点就输出Yes!,否则输出No!。 解题思路:如果存在这样的直线,过投影相交点(或投影相交区域中的点)作直线的垂线,该垂线(也是直线)必定与每条线段相交,问题转化为问是否存在一条直线和所有线段相交。 若存在一条直线与所有线段相交,此时该直线必定经过这些线段的某两个端点,所以枚举任意两个端点即可。

POJ 2318 几何 POJ 2398

给出0 , 1 , 2 ... n 个盒子, 和m个点, 统计每个盒子里面的点的个数。 const double eps = 1e-10 ;double add(double x , double y){if(fabs(x+y) < eps*(fabs(x) + fabs(y))) return 0 ;return x + y ;}struct Point{double x , y

poj 2653 几何

按顺序给一系列的线段,问最终哪些线段处在顶端(俯视图是完整的)。 const double eps = 1e-10 ;double add(double x , double y){if(fabs(x+y) < eps*(fabs(x) + fabs(y))) return 0 ;return x + y ;}struct Point{double x , y ;Point(){}Po

三维布尔运算对不规范几何数据的兼容处理

1.前言 上一篇文章谈过八叉树布尔运算,对于规范几何数据的情况是没有问题的。 在实际情况中,由于几何数据来源不一,处理和生成方式不一,我们无法保证进行布尔运算的几何数据都是规范的,对于不规范情况有时候也有需求,这就需要兼容不规范数据情况,当然这种兼容不是一味的让步,而是对于存在有限的不规范数据的兼容处理。 2.原始数据示例 下图是一个大坝模型和之上要对其进行布尔运算的立方体。 大坝模型由

CF#284 (Div. 2) C.(几何规律)

题目链接:http://codeforces.com/contest/499/problem/C 解题思路: 把两个点的坐标分别带入方程组,如果最后两个值相乘为负,即异号,计数器++。其中有一个有趣的现象,从A到B的最短步数,可以变化为求A和B之间夹了多少条直线,那么最后只要求出直线数,即可求出最小步数。 如果一条直线夹在A和B中间,那么把A和B的坐标带入后,所得值相乘一定为负。数据很

百度之星初赛1006(计算几何:能包含凸包的最小矩形面积)

矩形面积    Accepts: 717    Submissions: 1619  Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description 小度熊有一个桌面,小度熊剪了很多矩形放在桌面上,小度熊想知道能把这些