java递归实现科赫雪花

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

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

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

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

现在开始在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);

}

现在开始写递归函数

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变量。 

jta.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

spl.setNumber(Integer.parseInt(jta.getText()));

}

});

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

下面贴一个完整的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递归实现科赫雪花 的全部内容, 来源链接: utcz.com/z/358907.html

回到顶部