-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday_8.pde
129 lines (103 loc) · 2.57 KB
/
day_8.pde
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
// (c) 2023 Joseph HENRY
// This code is licensed under MIT license (see LICENSE for details)
float easeOutBounce(float x) {
float n1 = 7.5625;
float d1 = 2.75;
if (x < 1 / d1) {
return n1 * x * x;
} else if (x < 2 / d1) {
return n1 * (x -= 1.5 / d1) * x + 0.75;
} else if (x < 2.5 / d1) {
return n1 * (x -= 2.25 / d1) * x + 0.9375;
} else {
return n1 * (x -= 2.625 / d1) * x + 0.984375;
}
}
float easeInOutBack(float x) {
float c1 = 1.70158;
float c2 = c1 * 1.525;
return x < 0.5
? (pow(2 * x, 2) * ((c2 + 1) * 2 * x - c2)) / 2
: (pow(2 * x - 2, 2) * ((c2 + 1) * (x * 2 - 2) + c2) + 2) / 2;
}
interface SDF {
float compute(int x, int y);
}
class ConstantSDF implements SDF {
float value;
ConstantSDF(float value) {
this.value = value;
}
float compute(int x, int y) {
return value;
}
}
class CircleSDF implements SDF {
float cx, cy, diam;
CircleSDF(float cx, float cy, float diam) {
this.cx = cx;
this.cy = cy;
this.diam = diam;
}
float compute(int x, int y) {
return dist(x, y, cx, cy) - diam / 2;
}
}
class SDFSubtract implements SDF {
SDF sdf1, sdf2;
SDFSubtract(SDF sdf1, SDF sdf2) {
this.sdf1 = sdf1;
this.sdf2 = sdf2;
}
float compute(int x, int y) {
return max(sdf1.compute(x, y), -sdf2.compute(x, y));
}
}
class SDFAdd implements SDF {
SDF sdf1, sdf2;
SDFAdd(SDF sdf1, SDF sdf2) {
this.sdf1 = sdf1;
this.sdf2 = sdf2;
}
float compute(int x, int y) {
return min(sdf1.compute(x, y), sdf2.compute(x, y));
}
}
void displaySDF(SDF sdf, float threshold) {
loadPixels();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
float val = easeOutBounce(constrain(sdf.compute(x, y), -threshold, threshold) / threshold);
pixels[x + y * width] = lerpColor(#f69333, #774e7e, val);
}
}
updatePixels();
}
float animValue = 0;
float animOffset(float offset) {
return cos(animValue + offset);
}
void setup() {
size(400, 400);
frameRate(30);
}
void draw() {
background(255);
float aCos = easeInOutBack((cos(animValue) + 1) / 2);
float mAcos = 1 - aCos;
SDF sdf = new SDFAdd(
new SDFSubtract(
new CircleSDF(width / 2, height / 2, 200),
new CircleSDF(width / 2, height / 2, 200)
),
new SDFSubtract(
new SDFAdd(
new CircleSDF(width / 2 + mAcos * width / 4, height / 2 + 50, 100),
new CircleSDF(width / 2 - mAcos * width / 4, height / 2 - 50, 100)
),
new CircleSDF(width / 2, height / 2, 100 * aCos)
)
);
displaySDF(sdf, 10 + aCos * 80);
animValue += 0.05;
}