-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
88 lines (80 loc) · 1.33 KB
/
utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "utils.h"
#include <math.h>
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
bool SRAND_INITIALIZED = false;
void initSRand(){
if (!SRAND_INITIALIZED) {
srand(time(0));
SRAND_INITIALIZED = true;
}
}
double randDouble(){
initSRand();
return 1.*(rand() % 100000) / 100000.;
}
int randInt(int max){ // [0;max[
if (max <= 0){
cerr << "randInt: Invalid max: "<<max << endl;
exit(-1);
}
initSRand();
return rand() % max;
}
int posToIndex(int length, int x, int y){
return x+length*y;
}
void indexToPos(int* pos, int length, int index){
pos[0] = index % length;
pos[1] = index / length;
}
void HSVtoRGB( float *r, float *g, float *b, float h, float s, float v ){
h*=360.;
int i;
float f, p, q, t;
if( s == 0 ) {
// achromatic (grey)
*r = *g = *b = v;
return;
}
h /= 60; // sector 0 to 5
i = floor( h );
f = h - i; // factorial part of h
p = v * ( 1 - s );
q = v * ( 1 - s * f );
t = v * ( 1 - s * ( 1 - f ) );
switch( i ) {
case 0:
*r = v;
*g = t;
*b = p;
break;
case 1:
*r = q;
*g = v;
*b = p;
break;
case 2:
*r = p;
*g = v;
*b = t;
break;
case 3:
*r = p;
*g = q;
*b = v;
break;
case 4:
*r = t;
*g = p;
*b = v;
break;
default: // case 5:
*r = v;
*g = p;
*b = q;
break;
}
}