Skip to content

Instantly share code, notes, and snippets.

@rsayers
Created June 13, 2019 15:52
Show Gist options
  • Save rsayers/9433c49c1df45502945c96b1aca72752 to your computer and use it in GitHub Desktop.
Save rsayers/9433c49c1df45502945c96b1aca72752 to your computer and use it in GitHub Desktop.
generate mandelbrot set
program mand
integer , parameter :: width = 1920*3
integer , parameter :: height = 1080*3
integer, dimension (width, height, 3) :: image
integer :: col, row, iteration, max, max_rgb, pan_x
real :: r_x, r_y, x, y, x_new, color_idx, zoom
pan_x = -300
pan_y = 0
max = 1000
max_rgb = 256**3
zoom = 1
do row = 0, height-1
do col = 0, width-1
c_re = ((pan_x+col)-width/2.0)*4.0/width
c_im = ((pan_y+row) - height/2.0)*4.0/width
c_re = c_re / zoom
c_im = c_im / zoom
x = 0.0
y = 0.0
iteration = 0
do while(x*x+y*y <= 4.0 .and. iteration<max)
x_new = x*x - y*y + c_re
y = 2*x*y + c_im
x = x_new
iteration = iteration + 1
end do
if (iteration<max) then
image(col+1, row+1, 1) = int(min(255, iteration*2))
image(col+1, row+1, 2) = int(min(255, iteration*5))
image(col+1, row+1, 3) = int(min(255, iteration*10))
else
image(col+1, row+1, 1) = 0
image(col+1, row+1, 2) = 0
image(col+1, row+1, 3) = 0
end if
end do
end do
open (10, file = 'out.ppm')
write (10, '(a/ i0, 1x, i0/ i0)') 'P3', width, height, 255
do row = 0, height-1
do col = 0, width-1
write (10, '(i0)') image(col+1, row+1, 1), image(col+1, row+1, 2), image(col+1, row+1, 3)
end do
end do
close(10)
end program mand
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment