indexing description: "2D Perlin Noise" author: "Urs Doenni" date: "$Date$" revision: "$Revision$" class EM_1D_PERLIN_NOISE inherit EM_1D_PROCEDURE EM_PERLIN_NOISE create make, make_with_interpolator feature make_with_interpolator (an_interpolator: EM_INTERPOLATOR) is -- create the object using some interpololator do make set_interpolator (an_interpolator) end evaluate_at(pos: EM_VECTOR1D): DOUBLE is -- Evaluate 1D perlin noise at 'x' local oct: INTEGER do Result := 0.0 from oct := 1 until oct > num_of_octaves loop Result := Result + perlin_noise (pos.x, oct) oct := oct + 1 end end set_repeat_at(x: INTEGER) is -- repeat perlin noise after -- x in x-direction and after -- If x or y are 0, then the procedure isn't repeated in this direction require valid_x: x >= 0 do repeat_x := x end repeat_x: INTEGER feature perlin_noise(x: DOUBLE; an_octave: INTEGER): DOUBLE is -- compute perlin noise octave 'an_octave' at (x,y) require octave_valid: an_octave >= 1 local fraq_x: DOUBLE int_x: INTEGER nx0, nx1: DOUBLE factor: DOUBLE amplitude: DOUBLE mod_x: INTEGER do factor := 2 ^ (an_octave - 1).to_double amplitude := persistence ^ (an_octave - 1).to_double mod_x := (repeat_x * factor).floor int_x := (x * factor).floor fraq_x := (x * factor) - int_x int_x := modulo(int_x, mod_x) nx0 := get_noise (int_x) nx1 := get_noise (modulo(int_x + 1, mod_x)) Result := interpolator.interpolate (nx0, nx1, fraq_x) * amplitude ensure Result_valid: Result >= 0 and Result <= 1.0 end get_noise(an_x: INTEGER): DOUBLE is -- returns an integer value between 0 and largest_noise local x: INTEGER tmp_x: INTEGER do x := an_x tmp_x := perm_array.item(modulo(x, random_size).abs) Result := random_array.item (modulo(tmp_x, random_size).abs) * noise_factor ensure Result >= 0 and Result <= 1.0 end end