Re: Converting floats to a specific range

Richard Fabian <[email protected]> Fri, 7 May 2010 13:06:39 +0100
Newsgroups gmane.games.devel.algorithms
Message-ID <[email protected]>
I'll let you use the code i coded for this free of charge!

float makeRight( float val, float wrapsize )
{
 val /= wrapsize;
 switch( (int)val )
{
case 0:
 return wrapsize * val;
case 1:
 return wrapsize * (val-1.0f);
case 2:
 return wrapsize * (val-2.0f);
case 3:
 return wrapsize * (val-3.0f);
case -1:
 return wrapsize * (val+1.0f);
case -2:
 return wrapsize * (val+2.0f);
case -3:
 return wrapsize * (val+2.0f);
}
// just in case you haven't written the case for the incoming value:
return 0.0f;
}

On 7 May 2010 12:36, Zafar Qamar <[email protected]> wrote:

> Hi,
>
> I'm sure this problem is pretty simple, but here goes...
>
> I have a set of float numbers that I'm trying to auto-wrap within a
> specific range of 0.0 to wrapSize (wrapSize is positive, and not a power of
> 2)
>
> So, if I choose a wrapSize of 1.0 then I want the following to happen when
> I feed in numbers...
>
> -739.8 --> 0.2
> :
> :
> -1.1 --> 0.9
> -1.0 --> 0.0
> -0.9 --> 0.1
> :
> -0.2 --> 0.8
> -0.1 --> 0.9
> 0.0 --> 0.0
> 0.1 --> 0.1
> :
> 0.9 --> 0.9
> 1.0 --> 0.0
> 1.1 --> 0.1
> :
>
> Can anyone think of a bit of maths for this please? Note that it needs to
> work for any positive value of wrapSize (not just 1.0)
>
> Cheers
> Zafar
>
> **********************************************************************************
> Disclaimer
>
> The information and attached documentation in this e-mail is intended for
> the use of the addressee only and is confidential. If you are not the
> intended recipient please delete it and notify us immediately by telephoning
> or e-mailing the sender. Please note that without Codemasters’ prior written
> consent any form of distribution, copying or use of this communication or
> the information in it is strictly prohibited and may be unlawful.
>
> Attachments to this e-mail may contain software viruses. You are advised to
> take all reasonable precautions to minimise this risk and to carry out a
> virus check on any documents before they are opened.
>
> Any offer contained in this communication is subject to Codemasters’
> standard terms & conditions and must be signed by both parties. Except as
> expressly provided otherwise all information and attached documentation in
> this e-mail is subject to contract and Codemasters’ board approval.
> Any views or opinions expressed are solely those of the author and do not
> necessarily represent those of Codemasters.
>
> This footnote also confirms that this email message has been swept by
> SurfControl for the presence of computer viruses.
>
> **********************************************************************************
>
>
>
> ------------------------------------------------------------------------------
>
> _______________________________________________
> GDAlgorithms-list mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/gdalgorithms-list
> Archives:
> http://sourceforge.net/mailarchive/forum.php?forum_name=gdalgorithms-list




-- 
fabs();
Just because the world is full of people that think just like you, doesn't
mean the other ones can't be right.

------------------------------------------------------------------------------

_______________________________________________
GDAlgorithms-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gdalgorithms-list
Archives:
http://sourceforge.net/mailarchive/forum.php?forum_name=gdalgorithms-list