分形和数据结构之迭代实现分形

迭代实现分形之一:

package 文雅0605;

 

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Graphics;

import java.awt.Graphics2D;

 

import javax.swing.JFrame;

 

public class ring extends JFrame{

       public static void main(String[] args) {

              ring fl=new ring();

                fl.initUI();

              }

              public void initUI(){

                     getContentPane().setBackground(Color.white);

                     setTitle("ring");

                     setSize(600,500);

                     setDefaultCloseOperation(3);

                     setLocationRelativeTo(null);

                     setLayout(new FlowLayout());

                     setVisible(true);

                     Graphics g = getGraphics();//注意这里一定要在窗体可见之后才能调用

                     ((Graphics2D) g).setColor(Color.BLACK);

                   ringListener fl = new ringListener();

                     addMouseListener(fl);

                     fl.setGraphics(g);

              }

}

 

package 文雅0605;

 

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

 

       public class ringListener implements MouseListener {

              double x= 0, y = 0;

             

              private Graphics g;

              public Color color;

              public int i;

              public double x1, y1;

 

              public void setGraphics(Graphics gra) {

                     g = (Graphics2D) gra;

              }

 

              public void mouseClicked(MouseEvent e) {

                     if(e.getClickCount()==2){//双击

                     double a = 1.40, b = 1.56, c = 1.40, d =-6.56;

                     for (int i = 0; i < 100000; i++) {

                           

                            x1= d* Math.sin(a * x) - Math.sin(b * y);

                            y1= c* Math.cos(a * x) + Math.cos(b* y);

                      //int m = (int)(x1*10+200);

                            //int n = (int)(y1*10+200);

                     int m = (int) (x1* 20+300);

                     int n = (int) (y1* 20+300);//固定了图形的位置

                     //g.setColor(new Color(100, i %50 , i % 50));或者直接在窗体上((Graphics2D) g).setColor(Color.BLACK);设置颜色

 

                      g.drawLine(m,n,m,n);

                       //g.drawLine((int)x1*50+500,(int)y1*20+500,(int)x1*50+500,(int)y1*20+500);这种写法是错误的,图形的要求是要先算出结果再强制转型

      

                        x=x1;

                        y=y1;//迭代思维

                     }

                     x=0;

                     y=0;//能够控制绘制图形的新一轮的循环

              }

              else{

                     double a=-2,b=-2,c=-1.2,d=2;

                     for (int i = 0; i < 100000; i++) {

                     x1= (Math.sin(a * y) - Math.cos(b * x));

                     y1= (Math.sin(c * x) - Math.cos(d* y));

               int m = (int) (x1* 50 +500);//乘数控制图形大小,加法控制图形的位置,但是范围要控制在第一象限,否则无法看到图形

                int n = (int) (y1 *50 +500);

               g.drawLine(m,n,m,n);   

                 x=x1;

                 y=y1;

              }

                     x=0;

                     y=0;

                    

              }

              }

             

 

              public void mousePressed(MouseEvent e) {

 

              }

 

              public void mouseReleased(MouseEvent e) {

 

              }

 

              public void mouseEntered(MouseEvent e) {

              }

 

              public void mouseExited(MouseEvent e) {

 

              }

 

              public static void main(String[] args) {

              }

 

       }

输出结果:

 :

 

迭代实现分形之二:

package 文雅0612;

 

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Graphics;

import java.awt.Graphics2D;

 

import javax.swing.JFrame;

 

public class net extends JFrame {

       public static void main(String[] args) {

              net fl = new net();

              fl.initUI();

       }

 

       public void initUI() {

              getContentPane().setBackground(Color.black);

              setTitle("net");

              setSize(600, 500);

              setDefaultCloseOperation(3);

              setLocationRelativeTo(null);

              setLayout(new FlowLayout());

              setVisible(true);

              Graphics g = getGraphics();

              ((Graphics2D) g).setColor(Color.BLUE);

              netListener fl = new netListener();

              addMouseListener(fl);

              fl.setGraphics(g);

       }

}

 

 

package 文雅0612;

 

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

 

public class netListener implements MouseListener {

       double x = 0, y = 0;

 

       private Graphics g;

       public int i;

       public double x1, y1;

 

       public void setGraphics(Graphics gra) {

              g = (Graphics2D) gra;

       }

 

       public void mouseClicked(MouseEvent e) {

              if (e.getClickCount() == 2) {// 双击

                     for (int i = 0; i < 25000; i++) {

                            double a = 0.4, b = 1, c = 0;

                            // x1 = y - Math.signum(x) * Math.pow(Math.abs(b * x - c), 0.5);//到java目录中去寻找Math的应用,记得要在调用方法前面加上Math

                            x1 = y - Math.signum(x) * Math.sqrt(Math.abs(b * x - c));这两种方法都可以

                            y1 = a - x;

 

                            int m = (int) (x1 * 150 + 550);

                            int n = (int) (y1 * 150 + 550);

                            // System.out.println(m+"   "+x); 追踪输出值,这是一个检验差错的好方法

                            g.drawLine(m, n, m, n);

 

                            x = x1;

                            y = y1;

                     }

                     x = 0;

                     y = 0;

 

              } else {

                     for (int i = 0; i < 100000; i++) {

                            int  a = 1, b = 4, c = 60;

                            // x1 = y - Math.signum(x) * Math.pow(Math.abs(b * x - c), 0.5);

                            x1 = y - Math.signum(x) * Math.sqrt(Math.abs(b * x - c));

                            y1 = a - x;

                            int m = (int) (x1 * 1 + 300);// 乘数控制图形大小,加法控制图形的位置

                            int n = (int) (y1 * 1+ 300);//该图形的乘数不能太大,最好控制在0-5之间

                            g.drawLine(m, n, m, n);

                            x = x1;

                            y = y1;

                     }

                     x = 0;

                     y = 0;

 

              }

       }

 

       public void mousePressed(MouseEvent e) {

 

       }

 

       public void mouseReleased(MouseEvent e) {

 

       }

 

       public void mouseEntered(MouseEvent e) {

       }

 

       public void mouseExited(MouseEvent e) {

 

       }

 

       public static void main(String[] args) {

       }

 

}

输出结果:

感悟:刚开始的时候觉得很难,但是后来慢慢发现其实这种迭代分形应该是最容易的了。我们要注意的是把公式写对,用正确的方法去检验错误,问题基本上就迎刃而解了。通过这些分形的练习,我发现迭代分形的思路是这样的:首先,写完之后图形出不来一般是自己公式写错了,这个时候用System.out.println()去追踪代码输出的结果,从而找到自己出错的地方;接下来,如果图像不是自己想要的结果,则要注意你自己给图形扩大的程度了,也许是自己把图形放的太大了,也许是自己缩小了范围。

 

posted @ 2016-06-12 21:02  汗水与荣光  阅读(686)  评论(0编辑  收藏  举报