Advertisement
Guest User

mandelbrot

a guest
Oct 8th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Codec.Picture
  2. import Codec.Picture.Types
  3. import System.Environment
  4.  
  5. main :: IO ()
  6. main = do
  7.     putStrLn "Beginning rendering of image."
  8.     makenframes 1 5
  9.  
  10. makenframes :: Int -> Int -> IO ()
  11. makenframes n end
  12.                             | n >= end = putStrLn "Completed"
  13.                             | otherwise    = do
  14.                                                                 savePngImage ("frames\\"++(show n)++".jpg") (generateFractal n)
  15.                                                                 makenframes (n+1) end
  16.  
  17.  
  18. width :: Int
  19. width = 200
  20.  
  21. height :: Int
  22. height = 100
  23.  
  24. generateFractal :: Int -> DynamicImage
  25. generateFractal n = ImageRGB8 $ generateImage (mandelbrot n) width height
  26.  
  27. iters :: Int
  28. iters = 25
  29.  
  30. palette :: [PixelRGB8]
  31. palette = foldr (\a -> \b -> (PixelRGB8 (fromIntegral a*13) (fromIntegral a*10) 0):b) [] [0..iters]
  32.  
  33. mandelbrot :: Int -> Int -> Int -> PixelRGB8
  34. mandelbrot x0 y0 n = getColor 0 0 0 n
  35.     where getColor :: Float -> Float -> Int -> Int -> PixelRGB8
  36.           getColor x y i n = if x*x + y*y < 2*2 && i < iters
  37.                            then getColor (x*x - y*y + (scaleX x0 n)) (2*x*y + (scaleY y0 n)) (i+1) n
  38.                            else palette!!i
  39.  
  40. scaleX :: Int -> Int -> Float
  41. scaleX x n = (3.5/ (fromIntegral n) * (fromIntegral x)) / (fromIntegral width) - 2.5
  42.  
  43. scaleY :: Int -> Int -> Float
  44. scaleY y n = (2/ (fromIntegral n) * (fromIntegral y)) / (fromIntegral height) - 1.0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement