Search code examples
haskellnoiseperlin-noise

integer to noise function in Haskell


I am trying to implement a basic perlin noise function (I know there is a library that does just that, I just want to try my own), and I have some problem with the integer to noise function used to generate deterministic noise from integer inputs.

the function I am trying to implement is defined there: http://libnoise.sourceforge.net/noisegen/index.html#continuousnoise

and my code so far look like this:

noise2d :: (Int32, Int32) -> Double
noise2d (x, y) =
    let m = x + y * 57
        n = (shiftR m 13) ^ m
        j = (n * (n * n * 15731 + 789221) + 1376312589) .&. 0x7fffffff
    in  1.0 - (fromIntegral j / 1073741824.0)

the code compile but I get the same result for any input, due to the fact that n is evaluated to 0.

Is there a better way to do that?


Solution

  • The problem stems from your use of the ^-operator. In C it is bitwise exclusive disjunction (xor), whereas in Haskell it is exponentiation. So, you just need to change the fourth line to

            n = (shiftR m 13) `xor` m
    

    and everything should be fine.