递归,用最少的代码量完成最大的工作量,初步对递归开始上心是因为机试老师给了一道题,看到后开始着急忙慌就开始做,最后那个代码量,就不忍吐槽了,自己还觉得挺自豪的,写了那么多行代码,也打印出了正确图形,,结果老师一公布他的答案,和我几百行的代码相比老师只用了7行,完败。。。。。。。。。
于是觉得与其拿到题就开始做不如先找规律,本来思想就比代码重要,分形就是用递归画出美丽的图案,那些平时在我们眼中单调无趣的线条以及点,就能在不断递归中画出自然界之美。
我随意挑了两个比较有代表性的图案
画出的图如下所示:
以下是代码实现:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main extends JFrame {
private Color color;
double a,b,c,d;
double x1=0;
double y1=0;
double x2,y2;
public static void main(String[] args) {
// TODO Auto-generated method stub
Main a = new Main();
a.draw();
}
public void draw(){//绘制界面,建立画布
this.setSize(1000,800);//
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
this.setLayout(new FlowLayout());
//设置可见
this.setVisible(true);
Graphics g = this.getGraphics();
//调用·手镯图形
this.magic(g);
}
//手镯图形,公式已经得知
public void magic(Graphics g){
double a = 1.40,b = 1.56,c = 1.40, d = -6.56;
for(int i=0;i<12133;i++){
for(int t=0;t<80;t++){
Color co = new Color(t++,t*2,t*3);
g.setColor(co); }
double x2=d*Math.sin(a*x1)-Math.sin(b*y1);
double y2=c*Math.cos(a*x1)+Math.cos(b*y1);
int x22= (int) (x2*30)+300;
int y22=(int) (y2*30)+200;
System.out.println(x22+","+y22);
g.drawLine(x22,y22,x22,y22);
x1=x2;
y1=y2;}
}
public void paint(Graphics g){
super.paint(g);
this.magic2(500,550,100, Math.PI/2,0,Math.PI/6,25,g);
//(Math.PI为180°)
}
public void magic2(double x0,double y0,double l,double a,double b,double c,double count,Graphics g){
ImageIcon cat=new ImageIcon("image//$R7U8UGI.gif");
ImageIcon dog=new ImageIcon("image//$RJT225K.gif");
ImageIcon panda=new ImageIcon("image//$RM069VP.gif");
double x2;
double y2;
double x3;
double y3;
double x4;
double y4;
double x5;
double y5;
Random r=new Random();
//颜色随机变化
color = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));
g.setColor(color);
if(count<1)
{
return;
}//判断是否继续进行递归调用,注意:判断一定要放在递归调用之前,否则这段代码将永远不会被执行
x2 = x0 - l*Math.cos(a);
y2 = y0 - l*Math.sin(a);
x3 = x2 - l*Math.cos(b);
y3 = y2 - l*Math.sin(b);
x4 = x0 - l*Math.cos(b);
y4 = y0 - l*Math.sin(b);
x5 = x2 - l*Math.cos(Math.PI/6)*Math.cos(c);
y5 = y2 - l*Math.cos(Math.PI/6)*Math.sin(c);
//可画图形,计算五个点的位置,以右下点为(X0,Y0)
//g.drawImage(cat.getImage(),(int)x0-300,(int)y0-300, null);
//g.drawImage(panda.getImage(),(int)x2-300,(int)y2+300, null);
//g.drawImage(dog.getImage(),(int)x3,(int)y3, null);
//g.drawImage(cat.getImage(),(int)x4,(int)y4, null);
//g.drawImage(panda.getImage(),(int)x5,(int)y5, null);
g.drawLine((int)x0, (int)y0, (int)x2, (int)y2);
g.drawLine((int)x2, (int)y2, (int)x3, (int)y3);
g.drawLine((int)x3, (int)y3, (int)x4, (int)y4);
g.drawLine((int)x4, (int)y4, (int)x0, (int)y0);
g.drawLine((int)x2, (int)y2, (int)x5, (int)y5);
g.drawLine((int)x5, (int)y5, (int)x3, (int)y3);
//递归调用
magic2(x2,y2,l*Math.cos(Math.PI/6),a+Math.PI/6,b+Math.PI/6,c+Math.PI/6,count-1,g);
magic2(x5,y5,l*Math.sin(Math.PI/6),a-Math.PI/3,b-Math.PI/3,c-Math.PI/3,count-1,g);
}
private Color getColor(int inc){
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
red += inc;
green += inc;
blue += inc;
if(red > 255)
red = 255;
if(green > 255)
green = 255;
if(blue > 255)
blue = 255;
return new Color(red,green,blue);
}
}