Problem coloring the Mandelbrot set on Android
I have a problem with coloring the Mandelbrot set. This is my onDraw() procedure:
@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]
}
}
}
This is how it looks at the Java Android emulator: http://i55.tinypic.com/14ctqi8.png
This is how I want it to look: http://i54.tinypic.com/nh1aqe.png It's written in Delphi, but the coloring part is actually the same:
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));
Could someone help me please? Greetz,
Henry
- Don't do complex calculations in
onDraw
if it can be helped. - In general, use
Math.floor()
instead of round in these instances, as you don't really want things to be rounded upwards here. - Would be clearer with
else if
for the second and third conditions of your colouring algorithm. It seems that the third one is winning out over the second...? - (this is the real problem :-) I bet you declared
n
andg
as integers! The divisions will be integer divisions unless you do this:
convert to double
(n/(double)g)
Remember an integer division e.g. 25000 / 25600
will == 0
, and therefore all those pixels will get (255,0,0)
As Sanjay said your have a problem with division.
if ((n/g) < (1/2)) {...
if (((n/g) < 1) && ((n/g) > 1/2)) {....
You could fix in Sanjay-way. But take attention 1/2=0 1.0/2=0.5. Or less readeble but a bit faster
if((2*n)<g){...
and
if((n<g)&&(2*n>g)){...
精彩评论