-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVectorUtil.pde
More file actions
127 lines (119 loc) · 2.63 KB
/
VectorUtil.pde
File metadata and controls
127 lines (119 loc) · 2.63 KB
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
// Consts --------------------
static PVector Zero = vector(0);
static PVector One = vector(1);
static PVector HalfColor = vector(127);
static PVector FullColor = vector(255);
// Constructors --------------
static PVector vector(float x) { return new PVector(x,x,x); }
static PVector vector(PVector v) { return new PVector(v.x,v.y,v.z); }
void set(PVector v, float f)
{
v.x = f; v.y = f; v.z = f;
}
// Color --------------------
PVector colorToVector(color c)
{
int r = c >> 16 & 0xFF;
int g = c >> 8 & 0xFF;
int b = c & 0xFF;
int a = c >> 24 & 0xFF;
return new PVector(r,g,b);
}
color vectorToColor(PVector v)
{
return vectorToColor(v, 255);
}
color vectorToColor(PVector v, float a)
{
return color(v.x, v.y, v.z, a);
}
color addColor(color c1, color c2)
{
int r = constrain( (c1 >> 16 & 0xFF) + (c2 >> 16 & 0xFF), 0,255 );
int g = constrain( (c1 >> 8 & 0xFF) + (c2 >> 8 & 0xFF), 0,255 );
int b = constrain( (c1 & 0xFF) + (c2 & 0xFF), 0,255 );
return color(r,g,b);
}
color mixColor(color c1, color c2, float pct1)
{
float pct2 = 1 - pct1;
int r = constrain( int((c1 >> 16 & 0xFF)*pct1 + (c2 >> 16 & 0xFF)*pct2), 0,255 );
int g = constrain( int((c1 >> 8 & 0xFF)*pct1 + (c2 >> 8 & 0xFF)*pct2), 0,255 );
int b = constrain( int((c1 & 0xFF)*pct1 + (c2 & 0xFF)*pct2), 0,255 );
return color(r,g,b);
}
PVector mix(PVector v1, PVector v2, float pct1)
{
PVector result = vector(0);
addInPlace( result, PVector.mult(v1, pct1) );
addInPlace( result, PVector.mult(v2, 1-pct1) );
return result;
}
// Logic --------------------
void constrainInPlace(PVector v, PVector min, PVector max)
{
v.x = constrain(v.x, min.x,max.x);
v.y = constrain(v.y, min.y,max.y);
v.z = constrain(v.z, min.z,max.z);
}
void floorInPlace(PVector v)
{
v.x = floor(v.x);
v.y = floor(v.y);
v.z = floor(v.z);
}
void ceilInPlace(PVector v)
{
v.x = ceil(v.x);
v.y = ceil(v.y);
v.z = ceil(v.z);
}
// Math --------------------
void addInPlace(PVector v1, PVector v2)
{
v1.x += v2.x;
v1.y += v2.y;
v1.z += v2.z;
}
PVector addAbs(PVector v1, PVector v2, boolean inPlace)
{
PVector result = inPlace ? v1 : vector(v1);
result.x += abs(v2.x);
result.y += abs(v2.y);
result.z += abs(v2.z);
return result;
}
void addAbs(PVector v1, PVector v2)
{
addAbs(v1, v2, true);
}
void subInPlace(PVector v1, PVector v2)
{
v1.x -= v2.x;
v1.y -= v2.y;
v1.z -= v2.z;
}
void multInPlace(PVector v1, PVector v2)
{
v1.x *= v2.x;
v1.y *= v2.y;
v1.z *= v2.z;
}
void multInPlace(PVector v, float f)
{
v.x *= f;
v.y *= f;
v.z *= f;
}
void divideInPlace(PVector v1, PVector v2)
{
v1.x /= v2.x;
v1.y /= v2.y;
v1.z /= v2.z;
}
void divideInPlace(PVector v, float f)
{
v.x /= f;
v.y /= f;
v.z /= f;
}