namespace MrGameEng.WorldGen; /// /// A dense row-major grid of height values. Plain data with indexed access — fill it from a /// and read it from worldgen code. Values are arbitrary until /// rescales them into [0, 1]. /// public sealed class Heightmap { /// Grid width in cells. public int Width { get; } /// Grid height in cells. public int Height { get; } /// Row-major values, length ×. public float[] Values { get; } /// Creates an all-zero map of the given size. public Heightmap(int width, int height) { Width = width; Height = height; Values = new float[width * height]; } /// Height at (, ). public float this[int x, int y] { get => Values[y * Width + x]; set => Values[y * Width + x] = value; } /// Rescales all values into [0, 1] by min–max; a flat map becomes all zeros. public void Normalize() { var min = float.MaxValue; var max = float.MinValue; foreach (var v in Values) { if (v < min) { min = v; } if (v > max) { max = v; } } var range = max - min; if (range <= float.Epsilon) { Array.Clear(Values); return; } for (var i = 0; i < Values.Length; i++) { Values[i] = (Values[i] - min) / range; } } }