我的Mandelbrot集没有生成。我的方程式怎么了?

时间:2019-10-15 19:27:55

标签: java swing math mandelbrot

//Generator class
public class Generator {
double jump;
double sizeModifier;
int iterationRate,range;
ComplexNumber c;
public Generator(double jump) {
    this.jump=jump;
    this.sizeModifier=40;
    this.iterationRate=10;
    this.range=100;
    c=new ComplexNumber();
}
    public void generateSet(){
        DrawSet ds = new DrawSet();
        int ticker=0;
        for(double i=-2*range;i<=range;i+=jump){
            for(double j=-2*range;j<=range;j+=jump){
                c= new ComplexNumber((i/range),(j/range));
                double fz=c.square().mod()+c.mod();
                //System.out.println("c mod is: "+c.mod());
                //System.out.println("fz is: "+fz);
                if (fz < 2) {
                    for(int k=0;k<=iterationRate;k++) {
                        //System.out.println("nc:"+nc);
                        ticker++;
                        //System.out.println("ticker:"+ticker);
                        if(ticker==iterationRate) {
                            ds.addPoint(i + 450, j + 450);

                    }
                    if(fz>=2){
                        break;
                    }
                    else {
                        fz = Math.pow(fz, 2) + 1;
                    }
                }
            }
            ticker=0;
        }
    }
}
//Drawset class
public class DrawSet extends JPanel {
private ArrayList<Point> Points;
private ArrayList<Point> nPoints;


GraphicWindow gw;
public DrawSet(){
    this.Points=new ArrayList<>();
    this.nPoints=new ArrayList<>();
    gw = new GraphicWindow(1000,1000);
    gw.add(this);

}
public void addPoint(double x,double y){
    int ix=(int)x;
    int iy=(int)y;
    //int iwidth=(int)width*sizeModifier;
    //int iheight=(int)height*sizeModifier;
    Point a=new Point(ix,iy);
    Points.add(a);
    //System.out.println(Points.size());
}
public void paintComponent(Graphics g) {
        int pointSize = 1;
        super.paintComponents(g);
        System.out.println(Points.size());
        for (int i = 0; i < Points.size(); i++) {
            g.setColor(Color.BLACK);
            g.drawOval((int) Points.get(i).getX(), (int) Points.get(i).getY(), pointSize, pointSize);
        }
    }

基本问题是,仅经过10次迭代,fz就永远不会小于2,因此没有绘制点。为什么是这样?我应该执行什么方程式?

我正在使用复数来生成值。我使用的类可以在这里看到: https://github.com/abdulfatir/jcomplexnumber/ (或在Abdul Fatir先生的堆栈溢出论坛Does Java have a class for complex numbers?上)

1 个答案:

答案 0 :(得分:0)

好的,观察上

  • Generator不应创建DrawSet的新实例
  • DrawSet不应创建GraphicWindow的新实例

这些是这些类或方法的职责。这将高度耦合您的代码并产生副作用。

相反,generate应该传回它创建的点,然后以您想要的任何方式应用这些点,Generator不在乎。

因为我无权访问您的完整代码,所以我只是随机破解了一系列要点,以说明要点。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                DrawSet drawSet = new DrawSet();
                Generator generator = new Generator(100);
                drawSet.setPoints(generator.generateSet());

                JFrame frame = new JFrame();
                frame.add(drawSet);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class DrawSet extends JPanel {

        private ArrayList<Point> points;
        private ArrayList<Point> nPoints;

        public DrawSet() {
            this.points = new ArrayList<>();
            this.nPoints = new ArrayList<>();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(500, 500);
        }

        public void setPoints(ArrayList<Point> points) {
            this.points = points;
            repaint();
        }

//        public void addPoint(double x, double y) {
//            int ix = (int) x;
//            int iy = (int) y;
//            //int iwidth=(int)width*sizeModifier;
//            //int iheight=(int)height*sizeModifier;
//            Point a = new Point(ix, iy);
//            Points.add(a);
//            //System.out.println(Points.size());
//        }

        @Override
        protected void paintComponent(Graphics g) {
            int pointSize = 5;
            super.paintComponents(g);
            for (int i = 0; i < points.size(); i++) {
                g.setColor(Color.BLACK);
                g.drawOval((int) points.get(i).getX(), (int) points.get(i).getY(), pointSize, pointSize);
            }
        }
    }

    public class Generator {

        double jump;
        double sizeModifier;
        int iterationRate, range;
        //ComplexNumber c;

        public Generator(double jump) {
            this.jump = jump;
            this.sizeModifier = 40;
            this.iterationRate = 10;
            this.range = 100;
            //c = new ComplexNumber();
        }

        public ArrayList<Point> generateSet() {
            Random rnd = new Random();
            ArrayList<Point> points = new ArrayList<>(range);
            for (int index = 0; index < range; index++) {
                int x = (int)(490 * rnd.nextDouble());
                int  y = (int)(490 * rnd.nextDouble());
                points.add(new Point(x, y));
            }
            return points;

//            DrawSet ds = new DrawSet();
//            int ticker = 0;
//            for (double i = -2 * range; i <= range; i += jump) {
//                for (double j = -2 * range; j <= range; j += jump) {
//                    c = new ComplexNumber((i / range), (j / range));
//                    double fz = c.square().mod() + c.mod();
//                    //System.out.println("c mod is: "+c.mod());
//                    //System.out.println("fz is: "+fz);
//                    if (fz < 2) {
//                        for (int k = 0; k <= iterationRate; k++) {
//                            //System.out.println("nc:"+nc);
//                            ticker++;
//                            //System.out.println("ticker:"+ticker);
//                            if (ticker == iterationRate) {
//                                ds.addPoint(i + 450, j + 450);
//
//                            }
//                            if (fz >= 2) {
//                                break;
//                            } else {
//                                fz = Math.pow(fz, 2) + 1;
//                            }
//                        }
//                    }
//                    ticker = 0;
//                }
//            }
        }
    }
}

我也将pointSize放大了,因此尽管我会考虑填补分数,但实际上可以看到结果。

如果Generator花费很长时间来执行其计算,则可以考虑使用SwingWorker来执行该操作。这将允许卸载操作以分离线程,而不阻塞主UI线程。然后,您可以使用SwingWorker来将每个点计算出来,也可以将整个点数生成后的所有点安全地反馈到主UI线程。

有关更多详细信息,请参见Worker Threads and SwingWorker

  

这是为什么?

摇摆很懒。除非它认为需要,否则它不会对UI进行重新绘制或更新。因此,如果更改UI所依赖的某些状态,则需要微调Swing鼓励它进行新的绘画传递。用最简单的术语来说,这意味着在已更改的组件上调用repaint