Solved

Testing a value in a range

Posted on 2003-10-26
7
243 Views
Last Modified: 2012-05-04
Hi there gurus.

I am writing a program that converts numerical values into colors

For example

if value is between 0.0 and 0.01 color value is RGB(255,255,0)
                            0.011 and 0.02 color value is RGB(255,255,1)
                                        ...
                                        ...
                                        ...
                            0.091 and 0.1  color value is RGB(255,255,255)
There will be up to 1000 of these ranges
Any idea how to code this.

Regards

Peter
0
Comment
Question by:Pishky
7 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 9622214
Many options here:

1) if...else if...else if...else...
2) Table lookup.  If this, write a quick/dirty program to generate your data tables rather then hand-code them.
3) Algorithm.  The above ranges, I assume, are for example only.  But your real situation most likely follows a pattern that can be modeled with an algorithm and implemented in code.
0
 
LVL 6

Expert Comment

by:GaryFx
ID: 9622220
If there are only a few ranges, you can just chain if-else statements.  If there are a lot, then store the RGB values in an array, and convert the ranges to integers (e.g. irange = static_cast<int>(range*100).  This second approach pays the cost of a float->int conversion, but it's much cleaner and easier to maintain, assuming your ranges are all hundredths apart.

Gary
0
 

Author Comment

by:Pishky
ID: 9622253
Thank you for the above but I am not sure that this is answering my question. The suggestions above imply a single mapping from one value to another. The problem I have is to map a range of values to a single value.

Regards

Peter
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 6

Accepted Solution

by:
GaryFx earned 125 total points
ID: 9622265
No, they don't.  Note the multiplication by 100 in my cast:

If r is in the range [0.01, 0.02) (using the standard meaning of [ for inclusive and ) for exclusive), then r*100 will be between 1.0 and 2.0, so that static_cast<int>(r) will be 1.  That's how you get the mapping of an entire range to a single int.

Also, your ranges were (0.01, 0.02] (i.e. exclusive at the low end and inclusive at the high end).  This could be an issue, but more likely it won't matter because most of these decimals can't be represented exactly in binary anyway.

Gary
0
 
LVL 3

Expert Comment

by:monkesdb
ID: 9623131
Well

Why not simply use the integer version of the colour.

All the colours are represented by a number in the range [0, 0xFFFFFF]. If your number is in the range [0, 1) just multiply by 2^24. Then if you need to convert it to rgb it's simple.

Value = (long) (YourNumber * 0x1000000);
Red = (Value 0xFF0000) >> 16;
Green = (Value 0xFF00) >> 8;
Blue = (Value 0xFF);

If you want to convert RGB to your number then...

Value = (Red << 16) + (Green << 8) + Blue;
YourNumber = (float) Value / 0x1000000;

0
 
LVL 9

Expert Comment

by:tinchos
ID: 10546155
No comment has been added lately, so it's time to clean up this question.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: GaryFx {http:#9622265}

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question