POV-Ray : Newsgroups : povray.general : QUESTION: Programming: Converting Floats to 255-Integers Server Time
31 Oct 2024 20:03:51 EDT (-0400)
  QUESTION: Programming: Converting Floats to 255-Integers (Message 11 to 12 of 12)  
<<< Previous 10 Messages Goto Initial 10 Messages
From: Alain
Subject: Re: QUESTION: Programming: Converting Floats to 255-Integers
Date: 28 Apr 2018 17:11:41
Message: <5ae4e38d@news.povray.org>
Le 18-04-27 à 16:40, Kenneth a écrit :
> The bare formula would be to simply multiply the vector by 255 (not 256, because
> 0 to 255 makes 256 entries, total.)
> 
> (A full 5-component vector as an example...)
> #declare BB = <.327,.586,.902,.431,.202>;
> #declare BB_LARGE = BB*255;
> // To test this...
> #debug concat("\n","BB_LARGE = <",vstr(5,BB_LARGE,", ",0,3),">","\n")
> 
> another example...
> #declare CC = <0,1,.5>;
> #declare CC_LARGE = CC*255;
> // to test this...
> #debug concat("\n","CC_LARGE = <",vstr(3,CC_LARGE,", ",0,3),">","\n")
> 
> To pick out just one of the values, you would use dot notation--
>      .red, .green, .blue, .filter, .transmit--
> #declare BB_LARGE_FILTER = (BB_LARGE.filter);
> // To test this...
> #debug concat("\n","BB_LARGE_FILTER = ",str(BB_LARGE_FILTER,0,3),"\n")
> 
> If you want to eliminate the fractional parts to get just the integers, you
> could use POV-Ray's built-in functions like int, floor, or ceil. Like...
>                 int(BB_LARGE.filter)
> 
> But the integer results-- using either of the three function choices-- may be
> 'one integer higher or lower' than what you want. You may have to decide in
> which 'direction' to go.. For example: if BB*255 turns out to be
>                 <.999,0,254.999,0,128.000>
> then int(BB_LARGE.red) would be 0.000-- because int 'rounds toward zero'; and
> int(BB_LARGE.blue) would be 254, not 255. That's the same result you would get
> when using floor. Using ceil(...) instead would have a similar but opposite
> effect-- ceil(BB_LARGE.red) would be 1.000, while ceil(BB_LARGE.blue) would be
> 255. BUT, if BB_LARGE.red happended to be .001, ceil would still make it 1.000.
> 
> Just a small detail to be aware of.
> 
>

The usual way to round to the closest integer is to add 0.5 then to take 
the integer part :
#declare IntValue = int(InValue + 0.5);


Post a reply to this message

From: Sven Littkowski
Subject: Re: QUESTION: Programming: Converting Floats to 255-Integers
Date: 28 Apr 2018 21:14:32
Message: <5ae51c78$1@news.povray.org>
Thanks!

---
Diese E-Mail wurde von AVG auf Viren geprüft.
http://www.avg.com


Post a reply to this message

<<< Previous 10 Messages Goto Initial 10 Messages

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.