Browse Source

Now with non-losing rendering times and pretty colours!

master
Callum David OBrien 3 years ago
parent
commit
3a8f062c35
4 changed files with 32 additions and 14 deletions
  1. 1
    2
      README
  2. 1
    1
      config.mk
  3. 30
    11
      mandel.hs
  4. BIN
      out.ff.xz

+ 1
- 2
README View File

@@ -1,2 +1 @@
It renders the Mandelbrot set as a farbfeld image! I'm going to make
it less retardedly inefficient and add colours at some point :)
It renders the Mandelbrot set as a farbfeld image!

+ 1
- 1
config.mk View File

@@ -1,4 +1,4 @@
VERSION = 0.0.1
VERSION = 0.1.1

# Paths
PREFIX = /usr/local

+ 30
- 11
mandel.hs View File

@@ -1,7 +1,7 @@
{-# LANGUAGE CPP #-}

#define BOUND_MAX 0xfff
#define ITER_N 0xff
#define IN_COLOUR [0x2000, 0x1c00, 0x1800, 0xffff]
#define ITER_N 200

import Data.Complex
import Data.Word
@@ -9,8 +9,23 @@ import Data.ByteString.Builder
import System.Environment
import qualified Data.ByteString.Lazy as B

colours :: [[Word16]]
colours = [ [0xb300, 0x4800, 0x4800, 0xffff]
, [0xb300, 0x7d00, 0x4800, 0xffff]
, [0xb300, 0xb300, 0x4800, 0xffff]
, [0x7d00, 0xb300, 0x4800, 0xffff]
, [0x4800, 0xb300, 0x4800, 0xffff]
, [0x4800, 0xb300, 0x7d00, 0xffff]
, [0x4800, 0xb300, 0xb300, 0xffff]
, [0x4800, 0x7d00, 0xb300, 0xffff]
, [0x4800, 0x4800, 0xb300, 0xffff]
, [0x7d00, 0x4800, 0xb300, 0xffff]
, [0xb300, 0x4800, 0xb300, 0xffff]
, [0xb300, 0x4800, 0x7d00, 0xffff]
]

bounded :: RealFloat a => Complex a -> Bool
bounded z = magnitude z < BOUND_MAX
bounded z = magnitude z < 2

iterFs :: (a -> a) -> [(a -> a)]
iterFs = scanl (.) id . repeat
@@ -21,8 +36,9 @@ iters z = map ($ z) . iterFs
mandelIter :: Num a => a -> a -> a
mandelIter c z = z^2 + c

mandel :: RealFloat a => Complex a -> Bool
mandel z = foldl (&&) True
mandel :: RealFloat a => Complex a -> Int
mandel z = length
. takeWhile id
. map bounded
. take ITER_N
. iters 0
@@ -37,24 +53,27 @@ ffHeader w h = B.concat
$ [stringUtf8 "farbfeld", bs w, bs h]
where bs = word32BE . fromIntegral

pixel :: Bool -> B.ByteString
pixel :: Int -> B.ByteString
pixel x = B.concat
. map (toLazyByteString . word16BE)
. colour
$ x

colour :: Bool -> [Word16]
colour True = [0, 0, 0, 0xffff]
colour False = [0xffff, 0xffff, 0xffff, 0xffff]
colour :: Int -> [Word16]
colour x | x == ITER_N = IN_COLOUR
| otherwise = (!!) colours
. mod x
. length
$ colours

range :: Fractional t => t -> t -> Int -> [t]
range _ _ 0 = []
range min max n = take n
. scanl (+) min
. repeat
$ (max - min) / fromIntegral n

mandelRender :: RealFloat a => (a, a) -> (a, a) -> (Int, Int)
-> B.ByteString
mandelRender :: RealFloat a => (a, a) -> (a, a) -> (Int, Int) -> B.ByteString
mandelRender (r0, r1) (i0, i1) (w, h) = ffHeader w h `B.append` pixels
where pixels = B.concat
. map pixel

BIN
out.ff.xz View File


Loading…
Cancel
Save