【问题标题】:Problem coloring the Mandelbrot set on Android在 Android 上着色 Mandelbrot 集的问题
【发布时间】:2011-08-09 22:52:04
【问题描述】:

我在为 Mandelbrot 集着色时遇到问题。 这是我的 onDraw() 过程:

@Override
protected void onDraw(Canvas canvas) {
   g = Math.round(60+(iter_count*(2500/16)));
   iter_count++;
   for(int xx = 1; xx <= xMax; xx++) {
       for(int yy = 1; yy <= yMax; yy++) {
           schirmzupap(ar,br,bi,ai,xx,yy,xMax,yMax);
           n = 1;
           zr0 = zr;
           zi0 = zi;
           while ((n<g) && (zr*zr+zi*zi<4)) {
               zrh = zr;
               zr = (zr*zr)-zi*zi+zr0;
               zi = zrh*zi+zi*zrh+zi0;
               n++;
           }
           if (n==g) {                                                //[Coloring]
               paint.setARGB(255,0,0,0);  
           }
           if ((n/g) < (1/2)) {
               paint.setARGB(255,Math.round((n/g)*255),0,0);
           }
           if (((n/g) < 1) && ((n/g) > 1/2)) {
               paint.setARGB(255,255,Math.round((n/g)*255),Math.round((n/g)*255));  
           }
           canvas.drawPoint(xx, yy, paint);                           //[/Coloring]
       }              
    }
}

Java Android 模拟器是这样的:http://i55.tinypic.com/14ctqi8.png

这就是我想要的样子:http://i54.tinypic.com/nh1aqe.png 是用 Delphi 写的,但着色部分其实是一样的:

if n=g then image1.canvas.Pixels[xx,yy]:=RGB2TColor(0,0,0);
if (n/g)<(1/2) then image1.canvas.Pixels[xx,yy]:=RGB2TColor(Round((n/g)*255),0,0);
if ((n/g)<(1)) AND ((n/g)>(1/2)) then image1.canvas.Pixels[xx,yy]:=RGB2TColor(255,Round((n/g)*255),Round((n/g)*255));

有人可以帮帮我吗? 问候,

亨利

【问题讨论】:

    标签: java android colors gradient mandelbrot


    【解决方案1】:
    1. 如果有帮助,请勿在 onDraw 中进行复杂计算。
    2. 一般来说,在这些情况下,请使用 Math.floor() 而不是 round,因为您不希望在此处向上舍入。
    3. 对于着色算法的第二个和第三个条件,使用else if 会更清楚。似乎第三个胜过第二个......?
    4. (这是真正的问题 :-) 我敢打赌您将 ng 声明为整数!除非您这样做,否则除法将是整数除法:

    转换成双倍

    (n/(double)g)
    

    记住整数除法,例如25000 / 25600== 0,因此所有这些像素将得到 (255,0,0)

    【讨论】:

      【解决方案2】:

      正如桑杰所说,你的分裂有问题。

      if ((n/g) < (1/2)) {...
      
      if (((n/g) < 1) && ((n/g) > 1/2)) {....
      

      您可以在 Sanjay-way 进行修复。但要注意 1/2=0 1.0/2=0.5。 或者可读性较差但速度更快

      if((2*n)<g){...
      

      if((n<g)&&(2*n>g)){...
      

      【讨论】:

      • 非常感谢! n 和 g 已经被声明为浮点数。原因是我必须写 1.0/2 而不是 1/2。
      猜你喜欢
      • 1970-01-01
      • 2014-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多