Re: Re-mapping a value (Java)

From: Randy (never_at_home.com.invalid)
Date: 11/28/03


Date: Fri, 28 Nov 2003 21:28:09 GMT

Leif Roar Moldskred wrote:
> Randy <never@home.com.invalid> writes:
>
>
>>Here is the method/function. I know that it is weak code:
>
>
> The main problem with your code is that you're using integers
> throughout, and particularly that you're doing integer division. This
> truncates towards the lowest integer value, so you lose a lot of
> precision. I.e. int a = 3 * ( 10 / 3 ) will give a = 9, as the
> expression ( 10 / 3 ) is truncated to the integer result 3.
>
> Here's a small function to rescale a point between two ranges:
>
> private static int rescalePoint( int originalLower, int originalUpper,
> int rescaledLower, int rescaledUpper,
> int originalPoint ) {
>
> float originalRange = (float) (originalUpper - originalLower);
> float rescaledRange = (float) (rescaledUpper - rescaledLower);
> int rescaledPoint = rescaledLower + Math.round( (float) (originalPoint -
> originalLower) * rescaledRange / originalRange);
> return rescaledPoint;
> } // end rescalePoint
>
> For your purpose, call it thus: rescalePoint( -5000, 0, 0, 255, p1)

   Leif,
     Thanks for the prompt and excellent reply. I have never had any
   formal training in programming (unfortunately), so sometimes I end
   up doing things in Rube Goldberg-ish ways. I thought the '\' was
   for integer division, and the '/' returned a floating point result.
   Maybe I'm thinking in QuickBASIC. Is there an easy way to incorporate
   bounds-checking, so that values are guaranteed not to fall beyond
   the range (rescaledLower to rescaledUpper)?