自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java中的分形幾何:把遞歸用到極致

開發(fā) 后端
分形是自然界的幾何學(xué)。我們看到了一些以前沒有看到過得東西,它讓我們用另一種眼光去看世界。下面我就以遞歸的思想,采用java語言畫一樹葉。

"一尺之錘、日取其半、萬世不竭"這和分形幾何的思想極為相似。

無論從美學(xué)的觀點還是從科學(xué)的觀點,許多人在第一次見到分形時都有新的感受。----曼德勃羅

在外形看來分形藝術(shù)似乎是魔術(shù)。但不會有任何數(shù)學(xué)家疏于了解他的結(jié)構(gòu)和意義。

分形是自然界的幾何學(xué)。我們看到了一些以前沒有看到過得東西,它讓我們用另一種眼光去看世界。

蜿蜒曲折的海岸線、起伏不定的山脈、粗糙不堪的斷層、變換無常的浮云、九曲回腸的河流、縱橫交錯的血管、令人眼花繚亂的繁星,他們都那么的極不規(guī)則、極不光滑。粗略的說這些對象都是分形。分形幾何據(jù)有自相似性、自仿射性。分形幾何與歐式幾何有著明顯的區(qū)別:首先它是無規(guī)則的(不光滑的)、他是無限的、可以從局部看出整體、看上去復(fù)雜但結(jié)構(gòu)相當(dāng)簡單、他的維數(shù)一般為分?jǐn)?shù)。

想了解分形幾何可以去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);//側(cè)干主干的夾角 
  76.         float intersection = random.nextInt(40)-20;//主干偏轉(zhuǎn)角度 
  77.         float depth = 2+random.nextInt(2);//限制遞歸深度 
  78.         float ratio = 3f;//主干側(cè)干長度比(可調(diào)整使其更茂密或稀疏) 
  79.         float ratio2 = 1.2f;//上級主干與本級主干長度比(可調(diào)整使其變高低) 
  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.     } 

希望對大家有所幫助,特別是對分形幾何感興趣的朋友,希望可以進(jìn)一步交流。

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

【編輯推薦】

  1. Java常量池詳解之抓狂的面試題
  2. Java自帶的Future多線程模式
  3. 解析Java finally的神秘面紗
  4. Java內(nèi)存泄露的理解與解決
  5. 深入淺出Rhino:Java與JS互操作
責(zé)任編輯:林師授 來源: javaflex的博客
相關(guān)推薦

2011-03-10 09:32:47

Java測試

2017-07-20 16:21:52

UICountDownTidelay

2022-08-14 09:00:00

JWT 令牌憑證微服務(wù)

2023-12-14 10:03:52

內(nèi)存數(shù)據(jù)

2021-12-31 07:32:50

AI人工智能主播

2014-07-09 15:45:50

寬帶

2020-02-19 14:37:11

hashtagRediskey

2021-07-14 07:21:57

遞歸區(qū)域格式

2023-11-27 09:19:04

Memory@AliasFor

2020-10-29 07:17:37

雪崩系統(tǒng)服務(wù)

2022-05-16 09:17:06

反射

2022-06-20 19:39:31

微服務(wù)registry通信

2009-07-20 17:41:59

Java JDBC

2023-08-08 08:12:25

圖形編輯器幾何算法

2021-09-28 10:30:47

Morphling云原生

2010-10-12 16:46:18

交換

2021-02-05 15:35:21

Redis數(shù)據(jù)庫命令

2017-07-28 10:45:20

大數(shù)據(jù)TensorFlow分形圖案

2021-04-25 09:42:40

SQL遞歸SQL Server

2012-08-01 09:51:37

遞歸算法
點贊
收藏

51CTO技術(shù)棧公眾號