summaryrefslogtreecommitdiff
path: root/util.c
blob: 278e192f193f032ea1f4a3150056f8485967d753 (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//a collection of utility methods

#include "rubik-internal.h"
#include <math.h>
#include <float.h>

float
randf(float x)
{ //return random number in range [0,x)
    return rand()/(((double)RAND_MAX + 1) / x);
}

float
minimum(float x, float y)
{
    return ((x) < (y) ? (x) : (y));
}

float
maximum(float x, float y)
{
    return ((x) > (y) ? (x) : (y));
}

void
setColor(float* color, float r, float g, float b, float a,
         float randomOffset, float randomness)
{
    int i;
    float ro = randf(randomOffset);
    
    color[0]=r+ro+randf(randomness);
    color[1]=g+ro+randf(randomness);
    color[2]=b+ro+randf(randomness);
    color[3]=a;

    for (i=0; i<4; i++)
    {
	if (color[i]<0)
	    color[i]=0;
	else if (color[i]>1)
	    color[i]=1;
    }
}
void
setSpecifiedColor(float* color, int c)
{
    switch (c)
    {
    case RANDOMCOLOR:
	setColor(color, 0, 0, 0, 1, 0, 1);
	break;
    case BLACK:
	setColor(color, 0, 0, 0, 1, 0, 0);
	break;
    case BLUE:
	setColor(color, 0, 0, 1, 1, 0, 0);
	break;
    case BROWN:
	setColor(color, 0.5, 0.25, 0.05, 1, 0, 0);
	break;
    case CYAN:
	setColor(color, 0, 1, 1, 1, 0, 0);
	break;
    case GREEN:
	setColor(color, 0, 1, 0, 1, 0, 0);
	break;
    case GREY:
	setColor(color, 0.5, 0.5, 0.5, 1, 0, 0);
	break;
    case ORANGE:
	setColor(color, 1, 0.5, 0, 1, 0, 0);
	break;
    case PINK:
	setColor(color, 1, 0, 1, 1, 0, 0);
	break;
    case PURPLE:
	setColor(color, 0.5, 0, 1, 1, 0, 0);
	break;
    case RED:
	setColor(color, 1, 0, 0, 1, 0, 0);
	break;
    case YELLOW:
	setColor(color, 1, 1, 0, 1, 0, 0);
	break;
    case WHITE:
	setColor(color, 1, 1, 1, 1, 0, 0);
	break;
    }
}
void
rotateClockwise(squareRec * square)
{

    int m, j, k;

    for (m=0; m<hStrips/2; m++)
    {
	for (j=0; j<hStrips-1-2*m; j++)
	{
	    int pos1 = (vStrips-1-j-m);
	    int pos2 = j+m;
	    squareRec tempSquare2 = square[pos2*hStrips+m];
	    square[pos2*hStrips+m] = square[m*hStrips+pos1];
	    square[m*hStrips+pos1] = square[pos1*hStrips+(vStrips-1-m)];
	    square[pos1*hStrips+(vStrips-1-m)] = square[(vStrips-1-m)*hStrips+pos2];
	    square[(vStrips-1-m)*hStrips+pos2] = tempSquare2;
	}
    }

    for (k=0; k<vStrips; k++)
    {
	for (j=0; j<hStrips; j++)
	{
	    square[j*hStrips+k].psi = (square[j*hStrips+k].psi+1)%4;
	}
    }
}
void rotateAnticlockwise(squareRec * square)
{
    int m, j, k;

    for (m=0; m<hStrips/2; m++)
    {
	for (j=0; j<hStrips-1-2*m; j++)
	{
	    int pos1 = (vStrips-1-j-m);
	    int pos2 = j+m;
	    squareRec tempSquare2 = square[pos2*hStrips+m];
	    square[pos2*hStrips+m] = square[(vStrips-1-m)*hStrips+pos2];
	    square[(vStrips-1-m)*hStrips+pos2] = square[pos1*hStrips+(vStrips-1-m)];
	    square[pos1*hStrips+(vStrips-1-m)] = square[m*hStrips+pos1];
	    square[m*hStrips+pos1] = tempSquare2;
	}
    }

    for (k=0; k<vStrips; k++)
    {
	for (j=0; j<hStrips; j++)
	{
	    square[j*hStrips+k].psi = (square[j*hStrips+k].psi+4-1)%4;
	}
    }
}