-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphotogram.pde
211 lines (184 loc) · 6.21 KB
/
photogram.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
int numPoints = 100;
PVector[] points = new PVector[numPoints];
PVector[] noiseOffsets = new PVector[numPoints];
float[][] distances;
int k = 4;
float maxDistance;
// Parameters for controlling thickness and opacity
float thicknessMin = 0.5;
float thicknessMax = 4.0;
float opacityMin = 50;
float opacityMax = 255;
// Parameters for controlling Perlin noise movement
float noiseScale = 0.01;
float noiseSpeed = 0.01;
// Parameters for the soft boundary
int boundaryWidth = 800;
int boundaryHeight = 1600;
int boundaryX, boundaryY;
float boundaryForce = 0.1; // Force strength pushing points back inside
int[] keySecs = {5, 5+10, 5+10+5, 5+10+5+10, 5+10+5+10+5};
int[] keyframes = new int[keySecs.length];
void setup() {
size(1080, 1920, P3D);
stroke(255);
noFill();
frameRate(30);
boundaryX = (width - boundaryWidth) / 2;
boundaryY = (height - boundaryHeight) / 2;
// Generate random points inside the soft boundary
for (int i = 0; i < numPoints; i++) {
points[i] = new PVector(
random(boundaryX, boundaryX + boundaryWidth),
random(boundaryY, boundaryY + boundaryHeight),
random(-500, 500)
);
noiseOffsets[i] = new PVector(random(1000), random(1000), random(1000));
}
// Calculate distances between points
distances = new float[numPoints][numPoints];
for (int i = 0; i < numPoints; i++) {
for (int j = i + 1; j < numPoints; j++) {
float d = PVector.dist(points[i], points[j]);
distances[i][j] = d;
distances[j][i] = d;
if (d > maxDistance) {
maxDistance = d;
}
}
}
for (int i = 0; i < keySecs.length; i++) {
keyframes[i] = keySecs[i] * 30;
}
}
void draw() {
float dividerY;
// Calculate the divider position based on frameCount
if (frameCount <= keyframes[0]) {
background(255);
stroke(0);
dividerY = -1; // off-screen
} else if (frameCount <= keyframes[1]) {
background(25);
stroke(255);
dividerY = map(frameCount, keyframes[0], keyframes[1], 0, height / 2);
} else if (frameCount <= keyframes[2]) {
background(25);
stroke(255);
dividerY = height / 2;
} else if (frameCount <= keyframes[3]) {
background(25);
stroke(255);
dividerY = map(frameCount, keyframes[2], keyframes[3], height / 2, height);
} else {
background(25);
stroke(255);
dividerY = height + 1; // off-screen
}
if (frameCount > keyframes[0] && frameCount <= keyframes[3]) {
noStroke();
fill(25);
rect(0, 0, width, dividerY); // Upper half background
fill(255);
rect(0, dividerY, width, height - dividerY); // Lower half background
stroke(127);
line(0, dividerY, width, dividerY);
}
// Move points using Perlin noise
for (int i = 0; i < numPoints; i++) {
points[i].x += map(noise(noiseOffsets[i].x + frameCount * noiseSpeed), 0, 1, -1, 1);
points[i].y += map(noise(noiseOffsets[i].y + frameCount * noiseSpeed), 0, 1, -1, 1);
points[i].z += map(noise(noiseOffsets[i].z + frameCount * noiseSpeed), 0, 1, -1, 1);
// Apply boundary force
if (points[i].x < boundaryX) {
points[i].x += boundaryForce;
} else if (points[i].x > boundaryX + boundaryWidth) {
points[i].x -= boundaryForce;
}
if (points[i].y < boundaryY) {
points[i].y += boundaryForce;
} else if (points[i].y > boundaryY + boundaryHeight) {
points[i].y -= boundaryForce;
}
}
// Draw the points and their connections
for (int i = 0; i < numPoints; i++) {
// Find the k-nearest neighbors
int[] neighbors = getKNearestNeighbors(i);
// Draw lines to the k-nearest neighbors
for (int j = 0; j < k; j++) {
int neighborIndex = neighbors[j];
float distance = distances[i][neighborIndex];
// Calculate stroke weight and color based on z-value (depth)
float weight = map(points[i].z, -500, 500, thicknessMin, thicknessMax);
float alpha = map(points[i].z, -500, 500, opacityMin, opacityMax);
// Determine if the edge intersects the dividing line
if ((points[i].y < dividerY && points[neighborIndex].y >= dividerY) ||
(points[i].y >= dividerY && points[neighborIndex].y < dividerY)) {
// Calculate intersection point
float t = (dividerY - points[i].y) / (points[neighborIndex].y - points[i].y);
float ix = points[i].x + t * (points[neighborIndex].x - points[i].x);
// Draw the line in two segments
if (points[i].y < dividerY) {
// Upper half segment
stroke(255, alpha);
strokeWeight(weight);
line(points[i].x, points[i].y, ix, dividerY);
// Lower half segment
stroke(0, alpha);
strokeWeight(weight);
line(ix, dividerY, points[neighborIndex].x, points[neighborIndex].y);
} else {
// Lower half segment
stroke(0, alpha);
strokeWeight(weight);
line(points[i].x, points[i].y, ix, dividerY);
// Upper half segment
stroke(255, alpha);
strokeWeight(weight);
line(ix, dividerY, points[neighborIndex].x, points[neighborIndex].y);
}
} else {
// Single segment line
if (points[i].y < dividerY) {
stroke(255, alpha);
} else {
stroke(0, alpha);
}
strokeWeight(weight);
line(points[i].x, points[i].y, points[neighborIndex].x, points[neighborIndex].y);
}
}
}
// Save the frame as a PNG file
saveFrame("output_2/screen-####.png");
if (frameCount == keyframes[4]) {
exit();
}
}
// Function to get k-nearest neighbors of a point
int[] getKNearestNeighbors(int index) {
float[] distancesFromPoint = new float[numPoints];
for (int i = 0; i < numPoints; i++) {
distancesFromPoint[i] = distances[index][i];
}
int[] indices = new int[numPoints];
for (int i = 0; i < numPoints; i++) {
indices[i] = i;
}
// Sort indices based on distances
for (int i = 0; i < numPoints; i++) {
for (int j = i + 1; j < numPoints; j++) {
if (distancesFromPoint[indices[i]] > distancesFromPoint[indices[j]]) {
int temp = indices[i];
indices[i] = indices[j];
indices[j] = temp;
}
}
}
int[] kNearest = new int[k];
for (int i = 0; i < k; i++) {
kNearest[i] = indices[i + 1];
}
return kNearest;
}