-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDeflector.pde
More file actions
106 lines (94 loc) · 3.13 KB
/
Deflector.pde
File metadata and controls
106 lines (94 loc) · 3.13 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
// - - - - - - - - - - - - - - - - - - - - - - -
// DEFLECTOR CLASS
// - - - - - - - - - - - - - - - - - - - - - - -
class Deflector {
PVector p_location, p_locationPrev, p_velocity, sumAngles;
PVector[] pixelVector = new PVector[9];
float[] dPath_x;
float[] dPath_y;
boolean flipper = false;
int deflectionType;
Deflector(PVector mousePosition, int deflectionType_) {
deflectionType = deflectionType_;
initializeDeflector(mousePosition, deflectionType);
}
// - - - - - - - - - - - - - - - - - - - - - - -
void display() {
// Draw Rays
beginShape();
for (int i=1; i<dPath_x.length-1; i++) {
noFill();
strokeWeight(2);
stroke(150);
curveVertex(dPath_x[i-1], dPath_y[i-1]);
}
endShape();
}
// - - - - - - - - - - - - - - - - - - - - - - -
void initializeDeflector(PVector mousePosition, int deflectionSelection) {
p_location = new PVector(mousePosition.x, mousePosition.y);
p_locationPrev = new PVector(mousePosition.x, mousePosition.y);
dPath_x = new float[1];
dPath_y = new float[1];
dPath_x[0] = mousePosition.x;
dPath_y[0] = mousePosition.y;
deflectionType = deflectionSelection;
computeRays();
}
// - - - - - - - - - - - - - - - - - - - - - - -
void computeRays() {
p_velocity = new PVector(1, 1);
p_velocity.normalize();
for (int i=0; i<drawCycles; i++) {
p_velocity = computeDeflection(p_velocity);
p_location.add(p_velocity);
}
}
// - - - - - - - - - - - - - - - - - - - - - - -
PVector computeDeflection(PVector v_inbound) {
for (int i = 0; i < pixelVector.length; i++) {
pixelVector[i] = new PVector();
}
for (int i=-1; i<2; i++) {
for (int j=-1; j<2; j++) {
int iteration = (i+1)*3+j+1;
pixelVector[iteration].set(i, j);
pixelVector[iteration].normalize();
color pixelValue = pg.get(int(p_location.x+i+v_inbound.x), int(p_location.y+j+v_inbound.y));
int vectorFactor = hexToGrey(pixelValue);
pixelVector[iteration].mult(vectorFactor);
}
}
// Average Vectors
sumAngles = new PVector(0, 0);
for (int i = 0; i < pixelVector.length; i++) {
sumAngles.add(pixelVector[i]);
}
// Calculate Outbound Angle
PVector v_outbound = new PVector(0, 0);
sumAngles.rotate(PI/2);
v_outbound = v_inbound.get();
if (sumAngles.x != 0.0 && sumAngles.y != 0.0) { // If both, then not an edge
noFill();
stroke(255, 0, 0);
// Show tangent lines
// line(p_location.x-sumAngles.x, p_location.y-sumAngles.y, p_location.x+sumAngles.x, p_location.y+sumAngles.y);
float deltaAngle = v_inbound.heading() - sumAngles.heading();
if (deflectionType == 1) {
// bounce deflection
v_outbound.rotate(deflectionFactor*deltaAngle);
} else if (deflectionType == 2) {
// squiggle deflection
if (flipper) {
v_outbound.rotate(1.05*PI);
} else {
v_outbound.rotate(-1.05*PI);
}
flipper = !flipper;
}
dPath_x = append(dPath_x, p_location.x);
dPath_y = append(dPath_y, p_location.y);
}
return v_outbound;
}
}