Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Anders Mundt Due
goplay
Commits
fb46671c
Commit
fb46671c
authored
Nov 09, 2018
by
Anders Mundt Due
Browse files
pg62 :)
parent
866c5962
Changes
1
Hide whitespace changes
Inline
Side-by-side
mandelbrot/main.go
0 → 100644
View file @
fb46671c
// gopl.io/ch3/mandelbrot pg 61
// Mandelbrot emits a PNG image of the mandelbrot fractal
package
main
import
(
"image"
"image/color"
"image/png"
"math/cmplx"
"os"
)
func
main
()
{
const
(
xmin
,
ymin
,
xmax
,
ymax
=
-
2
,
-
2
,
+
2
,
+
2
width
,
height
=
800
,
600
)
img
:=
image
.
NewRGBA
(
image
.
Rect
(
0
,
0
,
width
,
height
))
for
py
:=
0
;
py
<
height
;
py
++
{
y
:=
float64
(
py
)
/
height
*
(
ymax
-
ymin
)
+
ymin
for
px
:=
0
;
px
<
width
;
px
++
{
x
:=
float64
(
px
)
/
width
*
(
xmax
-
xmin
)
+
xmin
z
:=
complex
(
x
,
y
)
// Image point (px, py) represents complex value z
img
.
Set
(
px
,
py
,
mandelbrot
(
z
))
}
}
png
.
Encode
(
os
.
Stdout
,
img
)
// NOTE: ignoring errors..
}
func
mandelbrot
(
z
complex128
)
color
.
Color
{
const
iterations
=
200
const
contrast
=
15
var
v
complex128
for
n
:=
uint8
(
0
);
n
<
iterations
;
n
++
{
v
=
v
*
v
+
z
if
cmplx
.
Abs
(
v
)
>
2
{
return
color
.
Gray
{
255
-
contrast
*
n
}
}
}
return
color
.
Black
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment