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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| import Graphics.Rendering.OpenGL
import Graphics.UI.GLUT
import Data.IORef
data Complex = C (Float, Float) deriving (Show,Eq)
instance Num Complex where
fromInteger n = C (fromIntegral n,0.0)
C (x,y) * C (z,t) = C (z*x - y*t, y*z + x*t)
C (x,y) + C (z,t) = C (x+z, y+t)
abs (C (x,y)) = C (sqrt (x*x + y*y),0.0)
signum (C (x,y)) = C (signum x , 0.0)
complex :: Float -> Float -> Complex
complex x y = C (x,y)
real :: Complex -> Float
real (C (x,y)) = x
im :: Complex -> Float
im (C (x,y)) = y
magnitude :: Complex -> Float
magnitude = real.abs
main :: IO ()
main = do
(progname,_) <- getArgsAndInitialize
initialDisplayMode $= [DoubleBuffered]
initialWindowSize $= Size 320 320
createWindow "Mandelbrot Set"
s <- prompt "Choisissez le set Couleur:"
displayCallback $= display s
putStr "PATIENTEZ - La fenetre avec l'image sera affichee a la fin du traitement"
--display d
mainLoop
display d = do
clear [ColorBuffer] -- make the window black
loadIdentity -- reset any transformation
preservingMatrix (drawMandelbrot d)
swapBuffers -- refresh screen
drawMandelbrot d =
renderPrimitive Points $ do
mapM_ drawColoredPoint (allPoints d)
where
drawColoredPoint (x,y,c) = do
color c -- set the current color to c
vertex $ Vertex3 x y 0
width = 320 :: GLfloat
height = 320 :: GLfloat
--allPoints :: [ GLfloat,GLfloat, Color3 GLfloat)]
allPoints d = [ ((x)/width,y/height, (colorFromValue d (mandel x y ))) |
x <- [-width..width],
y <- [-height..height]]
colorFromValue d n =
let
t :: Int -> GLfloat
t i = 0.5 + 0.5*sin( fromIntegral i / d )
in
Color3 (t n) (t (n+5)) (t (n+10))
mandel x y =
let r = realToFrac $ 2 * x / width
i = realToFrac $ 2 * y / height
in
f (complex r i) 0 64
f :: Complex -> Complex -> Int -> Int
f c z 0 = 0
f c z n = if (magnitude z > 2 )
then n
else f c ((z*z) + c) (n-1)
prompt :: String -> IO GLfloat
prompt s = do
putStrLn s
readLn
sss :: IO (GLfloat)
sss = do
putStrLn "entrez numero du s"
s <- readLn
return (s) |
Partager