【问题标题】:Draw a low resolution circle画一个低分辨率的圆圈
【发布时间】:2014-12-01 22:58:36
【问题描述】:

我真的很困惑,对此没有想法,我应该画一个圆圈,这很好。但是它将能够以不同的分辨率绘制,我无处可去,请帮助!我在这里向您展示的代码不是尝试使用不同的分辨率,但我不知道该怎么做。

我会提出我遇到问题的课程,然后我会提出程序的其余部分。还有一个简短的说明,该程序稍后将用于 Mandlebrot 集。

问题是,圆圈在高分辨率下可以正常工作,但是一旦我尝试减小它,我就会得到奇怪的线条图案,就像它只绘制某些线条,然后绘制它不应该的线条,我怀疑问题出在方法render()。我还怀疑更具体的问题是 for 循环,或者我的向量的索引。如果我的解释不清楚,请告诉我。

RESOLUTION_VERY_HIGH 到 RESOLUTION_VERY_LOW 的值分别为 2048、1024、...、128。

package mandelbrot;
import java.awt.Color;

import se.lth.cs.ptdc.fractal.MandelbrotGUI;

public class Generator {
    public Generator() {

    }

    public void render(MandelbrotGUI w) {
        w.disableInput();
        int resolution = 1;

         switch (w.getResolution()) {
            case MandelbrotGUI.RESOLUTION_VERY_HIGH:
             resolution = 1;
             break;
            case MandelbrotGUI.RESOLUTION_HIGH:
              resolution = 3;
              break;
            case MandelbrotGUI.RESOLUTION_MEDIUM:
              resolution = 5;
              break;
            case MandelbrotGUI.RESOLUTION_LOW:
              resolution = 7;
              break;
            case MandelbrotGUI.RESOLUTION_VERY_LOW:
              resolution = 9;
              break;
            }

        Complex complex[][] = mesh(w.getMinimumReal(), w.getMaximumReal(), w.getMinimumImag(),
                w.getMaximumImag(), w.getWidth(), w.getHeight());

        Color[][] picture = new Color[w.getHeight()][w.getWidth()];

        for (int i = 0; i<w.getHeight(); i+=resolution) {
            for (int j = 0; j<w.getWidth(); j+=resolution) {
                if ((complex[i][j]).getAbs()>1) {
                    picture[i][j] = Color.WHITE;
                }
                else {
                    picture[i][j] = Color.BLUE;
                }
            }
        }

        w.putData(picture, 1, 1);
        w.enableInput();
    }

    private Complex[][] mesh(double minReal, double maxReal, double minImaginary,
                                double maxImaginary, int width, int height) {

        Complex[][] matrice = new Complex[height][width];
        for (int i = 0; i<height; i++) {
            for (int j = 0; j<width; j++) {
                matrice[i][j] = new Complex((minReal + ((maxReal-minReal)/width)*j),
                                            ((maxImaginary - (((maxImaginary - minImaginary)/height)*i))));

            }
        }
        return matrice;
    }
}

我怀疑你是否需要它们,但这里是其他类;

我的主;

package mandelbrot;
import se.lth.cs.ptdc.fractal.MandelbrotGUI;

public class Mandelbrot {
    public static void main (String[] args) {
        MandelbrotGUI w = new MandelbrotGUI();
        Generator generator = new Generator();
        boolean zoomValue = false;

        while (true) {
            switch(w.getCommand()) {

                case MandelbrotGUI.QUIT: System.exit(0);
                break;

                case MandelbrotGUI.RENDER:
                    generator.render(w);
                break;

                case MandelbrotGUI.RESET:
                w.clearPlane();
                w.resetPlane();
                zoomValue = false;
                break;

                case MandelbrotGUI.ZOOM:
                    if (zoomValue) {
                            w.clearPlane();
                            generator.render(w);
                            break;
                    }
                    else {break;}
            }
        }
    }
}

这是我写的 Complex 类;

package mandelbrot;

public class Complex {
    double real;
    double imaginary;

    public Complex (double real, double imaginary) {
        this.real = real;
        this.imaginary = imaginary;
    }

    double getReal() {
        return real;
    }
    double getImaginary() {
        return imaginary;
    }

    double getAbs() {
        return Math.hypot(real, imaginary);
    }

    void add(Complex c) {
        real += c.getReal();
        imaginary += c.getImaginary();
    }

    void multiply(Complex c) {
        real = (real*c.getReal()) - (imaginary*c.getImaginary());
        imaginary = (real*c.getImaginary()) + (imaginary*c.getReal());
    }
}

您可以在此处找到 GUI 的规范; http://fileadmin.cs.lth.se/cs//Education/EDA016/javadoc/cs_eda016_doc/

感谢您的帮助,非常感谢! :)

【问题讨论】:

  • 您不会碰巧有指向 GUI 类源的链接吧?不是必需的,但如果你能准确地看到你在说什么,并且我可以从规范中重现,那么调试会更容易,那就是很多打字。 :)
  • 圆和 Mandelbrot 分形有什么共同点?如果我必须画一个圆,我会使用正弦和余弦函数,这会产生一个 true 圆。

标签: java graphics fractals


【解决方案1】:

如果我正确地按照你的代码和GUI类的操作。您需要做两件事。

1) 当分辨率不是 VERY_HIGH 时,您只需为每个 resolution 点设置颜色。这可能有效,但前提是您更改要绘制的像素的大小。没有 GUI 类,就没有足够的信息可说。我觉得 if 语句 if ((complex[i][j]).getAbs()&gt;1 应该是 if ((complex[i][j]).getAbs()&gt;resolution 并且 i 和 j 的增量应该是 1。同样,没有 GUI 类,我不能说。

2) 要更改pizel 的大小,您只需更改w.putData(picture,1,1); 命令以读取w.putData(picture,resolution,resolution);

如果这不正确,指向 GUI 源的链接或添加您当前获得的结果的图片会有所帮助。

【讨论】:

  • 好吧,我希望我能够访问 GUI 源代码,但我只获得了规范,所以它与我获得的信息大致相同。我会尝试在代码中实现你的想法。
【解决方案2】:

好吧,我似乎已经解决了这个问题,方法render应该是这样的;

public void render(MandelbrotGUI w) {
    w.disableInput();
    int resolution = 1;

     switch (w.getResolution()) {
        case MandelbrotGUI.RESOLUTION_VERY_HIGH:
         resolution = 1;
         break;
        case MandelbrotGUI.RESOLUTION_HIGH:
          resolution = 3;
          break;
        case MandelbrotGUI.RESOLUTION_MEDIUM:
          resolution = 5;
          break;
        case MandelbrotGUI.RESOLUTION_LOW:
          resolution = 7;
          break;
        case MandelbrotGUI.RESOLUTION_VERY_LOW:
          resolution = 9;
          break;
        }

    Complex complex[][] = mesh(w.getMinimumReal(), w.getMaximumReal(), w.getMinimumImag(),
            w.getMaximumImag(), w.getWidth(), w.getHeight());

    Color[][] picture = new Color[w.getHeight()/resolution + 1][w.getWidth()/resolution + 1];

    for (int i = 0; i<w.getHeight(); i+=resolution) {
        for (int j = 0; j<w.getWidth(); j+=resolution) {
            if ((complex[i][j]).getAbs()>1) {
                picture[i/resolution][j/resolution] = Color.WHITE;
            }
            else {
                picture[i/resolution][j/resolution] = Color.BLUE;
            }
        }
    }

    w.putData(picture, resolution, resolution);
    w.enableInput();
}

增量是j+=分辨率和i+=分辨率,我也把Color[][]的索引放到w.getHeight/resolution + 1w.getWidth/resolution + 1

解释是我只比较每个“分辨率”行(例如,分辨率 3 的每第三行)和该行上的每个“分辨率”像素。因此我只需要w.getWidth/resolution + 1 元素(w.getHeight 也是如此)。

然后我拿了picture[i/resolution][j/resolution],我觉得之前的问题是我保存了太多元素,然后当像素大小增加时,我的像素太多了。

另外,w.putData(picture, resolution, resolution);,这意味着我扩大了像素大小,因此分辨率较低。

感谢这里的人们为帮助我而遇到的所有麻烦。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-12
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    • 2019-09-04
    • 1970-01-01
    相关资源
    最近更新 更多