|
Author |
Topic: Math for Converting values (Read 218 times) |
|
Orkan Telhan Guest
|
Math for Converting values
« on: Jan 26th, 2004, 7:44am » |
|
Another very newbie question: How can one convert a value ranging from (0,255) to (0,1)? And is there a function that does that? thanks,
|
|
|
|
TomC
|
Re: Math for Converting values
« Reply #1 on: Jan 26th, 2004, 10:57am » |
|
You need to divide by 255. Be careful to make sure that your original value is a float, or converted to a float for the calculation. If you use an integer then values below 255 will get truncated to 0. e.g. Correct: Code: int value = 34; float newValue = float(value) / 255; println(newValue); // outputs 0.13333334 |
| Incorrect: Code: int value = 34; float newValue = value / 255; println(newValue); // outputs 0 |
| An alternative way to enforce the precision is to divide by 255.0f e.g. for all the numbers between 0 and 255 inclusive, Code: for (int i = 0; i <= 255; i++) { float value = i / 255.0f; println(value); } |
|
|
|
|
|
fry
|
Re: Math for Converting values
« Reply #2 on: Jan 26th, 2004, 3:49pm » |
|
a super tiny addition.. in processing you can also use: float value = i / 255.0; since it automatically adds in the 'f' for you. or in java you can use: float value = i / 255f; (unlike c/c++) so that it's easier to read than 255.0f;
|
|
|
|
|