Created
July 5, 2014 16:06
-
-
Save samrat/3a7ba17eb91266829d2f to your computer and use it in GitHub Desktop.
Failed attempt at Perlin- not random enough!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Generator as Gen | |
import Generator.Standard as Gs | |
(w,h) = (100,100) | |
-- Vectors | |
type Vec2 = { x : Float | |
, y : Float | |
} | |
add : Vec2 -> Vec2 -> Vec2 | |
add a b = { x = a.x + b.x | |
, y = a.y + b.y | |
} | |
sub : Vec2 -> Vec2 -> Vec2 | |
sub a b = { x = a.x - b.x | |
, y = a.y - b.y | |
} | |
dot : Vec2 -> Vec2 -> Float | |
dot a b = (a.x * b.x) + (a.y * b.y) | |
tupleToVec : (Float, Float) -> Vec2 | |
tupleToVec (x,y) = { x=x, y=y } | |
-- | |
locs : (Vec2, Vec2, Vec2, Vec2) -> Vec2 -> (Vec2, Vec2, Vec2, Vec2) | |
locs (g1,g2,g3,g4) p = | |
( sub p g1, sub p g2, sub p g3, sub p g4 ) | |
stuv : (Vec2, Vec2, Vec2, Vec2) -> (Float, Float, Float, Float) | |
stuv locs = | |
let [pr1, pr2, pr3, pr4] = prGradients locs | |
(l1, l2, l3, l4) = locs | |
in ( dot pr1 l1 | |
, dot pr2 l2 | |
, dot pr3 l3 | |
, dot pr4 l4 | |
) | |
prGradient {x,y} = | |
let gen = Gs.generator (round x) | |
(gx, gen') = Gen.float gen | |
(gy, gen'') = Gen.float gen' | |
in tupleToVec (x*gx, y*gy) | |
prGradients gridPoints = | |
let (a, b, c, d) = gridPoints | |
gradients = map prGradient [a,b,c,d] | |
in gradients | |
noise : Vec2 -> Float | |
noise p = | |
let (x0, x1) = (toFloat <| floor p.x, toFloat <| ceiling p.x) | |
(y0, y1) = (toFloat <| floor p.y, toFloat <| ceiling p.y) | |
gridPoints = ( tupleToVec (x0, y0) | |
, tupleToVec (x1, y0) | |
, tupleToVec (x0, y1) | |
, tupleToVec (x1, y1)) | |
(g1,g2,g3,g4) = gridPoints | |
dX = p.x - g1.x | |
wtX = (3*dX^2) - (2*dX^3) | |
(s,t,u,v) = stuv (locs gridPoints p) | |
a = s + (wtX * (t-s)) | |
b = u + (wtX * (v-u)) | |
dY = p.y - g1.y | |
wtY = (3*dY^2) - (2*dY^3) | |
z = a + (wtY * (b-a)) | |
in z | |
drawPixel : (Float, Float) -> Element | |
drawPixel (x,y) = | |
spacer 1 1 | |
|> color (grayscale (noise (tupleToVec (x, y)))) | |
draw = map (\x -> map (\y -> drawPixel (x/w, y/h)) [1..w]) [1..h] | |
|> map (flow right) | |
|> flow down | |
|> container w h topLeft | |
main = draw |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment