summaryrefslogtreecommitdiffstats
path: root/mandelbrot.go
blob: 482850a8fbeed2fd66ac68b2f3f8bb6de4174a76 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main

import (
	"fmt"
)

func mandelbrot(x0, y0 float64) int {
	var x, y, x2, y2 float64
	for i := 0; i < 1000; i++ {
		if x2+y2 > 4 {
			return (i-1)%6 + 1
		}
		y = (x+x)*y + y0
		x = x2 - y2 + x0
		x2 = x * x
		y2 = y * y
	}
	return 0
}

func main() {
	const (
		xMin   = -2.
		xRange = 2.47
		yMin   = -1.12
		yRange = 2.24

		width  = 80
		height = 25

		xStep = xRange / float64(width)
		yStep = yRange / float64(height)
	)

	y := yMin
	for i := 0; i < height; i++ {
		x := xMin
		for j := 0; j < width; j++ {
			fmt.Printf("\033[4%dm ", mandelbrot(x, y))
			x += xStep
		}
		fmt.Println("\033[49m")
		y += yStep
	}
}