ConvertRange function, need some help with it.

This is a discussion on ConvertRange function, need some help with it. within the C++ Programming forums, part of the General Programming Boards category; I cant figure out a good way of doing this one. Code: int ConvertRange(float fmin, float fmax, float cur, int ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    411

    ConvertRange function, need some help with it.

    I cant figure out a good way of doing this one.

    Code:
    int ConvertRange(float fmin, float fmax, float cur, int imin, int imax) {
    	int i;
    	
    	return i;
    }
    It should take the range of fmin to fmax and the relitave position cur, and return a relitave position that is from imin to imax.

    EXAMPLE
    fmin = 0.0f
    fmax = 1.0f
    cur = 0.1
    imin = 0
    imax = 10
    return = 1

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    The first step would seem to be a better definition of the problem. For example, is the range fmin to fmax the same as imin to imax, or is imim to imax inclusive but not necessarily the same as the range from fmin to fmax, or can fmin be less than imin or fmax larger than imax? Is the return value supposed to be the next biggest integer above cur but less than or the same as imax or is the return value determined by some other protocol?

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,702
    Use percentages...
    Code:
    Range A [1 - 10]
    Range B [50 - 100]
    
    If a value in range A is 4, what is the relative value in range B?
    
    Determine the length of A
    10 - 1 + 1 = 10
    
    Normalize the A range value to 0 by subtracting the range start
    4 - 1 = 3
    
    Determine the percentage of 3 out of 10 (length of A)
    (3 * 100) / 10 = 30%
    
    What value is "30% into" range B?
    
    Determine the length of B
    100 - 50 + 1 = 51
    
    Determine 30% of 51
    (30 * 51) / 100 = 15.3
    
    Add that value to the range start of B
    50 + 15.3 = 65.3
    For the least amount of %error, use doubles for your calculations. Round your final awnser and truncate it to an integer.

    gg

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    No, the ranges are necessarly the same. fmin will allways be smaller than fmax, and imin will allways be smaller than imax.

    Here is what I came up with... It works, but only for positive values, which is sufficient for now.
    Code:
    int ConvertRange(float fmin, float fmax, float cur, int imin, int imax) {
    	fmax -= fmin;				//makes the range from 0 to fmax
    	cur -= fmin;
    	float foff = 1.0f - fmax;	//the ammount to change fmax to make it from 0 to 1, make it % based
    	cur += cur*foff;			//fix cur so it stays in the right position in the relitave range
    	fmax += foff;				//fmax should be 1.0f / 100%
    	float ir = imax-imin;		//integer range
    	int i = imin + (int)(ir*cur+0.5);
    	return i;
    }

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    Revised after reading codeplugs post again

    Code:
    int ConvertRange(float fmin, float fmax, float cur, int imin, int imax) {
    	float per = (cur - fmin) / (fmax - fmin);
    	int i = per * (float)(imax - imin);
    	return i + imin;
    }

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Yeah, that looks good, and it's actually readable. I came up with this:
    return imin + static_cast<int> ((imax-imin) / (fmax-fmin) * (cur-fmin));

    but I think yours is more readable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21