A
Anand Hariharan
The problem simply put is as follows:
I have a list of entities each of which needs to be assigned a unique
triplet of values, each in the range [0.0, 1.0]. The triplet (0.0,
0.0, 0.0) is not allowed.
So, I came with this ('NUMENTITIES' is defined as a compile time
constant):
--- BEGIN ---
#include <math.h>
#include <stdio.h>
#include <float.h>
#include <assert.h>
int main(void)
{
const double Num = pow(NUMENTITIES + 1, (1.0/3.0));
const double FloorNum = floor(Num);
const double Step = Num / ( Num == FloorNum ? FloorNum - 1 :
FloorNum );
double r = 0.0, g = 0.0, b = Step;
int i;
printf("Num = %f, Step = %f\n", Num, Step);
for ( i = 1 ; i <= NUMENTITIES ; ++i )
{
printf("[%d] (%f, %f, %f)\n", i, (r/Num), (g/Num), (b/Num));
b += Step;
if ( b > Num + DBL_EPSILON )
{
b = 0;
g += Step;
if ( g > Num + DBL_EPSILON )
{
g = 0;
r += Step;
if ( r > Num + DBL_EPSILON && i != NUMENTITIES )
{
break;
}
}
}
}
assert( i == NUMENTITIES + 1 );
return 0;
}
--- END ---
This seems to do an adequate job. For example, for NUMENTITIES=8, it
generates:
Num = 2.080084, Step = 1.040042
[1] (0.000000, 0.000000, 0.500000)
[2] (0.000000, 0.000000, 1.000000)
[3] (0.000000, 0.500000, 0.000000)
[4] (0.000000, 0.500000, 0.500000)
[5] (0.000000, 0.500000, 1.000000)
[6] (0.000000, 1.000000, 0.000000)
[7] (0.000000, 1.000000, 0.500000)
[8] (0.000000, 1.000000, 1.000000)
No two triplet of values are the same, so the objective is met.
However, the 'r' component of the triplet is not used at all.
Am hoping that many of you will be able to tell me how to design a
loop construct so that I get a more 'uniform' distribution of values
that uses all three components.
Advance wishes to all for a happy & prosperous new year!
- Anand
PS: Feel free to show attitude (e.g., suggest that a high school kid
should be able to do it, but not actually offer any solution or
constructive criticism) but be rest assured this is not homework.
I have a list of entities each of which needs to be assigned a unique
triplet of values, each in the range [0.0, 1.0]. The triplet (0.0,
0.0, 0.0) is not allowed.
So, I came with this ('NUMENTITIES' is defined as a compile time
constant):
--- BEGIN ---
#include <math.h>
#include <stdio.h>
#include <float.h>
#include <assert.h>
int main(void)
{
const double Num = pow(NUMENTITIES + 1, (1.0/3.0));
const double FloorNum = floor(Num);
const double Step = Num / ( Num == FloorNum ? FloorNum - 1 :
FloorNum );
double r = 0.0, g = 0.0, b = Step;
int i;
printf("Num = %f, Step = %f\n", Num, Step);
for ( i = 1 ; i <= NUMENTITIES ; ++i )
{
printf("[%d] (%f, %f, %f)\n", i, (r/Num), (g/Num), (b/Num));
b += Step;
if ( b > Num + DBL_EPSILON )
{
b = 0;
g += Step;
if ( g > Num + DBL_EPSILON )
{
g = 0;
r += Step;
if ( r > Num + DBL_EPSILON && i != NUMENTITIES )
{
break;
}
}
}
}
assert( i == NUMENTITIES + 1 );
return 0;
}
--- END ---
This seems to do an adequate job. For example, for NUMENTITIES=8, it
generates:
Num = 2.080084, Step = 1.040042
[1] (0.000000, 0.000000, 0.500000)
[2] (0.000000, 0.000000, 1.000000)
[3] (0.000000, 0.500000, 0.000000)
[4] (0.000000, 0.500000, 0.500000)
[5] (0.000000, 0.500000, 1.000000)
[6] (0.000000, 1.000000, 0.000000)
[7] (0.000000, 1.000000, 0.500000)
[8] (0.000000, 1.000000, 1.000000)
No two triplet of values are the same, so the objective is met.
However, the 'r' component of the triplet is not used at all.
Am hoping that many of you will be able to tell me how to design a
loop construct so that I get a more 'uniform' distribution of values
that uses all three components.
Advance wishes to all for a happy & prosperous new year!
- Anand
PS: Feel free to show attitude (e.g., suggest that a high school kid
should be able to do it, but not actually offer any solution or
constructive criticism) but be rest assured this is not homework.