分形图java_数字的美丽——分形图形
第一次接触分形就被那些图形深深的吸引住了,在那些简单的杂乱无章的外貌下 实际上藏着的事一颗简单智慧
的心(分形公式)。
最开始接触的事丢色子游戏:
1.平面上随机选A,B,C三个点。再随机选一个点,记为P。
2.有一个三面色子,每丢一次,则选中ABC三个中一点。
开始游戏:
1、重复丢色子,如果选中A,则取A和P的中点P1,画黑,
2、如果选中B,则取B和P1的中点P2,画黑
3、如果选中A,则取A和P2的中点P3,画黑
4、...一直重复(如每点一下鼠标,丢100次色子。
最终画出来的效果出乎意料之外 看是杂乱的规则 最后却画出了规则的图形(倾斜的谢冰斯基三角形)。
慢慢的又得到了许多分形公式,一一画出来,顿时觉得数学是多么的强大,数学也可以如此美丽。
得到公式后并不是简单的一模一样的调用还是需要灵活的应用的:
第一类应用方法:迭代。 所谓迭代关系式,指如何从变量的前一个值推出其下一个值的公式(或关系)。
迭代关系式的建立是解决迭代问题的关键,通常可以使用递推或倒推的方法来完成。
类似于高中的数列,由n推出n+1项。
第二类应用方法:递归。 递归就是在过程或函数里调用自身;在使用递归策略时,必须有一个明确的
递归结束条件,称为递归出口。一般来说,递归需要有边界条件、递归前进段和递归返
回段。当边界条件不满足时,递归前进;当边界条件满足时,递归返回。
斐波纳契数列(Fibonacci Sequence),又称黄金分割数列,指的是这样一个数列
:1、1、2、3、5、8、13、21..... I[1] 斐波纳契数列是典型的递归案例。
课后小结:总体来说分形还是蛮简单的,只要分析出规律还是很好做的。但还是有些比较难的,迭代比较简单,
一般就是直接应用就行,递归的话,条理一定要清晰。不然很容易混乱。记得递归时参数的变化,及递归的出口,次数。
下面是几个简单的分形例子:
丢色子游戏:
class="java" name="code">
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JFrame;
public class DrawPicture extends JFrame {
/**
* @param args
*/
public static void main(String[] args) {
DrawPicture dp = new DrawPicture();
dp.init();
}
public void init(){
this.setSize(600, 600);
this.setDefaultCloseOperation(3);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
g = this.getGraphics();
JFrameListener jfl = new JFrameListener(g);
this.addMouseListener(jfl);
}
public void paint(Graphics g){
super.paint(g);
}
Graphics g;
}
package cn.gxx.study09;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
public class JFrameListener extends MouseAdapter {
Graphics g;
public JFrameListener(Graphics g){
this.g=g;
}
public void mouseClicked(MouseEvent e) {
drawpoint(g);
}
public void drawpoint(Graphics g){
Random rand = new Random();
//画A
ax = rand.nextInt(600)+1;
ay = rand.nextInt(600)+1;
g.drawLine(ax,ay,ax,ay);
//画B
bx = rand.nextInt(600)+1;
by = rand.nextInt(600)+1;
g.drawLine(bx,by,bx,by);
//画C
cx = rand.nextInt(600)+1;
cy = rand.nextInt(600)+1;
g.drawLine(cx,cy,cx,cy);
//画P
px = rand.nextInt(600)+1;
py = rand.nextInt(600)+1;
g.drawLine(px, py, px, py);
while(count<100000){
choice = rand.nextInt(3);
if(choice==0){
px=(px+ax)/2;
py=(py+ay)/2;
g.drawLine(px, py, px, py);
}
else if (choice==1){
px=(px+bx)/2;
py=(py+by)/2;
g.drawLine(px, py, px, py);
}
else if(choice==2){
px=(px+cx)/2;
py=(py+cy)/2;
g.drawLine(px, py, px, py);
}
count++;
}
}
private int ax,bx,cx,dx,ex,fx,ay,by,cy,dy,ey,fy;
private int px,py;
private int choice;
private long count;
}
三角形:
//谢冰斯基三角形
public void draw2(Graphics g){
double x1=400,y1=50;
double x2=100,y2=570;
double x3=700,y3=570;
g.setColor(Color.GREEN);
g.drawLine((int)x1,(int) y1,(int) x2,(int) y2);
g.drawLine((int)x1, (int)y1,(int) x3,(int)y3);
g.drawLine((int)x2,(int) y2,(int) x3,(int) y3);
draw3ang_2(x1,y1,x2,y2,x3,y3);
}
public void draw3ang_2(double x1,double y1,double x2,double y2,double x3,double y3){
double x1_temp=0,y1_temp=0;
double x2_temp=0,y2_temp=0;
double x3_temp=0,y3_temp=0;
x1_temp=(x1+x2)/2;
y1_temp=(y1+y2)/2;
x2_temp=(x3+x2)/2;
y2_temp=(y3+y2)/2;
x3_temp=(x1+x3)/2;
y3_temp=(y1+y3)/2;
if((int)Math.abs(x1_temp-x2_temp)==0){
return;
}
g.drawLine((int)x1_temp,(int) y1_temp,(int) x2_temp,(int) y2_temp);
g.drawLine((int)x1_temp, (int)y1_temp,(int) x3_temp,(int)y3_temp);
g.drawLine((int)x2_temp,(int) y2_temp,(int) x3_temp,(int) y3_temp);
draw3ang_2(x1,y1,(x2+x1)/2,(y1+y2)/2,(x1+x3)/2,(y1+y3)/2);
draw3ang_2((x2+x1)/2,(y1+y2)/2,x2,y2,(x2+x3)/2,(y2+y3)/2);
draw3ang_2((x1+x3)/2,(y1+y3)/2,(x2+x3)/2,(y3+y2)/2,x3,y3);
}
矩形:
//画矩形
public void draw4(Graphics g){
double x1=150,y1=80;
double width=500,height=500;
g.setColor(Color.GREEN);
g.fillRect((int)x1, (int)y1, (int)width, (int)height);
drawrec(x1, y1,width,height);
}
public void drawrec(double x1,double y1,double width,double height){
if((int)width==0){
return ;
}
double x=0,y=0;
double width_temp=width/3;
double height_temp=height/3;
x=x1+width_temp;
y=y1+height_temp;
g.setColor(Color.WHITE);
g.fillRect((int)x,(int)y,(int)width_temp,(int)height_temp);
drawrec(x-width_temp,y-height_temp,width_temp,height_temp);
drawrec(x,y-height_temp,width_temp,height_temp);
drawrec(x+width_temp,y-height_temp,width_temp,height_temp);
drawrec(x-width_temp,y,width_temp,height_temp);
drawrec(x+width_temp,y,width_temp,height_temp);
drawrec(x-width_temp,y+height_temp,width_temp,height_temp);
drawrec(x,y+height_temp,width_temp,height_temp);
drawrec(x+width_temp,y+height_temp,width_temp,height_temp);
}
树叶:
public void drawLeaf(Graphics g, double x, double y, double L, double a) {
// // 可以方面速度画以了解其算法
// try {
// Thread.sleep(10);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
int red = random.nextInt(126);
int green = random.nextInt(126);
int blue = random.nextInt(126);
//随机颜色
g.setColor(new Color(red, green, blue));
// g.setColor(Color.GREEN);
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) {
System.out.println("");
x2=x+L*Math.cos(a*PI1);
y2=y+L*Math.sin(a*PI1);
x2R=x2+L/ratio*Math.cos((a+deflection)*PI1);
y2R=y2+L/ratio*Math.sin((a+deflection)*PI1);
x2L=x2+L/ratio*Math.cos((a-deflection)*PI1);
y2L=y2+L/ratio*Math.sin((a-deflection)*PI1);
x1=x+L/ratio*Math.cos(a*PI1);
y1=y+L/ratio*Math.sin(a*PI1);
x1L=x1+L/ratio*Math.cos((a-deflection)*PI1);
y1L=y1+L/ratio*Math.sin((a-deflection)*PI1);
x1R=x1+L/ratio*Math.cos((a+deflection)*PI1);
y1R=y1+L/ratio*Math.sin((a+deflection)*PI1);
g.drawLine((int)x-50,(int)y+50,(int)x2-50,(int)y2+50);
g.drawLine((int)x2-50,(int)y2+50,(int)x2R-50,(int)y2R+50);
g.drawLine((int)x2-50,(int)y2+50,(int)x2L-50,(int)y2L+50);
g.drawLine((int)x1-50,(int)y1+50,(int)x1L-50,(int)y1L+50);
g.drawLine((int)x1-50,(int)y1+50,(int)x1R-50,(int)y1R+50);
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_数字的美丽——分形图形相关推荐
- 【POJ3889】Fractal Streets(分形图)
problem 给你一个原始的分形图 t组数据,对于每组数据,输入3个数n,h,o (n为在第n级,h,o为两个房子的编号) 求在第n级情况下,编号为h和o的两个点之间的距离*10为多少 其中,第n级 ...
- python绘制分形图基础_Python 绘制分形图(曼德勃罗集、分形树叶、科赫曲线、分形龙、谢尔宾斯基三角等)附代码...
1. 曼德勃罗集 import numpy as np import pylab as pl import time from matplotlib import cm def iter_point( ...
- 分形图的递归算法简介
分形图的递归算法 - 在计算机程序设计中,递归是指一个过程直接或间接得调用其自身的一种算法. - 直接递归调用的例子如下: void Recur(n){````Recur(m);```` } 过程Re ...
- 计算机图形学在装饰设计中的应用,混沌分形图在软装饰艺术设计中的应用研究...
摘要: 分形(Fractal)是在二十世纪70年代兴起的最重要的非线性科学之一,曼德尔布罗特(Mandelbrot)通过数学公式迭代和计算机编程技术构建了以自己名字命名的Mandelbrot集,分形理 ...
- python绘制分形图形_Python绘制L-System的分形图
Python绘制L-System的分形图代码及解析. 完整代码如下 # -*- coding: utf-8 -*- #L-System(Lindenmayer system)是一种用字符串替代产生分形 ...
- 如何用 canvas 画出分形图
前言 分形是一门以非规则几何形态为研究对象的几何学,由曼德勃 罗(B.B.Mandelbrot)等人创立并命名. 分形图从整体上看,是处处不规律的.但从局部观察,图形的规则性又是相同的,即具有自相似的 ...
- 轻松一下:python(turtle模块)绘制分形图
分形 分形,具有以非整数维形式充填空间的形态特征.通常被定义为"一个粗糙或零碎的几何形状,可以分成数个部分,且每一部分都(至少近似地)是整体缩小后的形状",即具有自相似的性质.分形 ...
- python绘制分形图基础_python绘制分形图
用Delphi 实现分形图形的绘制 [日期:2006-05-27] 来源: 作者... 基于 VB 的分形图形绘制 尹舸;胡小芳;许华忠 [期刊名称]<网络新媒体技术> [年(卷),期]2 ...
- Fractal Streets (POJ3889)(分形图、递归)
题目传送门:http://poj.org/problem?id=3889 感谢大雪菜大神bilibili上的讲解和李煜东大神书本的引导. 题面大概意思是: 给你一个原始的分形图,t组数据,对于每组数据 ...
最新文章
- 这是一份不完整的数据竞赛年鉴
- Angular6自定义指令实现多图片上传预览
- 11Linux服务器编程之:VFS虚拟文件系统,dup()函数和dup2()函数
- 全国计算机等级考试题库二级C操作题100套(第84套)
- 回发或回调参数无效。在配置中使用 enableEventValidation=true或在页面中启用了事件验证...(转)...
- Python 面向对象(OOP)基本概念
- WEB数据挖掘(十)——Aperture数据抽取(6):在Aperture中使用RDF2Go
- 原生js写小球向右移动移动一定距离停止运动及小球加速运动
- 基于单片机的智能温度监测系统设计(电路图+程序)
- 模电实验——实验三 集成运算放大器的基本应用
- Detours库配置记录
- 一级计算机脚注怎么加,word怎么插入脚注 word添加脚注图文教程
- mysql1682错误_ERROR 1682 (HY000)
- 2.leapmotion之开发指南
- Tensorboard无法显示图片
- 逆袭之路——python进阶基础之 网络编程【day32】
- 沈航数值-17-18年A-有答案
- 【EasyClick iOS免越狱常见问题】脚本运行 显示执行异常:com.js.main
- Kaggle-Digit Recognizer-ML
- 无线充电仿真 simulink llc谐振器实现恒压输出 WPT 无线电能传输