Mandelbrot 在 Java 上的设置 - 我的算法有问题吗?
我被要求在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你必须扩大界限。 [-200,100],其中 Re 应该为 [-2,1],Im 应该为 [-1,1]。
I think that you have to wide boundaries. [-200,100], where you should have [-2,1] for Re and [-1,1] for Im.
由于
x
和y
是int
(还有SCALE
),因此您在这里进行整数除法。试试这个:
顺便说一句,这不是一个错误,但
add()
和multiply()
确实应该是Complex
的方法class,根据 Java 命名约定应大写。Since
x
andy
areint
s (andSCALE
too), you're doing integer division here.Try this instead:
By the way, this is not an error, but
add()
andmultiply()
should really be methods of theComplex
class, which should be capitalised according to the Java naming convention.