Scale floats -7.8f/24.1f to range like 0.0f/1.0f. Example problem.

Hello
I'm trying to scale the output of the diamond nose maker to values to 0.0f to 1.0f. From any pair of mininumY to maximumY. I was able to get it to partially work but stuck. I'm not sure how the final line should be to convert signed floats to a 0 to 1 range keeping the original scale.

Here is the code.

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
 bool Image::generateDiamondMethod1 (float * buffer,const int &width, const int &height, const float &maxYcoords,const float &minYcoords)
{
    float maxY=2048;
    float minY=-2048;


    /// set range keeping scale
    float NewMax = 1.0f;
    float NewMin = 0.0f;
    float NewRange = (NewMax - NewMin);

    float lowfloat=0.0f;
    float highfloat=0.0f;


(rest of code at end)
 //Calculate minY and maxY values
    for (int i = 0; i<DATA_SIZE-1; i++)
    {


        for(int j=0; j<DATA_SIZE-1; j++)
        {
            if (diamond[i][j] > maxY)
                maxY = diamond[i][j];
            if (diamond[i][j] < minY)
                minY = diamond[i][j];
        }
    }



    //print out the data
        for(int x=0; x < DATA_SIZE-1; x++){
                for(int y=0; y < DATA_SIZE-1; y++){

                        //populate the point struct
                        float floaty = diamond[x][y];

						//change range to 0..1
						diamond[x][y] = (floaty - minY) / (maxY - minY);
                }

        }

    // Set range
    float OldRange = (highfloat-lowfloat);

    // loop through all the floats then convert to grayscale setting the color basis to .5 (forcing values 0 to 1)
    for(unsigned x = 0; x<width; x++)
    {
        for(unsigned y = 0; y<height; y++)
        {
            /// incremennt memory which seems to work
            int index = x+(y*width);

            float value=diamond[x][y];
            buffer[index]=value;
        }
    }
Last edited on
Topic archived. No new replies allowed.