2014-12-01 100 views
1

我真的被卡住了,並且出於這個想法,我應該畫一個圓圈,這很好。但它可以用不同的分辨率來繪製,而且我無處可去,請幫助!我在這裏展示的代碼並不是試圖使用不同的解決方案,但我不知道該怎麼做。畫一個低分辨率的圈子

我會安排我遇到麻煩的課程,然後我會安排課程的其餘部分。還有一個簡要說明,該程序稍後將用於Mandlebrot套件。

問題是,圓形在高分辨率下工作正常,但是一旦我嘗試減少它,我會得到奇怪的線條樣式,就像它只繪製某些線條,然後繪製它不應該的線條, m懷疑問題是方法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/

感謝您的任何幫助,非常感謝! :)

+0

你不會碰巧有一個鏈接到GUI類的來源嗎?不是必需的,但是如果你能夠清楚地看到你在說什麼,並且可以從規範中複製,那麼調試就會變得更加容易,那就是很多輸入。 :) – 2014-12-02 00:35:50

+0

圓和Mandelbrot分形有什麼共同點?如果我必須畫一個圓,我會使用正弦和餘弦函數,這會產生一個_true_圓。 – karatedog 2014-12-02 11:21:40

回答

0

如果我正確地按照您的代碼和GUI類的操作。有兩件事你需要做。

1)當處於VERY_HIGH以外的分辨率時,只設置每個resolution點的顏色。這可能會工作,但只有當您更改您正在使用的像素的大小。沒有GUI類,沒有足夠的信息來說。我的感覺是,if語句if ((complex[i][j]).getAbs()>1應該是if ((complex[i][j]).getAbs()>resolution,而i和j的增量應該是1.再說一次,沒有GUI類,我不能說。

2)要更改pizel的大小,您只需更改w.putData(picture,1,1);命令即可閱讀w.putData(picture,resolution,resolution);

如果這是不正確的,到GUI源的鏈接或添加當前正在獲得的結果的圖片將有所幫助。

+0

嗯,我希望能夠訪問GUI源代碼,但是我只知道規範,所以它與我得到的信息大致相同。我會盡力在代碼中實現你的想法。 – BarksRosaRota 2014-12-02 13:01:30

0

好吧,我似乎已經解決了這個問題,渲染的方法應該是這樣的;

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 + =分辨率,我也把顏色[] []的索引w.getHeight/resolution + 1,和w.getWidth/resolution + 1

解釋是我只比較每個「分辨率」行(例如分辨率3的每第三行)和該行上的每個「分辨率」像素。因此,我只需要w.getWidth/resolution + 1元素(對於w.getHeight也是如此)。

然後我拿了picture[i/resolution][j/resolution],我覺得之前的問題是我保存了太多的元素,然後當pixelize增加時,我的像素太多了。

另外,w.putData(picture, resolution, resolution);,這意味着我擴大了像素大小,所以分辨率更低。

感謝所有麻煩的人在這裏幫助我。