Mandelbrot 在 Java 上的设置 - 我的算法有问题吗?

发布于 2024-10-19 14:54:50 字数 2130 浏览 1 评论 0原文

我被要求在 Java 上显示 Mandelbrot 集,但我遇到了问题。我和我的老师都困惑于为什么它不能正确运行。

我认为这与算法或复杂类有关,因为除了正/负 1 和 0 之外的所有值在两次迭代后都会逃逸到无穷大。

import javax.swing.*;
import java.awt.*;

public class fractal {

        public class complex { double re; double im;
                public complex(double x, double y){
                    this.re =x;
                    this.im =y;
                }
                public double mag(){
                    return (Math.sqrt(re*re+im*im));
                }
        }


static int xcord = 500;
static int ycord = 500;

 public static void main(String[] args) {   
        JFrame myFrame = new JFrame("Question 10");
        myFrame.setSize(xcord,ycord);
        JPanel myPane = (JPanel) myFrame.getContentPane();
        myPane.add(new paint());
        myFrame.setVisible(true);
    }
}

class paint extends JComponent {

        public complex add(complex a, complex b){
            return new complex(a.re+b.re,a.im+b.im);
            }
         public complex multiply(complex a,complex b) {
              return new complex((a.re*b.re)-(a.im*b.im),(a.re*b.im)+(a.im*b.re));
            }




public void paint (Graphics g){

    final int SCALE =100; //pixels per unit
    int itr = 0;
    int max_itr = 30;
    Color clr = Color.black;
    g.translate(fractal.xcord/2, fractal.ycord/2); // Move origin to center of frame


    for (int x = -2*SCALE; x <= 1*SCALE; x++){

        for (int y = -1*SCALE; y <= 1*SCALE; y++){

            complex C = new complex(x/SCALE,y/SCALE); // math done on unscaled values
            complex z = C;
            itr = 0;

            while ( z.mag() <= 4.0 && itr < max_itr){ 

                z = add(multiply(z,z),C);   
                itr++;

                }

            if (itr == max_itr){
                clr = Color.black;
            }

            else{
                clr = new Color((int) Math.round(itr*8.5),(int) Math.round(itr*8.5),(int) Math.round(itr*8.5)); // Colouring of fractal
            }

            g.drawOval(x, y, 2, 2);
            g.setColor(clr);

            }
        }
    } 
}

I was asked to display the Mandelbrot set on Java but I have encountered a problem. My teacher and I both are stumped to why this doesn't run correctly.

I reckon it has something to do with the algorithm or the complex class since all values except positive/negative 1 and 0 escape to infinity after two iterations.

import javax.swing.*;
import java.awt.*;

public class fractal {

        public class complex { double re; double im;
                public complex(double x, double y){
                    this.re =x;
                    this.im =y;
                }
                public double mag(){
                    return (Math.sqrt(re*re+im*im));
                }
        }


static int xcord = 500;
static int ycord = 500;

 public static void main(String[] args) {   
        JFrame myFrame = new JFrame("Question 10");
        myFrame.setSize(xcord,ycord);
        JPanel myPane = (JPanel) myFrame.getContentPane();
        myPane.add(new paint());
        myFrame.setVisible(true);
    }
}

class paint extends JComponent {

        public complex add(complex a, complex b){
            return new complex(a.re+b.re,a.im+b.im);
            }
         public complex multiply(complex a,complex b) {
              return new complex((a.re*b.re)-(a.im*b.im),(a.re*b.im)+(a.im*b.re));
            }




public void paint (Graphics g){

    final int SCALE =100; //pixels per unit
    int itr = 0;
    int max_itr = 30;
    Color clr = Color.black;
    g.translate(fractal.xcord/2, fractal.ycord/2); // Move origin to center of frame


    for (int x = -2*SCALE; x <= 1*SCALE; x++){

        for (int y = -1*SCALE; y <= 1*SCALE; y++){

            complex C = new complex(x/SCALE,y/SCALE); // math done on unscaled values
            complex z = C;
            itr = 0;

            while ( z.mag() <= 4.0 && itr < max_itr){ 

                z = add(multiply(z,z),C);   
                itr++;

                }

            if (itr == max_itr){
                clr = Color.black;
            }

            else{
                clr = new Color((int) Math.round(itr*8.5),(int) Math.round(itr*8.5),(int) Math.round(itr*8.5)); // Colouring of fractal
            }

            g.drawOval(x, y, 2, 2);
            g.setColor(clr);

            }
        }
    } 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九命猫 2024-10-26 14:54:50

我认为你必须扩大界限。 [-200,100],其中 Re 应该为 [-2,1],Im 应该为 [-1,1]。

complex C = new complex(x/SCALE,y/SCALE);

I think that you have to wide boundaries. [-200,100], where you should have [-2,1] for Re and [-1,1] for Im.

complex C = new complex(x/SCALE,y/SCALE);
蓝梦月影 2024-10-26 14:54:50
              complex C = new complex(x/SCALE,y/SCALE); // math done on unscaled values

由于 xyint(还有 SCALE),因此您在这里进行整数除法。

试试这个:

              complex C = new complex((double)x/SCALE,(double)y/SCALE); // math done on unscaled values

顺便说一句,这不是一个错误,但 add()multiply() 确实应该是 Complex 的方法class,根据 Java 命名约定应大写。

              complex C = new complex(x/SCALE,y/SCALE); // math done on unscaled values

Since x and y are ints (and SCALE too), you're doing integer division here.

Try this instead:

              complex C = new complex((double)x/SCALE,(double)y/SCALE); // math done on unscaled values

By the way, this is not an error, but add() and multiply() should really be methods of the Complex class, which should be capitalised according to the Java naming convention.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文