Java中的分形几何:把递归用到极致

开发 后端
分形是自然界的几何学。我们看到了一些以前没有看到过得东西,它让我们用另一种眼光去看世界。下面我就以递归的思想,采用java语言画一树叶。

"一尺之锤、日取其半、万世不竭"这和分形几何的思想极为相似。

无论从美学的观点还是从科学的观点,许多人在第一次见到分形时都有新的感受。----曼德勃罗

在外形看来分形艺术似乎是魔术。但不会有任何数学家疏于了解他的结构和意义。

分形是自然界的几何学。我们看到了一些以前没有看到过得东西,它让我们用另一种眼光去看世界。

蜿蜒曲折的海岸线、起伏不定的山脉、粗糙不堪的断层、变换无常的浮云、九曲回肠的河流、纵横交错的血管、令人眼花缭乱的繁星,他们都那么的极不规则、极不光滑。粗略的说这些对象都是分形。分形几何据有自相似性、自仿射性。分形几何与欧式几何有着明显的区别:首先它是无规则的(不光滑的)、他是无限的、可以从局部看出整体、看上去复杂但结构相当简单、他的维数一般为分数。

想了解分形几何可以去baidu百科找http://baike.baidu.com/view/44498.htm

下面我就以递归的思想,采用java语言画一树叶。

截图看一下:

程序代码:

  1. import java.awt.*; 
  2. import java.awt.event.*; 
  3. import java.util.Random; 
  4. import javax.swing.*; 
  5. /** 
  6.  *  
  7.  * @author http://javaflex.iteye.com/ 
  8.  * 
  9.  */ 
  10. public class GraphicsTest extends JFrame implements ActionListener { 
  11.     public static final double PI = Math.PI / 180
  12.     JPanel panel; 
  13.     JPanel pnlCtl; 
  14.     JButton button; 
  15.     JButton button2; 
  16.     Graphics2D g2; 
  17.  
  18.     public GraphicsTest(String string) { 
  19.         super(string); 
  20.     } 
  21.  
  22.     public void init() { 
  23.         panel = new JPanel(); 
  24.         pnlCtl = new JPanel(); 
  25.         button = new JButton("分形树"); 
  26.         button2 = new JButton("清除"); 
  27.         this.add(panel, BorderLayout.CENTER); 
  28.         button.addActionListener(this); 
  29.         button2.addActionListener(this); 
  30.         pnlCtl.add(button); 
  31.         pnlCtl.add(button2); 
  32.         this.add(pnlCtl, BorderLayout.NORTH); 
  33.         setSize(800600); 
  34.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  35.         this.setVisible(true); 
  36.         Dimension winSize = Toolkit.getDefaultToolkit().getScreenSize(); 
  37.         this.setLocation((winSize.width - this.getWidth()) / 2
  38.                 (winSize.height - this.getHeight()) / 2); 
  39.         g2 = (Graphics2D) panel.getGraphics(); 
  40.     } 
  41.  
  42.     public static void main(String[] args) throws ClassNotFoundException, 
  43.             InstantiationException, IllegalAccessException, 
  44.             UnsupportedLookAndFeelException { 
  45.         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
  46.         GraphicsTest testPanel = new GraphicsTest("分形树:QQ:三2824七676"); 
  47.         testPanel.init(); 
  48.     } 
  49.  
  50.      
  51.     @Override 
  52.     public void actionPerformed(ActionEvent e) { 
  53.         if ("分形树".equals(e.getActionCommand())) { 
  54.             drawLeaf(g2, 400500100210+random.nextInt(100)); 
  55.         } else if ("清除".equals(e.getActionCommand())) { 
  56.             panel.getGraphics().clearRect(00800800); 
  57.         } 
  58.     } 
  59.     Random random=new Random(); 
  60.     public void  drawLeaf(Graphics g, double x, double y, double L, double a) { 
  61.         //random=new Random(); 
  62.         //可以方面速度画以了解其算法 
  63. //      try { 
  64. //          Thread.sleep(1000); 
  65. //      } catch (InterruptedException e) { 
  66. //          // TODO Auto-generated catch block 
  67. //          e.printStackTrace(); 
  68. //      } 
  69.         int red = random.nextInt(127); 
  70.         int green = random.nextInt(127); 
  71.         int blue = random.nextInt(127); 
  72. //随机颜色 
  73.         g.setColor(new Color(red, green, blue)); 
  74.         double x1, x2, x1L, x2L, x2R, x1R, y1, y2, y1L, y2L, y2R, y1R; 
  75.         float deflection = 50-random.nextInt(20);//侧干主干的夹角 
  76.         float intersection = random.nextInt(40)-20;//主干偏转角度 
  77.         float depth = 2+random.nextInt(2);//限制递归深度 
  78.         float ratio = 3f;//主干侧干长度比(可调整使其更茂密或稀疏) 
  79.         float ratio2 = 1.2f;//上级主干与本级主干长度比(可调整使其变高低) 
  80.         if (L > depth) { 
  81.             x2=x+L*Math.cos(a*PI); 
  82.             y2=y+L*Math.sin(a*PI); 
  83.             x2R=x2+L/ratio*Math.cos((a+deflection)*PI); 
  84.             y2R=y2+L/ratio*Math.sin((a+deflection)*PI); 
  85.             x2L=x2+L/ratio*Math.cos((a-deflection)*PI); 
  86.             y2L=y2+L/ratio*Math.sin((a-deflection)*PI); 
  87.             x1=x+L/ratio*Math.cos(a*PI); 
  88.             y1=y+L/ratio*Math.sin(a*PI); 
  89.             x1L=x1+L/ratio*Math.cos((a-deflection)*PI); 
  90.             y1L=y1+L/ratio*Math.sin((a-deflection)*PI); 
  91.             x1R=x1+L/ratio*Math.cos((a+deflection)*PI); 
  92.             y1R=y1+L/ratio*Math.sin((a+deflection)*PI); 
  93.             g.drawLine((int)x,(int)y,(int)x2,(int)y2); 
  94.             g.drawLine((int)x2,(int)y2,(int)x2R,(int)y2R); 
  95.             g.drawLine((int)x2,(int)y2,(int)x2L,(int)y2L); 
  96.             g.drawLine((int)x1,(int)y1,(int)x1L,(int)y1L);          g.drawLine((int)x1,(int)y1,(int)x1R,(int)y1R); 
  97.             drawLeaf(g,x2,y2,L/ratio2,a+intersection); 
  98.             drawLeaf(g,x2R,y2R,L/ratio,a+deflection); 
  99.             drawLeaf(g,x2L,y2L,L/ratio,a-deflection); 
  100.             drawLeaf(g,x1L,y1L,L/ratio,a-deflection); 
  101.             drawLeaf(g,x1R,y1R,L/ratio,a+deflection); 
  102.         } 
  103.     } 

希望对大家有所帮助,特别是对分形几何感兴趣的朋友,希望可以进一步交流。

原文链接:http://javaflex.iteye.com/blog/1266365

【编辑推荐】

  1. Java常量池详解之抓狂的面试题
  2. Java自带的Future多线程模式
  3. 解析Java finally的神秘面纱
  4. Java内存泄露的理解与解决
  5. 深入浅出Rhino:Java与JS互操作
责任编辑:林师授 来源: javaflex的博客
相关推荐

2011-03-10 09:32:47

Java测试

2017-07-20 16:21:52

UICountDownTidelay

2022-08-14 09:00:00

JWT 令牌凭证微服务

2023-12-14 10:03:52

内存数据

2021-12-31 07:32:50

AI人工智能主播

2014-07-09 15:45:50

宽带

2020-02-19 14:37:11

hashtagRediskey

2023-11-27 09:19:04

Memory@AliasFor

2021-07-14 07:21:57

递归区域格式

2020-10-29 07:17:37

雪崩系统服务

2022-05-16 09:17:06

反射

2009-07-20 17:41:59

Java JDBC

2022-06-20 19:39:31

微服务registry通信

2021-09-28 10:30:47

Morphling云原生

2023-08-08 08:12:25

图形编辑器几何算法

2021-02-05 15:35:21

Redis数据库命令

2017-07-28 10:45:20

大数据TensorFlow分形图案

2010-10-12 16:46:18

交换

2021-04-25 09:42:40

SQL递归SQL Server

2012-08-01 09:51:37

递归算法
点赞
收藏

51CTO技术栈公众号