服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - java递归实现科赫雪花

java递归实现科赫雪花

2021-05-10 11:07Mukever Java教程

这篇文章主要为大家详细介绍了java递归实现科赫雪花,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

今天来写k阶科赫雪花的递归实现,(k值需要你手动输入)至于科赫雪花是什么请大家自行百度。

首先来思考这个程序怎么写,当 count = 0 时就应该是一个三角形,这三个点是你一开始就确定的,以后的改变都依据这三个点发展的。当不是0的时候就需要计算相对于这个三角形的9个点,分别是每条边上的两个点,和它对应的三角形第三个顶点。

首先在jframe中添加一个panel,我们需要在这个panel上画图。

大家再来看这个图片,这张图介绍了通过两个点来计算其他三个点的过程。

java递归实现科赫雪花

现在开始在panel中画图:

java" id="highlighter_252477">
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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);
       
    }

现在开始写递归函数。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 它输入的数据要传入到number中。所以为其添加一个监听器。 已有数据输入就调用其中的setnumber()函数设置number变量。 

?
1
2
3
4
5
6
jta.addactionlistener(new actionlistener() {
       
      public void actionperformed(actionevent arg0) {
        spl.setnumber(integer.parseint(jta.gettext()));       
      }
    });

 所以总体已经完成了,剩下的就是简答的窗体设置。

下面贴一个完整的java代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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递归实现科赫雪花

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/A948433271/article/details/49560071

延伸 · 阅读

精彩推荐