溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java遞歸實現(xiàn)科赫雪花

發(fā)布時間:2020-09-04 21:46:29 來源:腳本之家 閱讀:160 作者:Mukever 欄目:編程語言

今天來寫k階科赫雪花的遞歸實現(xiàn),(K值需要你手動輸入)至于科赫雪花是什么請大家自行百度。

首先來思考這個程序怎么寫,當 count = 0 時就應該是一個三角形,這三個點是你一開始就確定的,以后的改變都依據(jù)這三個點發(fā)展的。當不是0的時候就需要計算相對于這個三角形的9個點,分別是每條邊上的兩個點,和它對應的三角形第三個頂點。

首先在JFrame中添加一個panel,我們需要在這個panel上畫圖。

大家再來看這個圖片,這張圖介紹了通過兩個點來計算其他三個點的過程。

java遞歸實現(xiàn)科赫雪花

現(xiàn)在開始在panel中畫圖:

static class showpanel extends JPanel{ 
    int number = 0; 
    public void setNumber(int number) { 
      this.number = number; 
      repaint(); 
    } 
    public void paintComponent(Graphics g) { 
      super.paintComponent(g);//畫一個簡單的panel 
       
      int side =(int)(Math.min((int)getWidth(),(int)getHeight())*0.8); 
      int high =(int)(side*Math.cos(Math.toRadians(30))); 
       
       Point p1 = new Point(getWidth() / 2, 10); 
       Point p2 = new Point(getWidth() / 2 - side / 2, 10 + high); 
       Point p3 = new Point(getWidth() / 2 + side / 2, 10 + high); 
        
       playKochSnowFlake(g, number, p1, p2); 
       playKochSnowFlake(g, number, p2, p3); 
       playKochSnowFlake(g, number, p3, p1); 
       
    } 

現(xiàn)在開始寫遞歸函數(shù)。

public static void playKochSnowFlake(Graphics g,int number,Point p1,Point p2) { 
      if(number == 0){ 
        g.drawLine(p1.x, p1.y,p2.x, p2.y); 
      } 
      else{ 
         int deltaX = p2.x - p1.x; 
          int deltaY = p2.y - p1.y; 
 
          Point x = new Point(p1.x + deltaX / 3, p1.y + deltaY / 3); 
          Point y = new Point(p1.x + deltaX * 2 / 3, p1.y + deltaY * 2 / 3); 
          Point z = new Point( 
           (int)((p1.x + p2.x) / 2 + Math.sin(Math.toRadians(60)) * (p1.y - p2.y) / 3), 
           (int)((p1.y + p2.y) / 2 + Math.sin(Math.toRadians(60)) * (p2.x - p1.x) / 3)); 
          playKochSnowFlake(g, number - 1, p1, x); 
          playKochSnowFlake(g, number - 1, x, z); 
          playKochSnowFlake(g, number - 1, z, y); 
          playKochSnowFlake(g, number - 1, y, p2); 
      } 
    } 

然后在主面板中加入一個JTextField   jta 它輸入的數(shù)據(jù)要傳入到number中。所以為其添加一個監(jiān)聽器。 已有數(shù)據(jù)輸入就調(diào)用其中的setNumber()函數(shù)設置number變量。 

jta.addActionListener(new ActionListener() { 
       
      public void actionPerformed(ActionEvent arg0) { 
        spl.setNumber(Integer.parseInt(jta.getText()));        
      } 
    }); 

 所以總體已經(jīng)完成了,剩下的就是簡答的窗體設置。

下面貼一個完整的java代碼:

import java.awt.BorderLayout; 
import java.awt.FlowLayout; 
import java.awt.Graphics; 
import java.awt.Point; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
 
public class SnowFlake extends JFrame { 
   
  private JTextField jta = new JTextField(5); 
  private showpanel spl = new showpanel(); 
   
  static class showpanel extends JPanel{ 
    int number = 0; 
    public void setNumber(int number) { 
      this.number = number; 
      repaint(); 
    } 
    public void paintComponent(Graphics g) { 
      super.paintComponent(g);//畫一個簡單的panel 
       
      int side =(int)(Math.min((int)getWidth(),(int)getHeight())*0.8); 
      int high =(int)(side*Math.cos(Math.toRadians(30))); 
       
       Point p1 = new Point(getWidth() / 2, 10); 
       Point p2 = new Point(getWidth() / 2 - side / 2, 10 + high); 
       Point p3 = new Point(getWidth() / 2 + side / 2, 10 + high); 
        
       playKochSnowFlake(g, number, p1, p2); 
       playKochSnowFlake(g, number, p2, p3); 
       playKochSnowFlake(g, number, p3, p1); 
       
    } 
    public static void playKochSnowFlake(Graphics g,int number,Point p1,Point p2) { 
      if(number == 0){ 
        g.drawLine(p1.x, p1.y,p2.x, p2.y); 
      } 
      else{ 
         int deltaX = p2.x - p1.x; 
          int deltaY = p2.y - p1.y; 
 
          Point x = new Point(p1.x + deltaX / 3, p1.y + deltaY / 3); 
          Point y = new Point(p1.x + deltaX * 2 / 3, p1.y + deltaY * 2 / 3); 
          Point z = new Point( 
           (int)((p1.x + p2.x) / 2 + Math.sin(Math.toRadians(60)) * (p1.y - p2.y) / 3), 
           (int)((p1.y + p2.y) / 2 + Math.sin(Math.toRadians(60)) * (p2.x - p1.x) / 3)); 
          playKochSnowFlake(g, number - 1, p1, x); 
          playKochSnowFlake(g, number - 1, x, z); 
          playKochSnowFlake(g, number - 1, z, y); 
          playKochSnowFlake(g, number - 1, y, p2); 
      } 
    } 
  } 
   
  public SnowFlake() { 
    JPanel panel = new JPanel(); 
    panel.setLayout(new FlowLayout()); 
    panel.add(new JLabel("Please input the number")); 
    panel.add(jta); 
    add(spl,BorderLayout.CENTER); 
    add(panel,BorderLayout.SOUTH); 
    jta.addActionListener(new ActionListener() { 
       
      public void actionPerformed(ActionEvent arg0) { 
        spl.setNumber(Integer.parseInt(jta.getText()));        
      } 
    }); 
  } 
  public static void main(String args[]) { 
    SnowFlake snowFlake = new SnowFlake(); 
    snowFlake.setSize(300, 300); 
    snowFlake.setTitle("SnowFlake"); 
    snowFlake.setLocationRelativeTo(null); 
    snowFlake.setVisible(true); 
  } 
} 

效果圖:

java遞歸實現(xiàn)科赫雪花

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI