Google Go and the Mandelbrot Set
November 13, 2009 on 12:30 pm | In english, hacking, linux |Learning a new programming language is fun. Here is my version of calculating the mandelbrot set and save the result as a PNG picture with Google Go.
package main
import ( "image";
"image/png";
"bufio";
"fmt";
"os";
"math";
"time"; )
func PointIteration( cx, cy, maxValue float64, maxIter uint8) uint8{
quadValue := float64(0.0);
iter := uint8(0);
x := float64(0.0);
y := float64(0.0);
for quadValue <= maxValue && iter < maxIter {
xt:= ( x * x ) - ( y * y) + cx;
yt:= ( float64(2.0) * x * y ) + cy;
x = xt;
y = yt;
iter++;
quadValue = ( x * x ) + ( y * y );
}
return iter;
}
func main(){
start := time.Seconds();
const pictureSize = 500;
img := image.NewRGBA(pictureSize,pictureSize);
f, err := os.Open("mandel.png", os.O_WRONLY|os.O_CREAT, 0666);
if err != nil {
fmt.Printf("Can't create picture file\n");
}
deltaX := math.Fabs(float64(-2.0 - 1.0)) / float64(pictureSize);
deltaY := math.Fabs(float64(-1.0 - 1.0)) / float64(pictureSize);
cx := float64(-2.0);
for x:=0;x<pictureSize;x++{
cx+=deltaX;
cy := float64(-1.0);
for y:=0;y<pictureSize;y++{
cy+=deltaY;
iter := PointIteration(cx,cy,255.0,255);
color := new(image.NRGBAColor);
color.A = 255;
color.R = iter;
color.G = iter;
color.B = iter;
img.Set(x,y,color);
}
}
w := bufio.NewWriter(f);
png.Encode(w,img);
w.Flush();
fmt.Printf("Seconds needed %d\n",time.Seconds() - start);
}
And here the result.

Next step is trying to improve the speed by using goroutines and channels.
1 Comment »
RSS feed for comments on this post. TrackBack URI











[...] Link to part 1 Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages. [...]
Pingback by Developer with some grey hairs » Google Go and the Mandelbrot Set Part II — November 17, 2009 #