aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim.Terrain.BasicTerrain/Normalise.cs
blob: 507795bdd2e8ebfc6df8b1b8e28380c9fc8378f3 (plain)
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
using System;
using System.Collections.Generic;
using System.Text;

namespace OpenSim.Terrain.BasicTerrain
{
    static class Normalise
    {
        public static void normalise(float[,] map)
        {
            double max = findMax(map);
            double min = findMin(map);
            int w = map.GetLength(0);
            int h = map.GetLength(1);

            int x, y;

            for (x = 0; x < w; x++)
            {
                for (y = 0; y < h; y++)
                {
                    map[x, y] = (float)((map[x, y] - min) * (1.0 / (max - min)));
                }
            }
        }

        public static void normalise(float[,] map, double newmax)
        {
            double max = findMax(map);
            double min = findMin(map);
            int w = map.GetLength(0);
            int h = map.GetLength(1);

            int x, y;

            for (x = 0; x < w; x++)
            {
                for (y = 0; y < h; y++)
                {
                    map[x, y] = (float)((map[x, y] - min) * (1.0 / (max - min)) * newmax);
                }
            }
        }

        public static double findMax(float[,] map)
        {
            int x, y;
            int w = map.GetLength(0);
            int h = map.GetLength(1);
            double max = double.MinValue;

            for (x = 0; x < w; x++)
            {
                for (y = 0; y < h; y++)
                {
                    if (map[x, y] > max)
                        max = map[x, y];
                }
            }

            return max;
        }

        public static double findMin(float[,] map)
        {
            int x, y;
            int w = map.GetLength(0);
            int h = map.GetLength(1);
            double min = double.MaxValue;

            for (x = 0; x < w; x++)
            {
                for (y = 0; y < h; y++)
                {
                    if (map[x, y] < min)
                        min = map[x, y];
                }
            }

            return min;
        }
    }
}