/
cubic.js
318 lines (269 loc) · 7.44 KB
/
cubic.js
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
var angle = 0;
var points = [];
var lines = [];
const projection = [
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
];
const scale = [
[400, 0, 0],
[0, 400, 0],
[0, 0, 400]
]
var palettes, palette;
function preload() {
palettes = loadJSON(baseURL + '/1000.json');
}
function setup() {
let canvas = makeCanvas();
palette = randomElement(palettes);
strokeCap(SQUARE);
points[0] = createVector(-0.5, -0.5, -0.5);
points[1] = createVector(0.5, -0.5, -0.5);
points[2] = createVector(0.5, -0.5, 0.5);
points[3] = createVector(-0.5, -0.5, 0.5);
points[4] = createVector(-0.5, 0.5, -0.5);
points[5] = createVector(0.5, 0.5, -0.5);
points[6] = createVector(0.5, 0.5, 0.5);
points[7] = createVector(-0.5, 0.5, 0.5);
angle = QUARTER_PI;
}
var angleInc = 0;
function draw() {
// background(palette[0]);
translate(width / 2, height / 2);
lines = [];
function rotationZ(a) {
return [
[cos(a), -sin(a), 0],
[sin(a), cos(a), 0],
[0, 0, 1]];
}
function rotationX(a) {
return [
[1, 0, 0],
[0, cos(a), -sin(a)],
[0, sin(a), cos(a)]];
}
const rotationY = [
[cos(angle), 0, sin(angle)],
[0, 1, 0],
[-sin(angle), 0, cos(angle)]
];
const camProjection = [
[1, 0, 0],
[0, cos(radians(20)), -sin(radians(20))],
[0, sin(radians(20)), cos(radians(20))]
];
let unProjected = [];
let projected = [];
for (let i = 0; i < points.length; i++) {
let rotated = matmul(rotationY, points[i]);
rotated = matmul(scale, rotated);
unProjected[i] = rotated;
rotated = matmul(camProjection, rotated);
let projected2d = matmul(projection, rotated);
projected[i] = projected2d;
}
// Connecting
for (let i = 0; i < 4; i++) {
let normalLeft = normal3(
unProjected[(i+3)%4],
unProjected[i],
unProjected[((i+3)%4)+4]
);
normalLeft.normalize();
let normalRight = normal3(
unProjected[i],
unProjected[(i+1)%4],
unProjected[i+4]
);
normalRight.normalize();
connect(i, (i + 1) % 4, projected);
if (normalRight.z > 0) connect(i + 4, ((i + 1) % 4) + 4, projected);
if (!(normalLeft.z < 0 && normalRight.z < 0)) connect(i, i + 4, projected);
}
// Draw the lines
// strokeWeight(1);
// stroke(255);
// for (const seg of lines) {
// line(seg[0].x, seg[0].y, seg[1].x, seg[1].y);
// }
rectMode(CORNERS);
noStroke();
fill(palette[1]);
let bandHeight = 15;
// Per-pixel drawing method
noStroke();
noFill();
rectMode(CORNERS);
for (let y=-height/2; y<height/2; y+=bandHeight) {
fill(mapColor(palette[0], palette[2], map(y, -height/2, height/2, 0, 1)));
rect(-width/2, y, width/2, y+bandHeight);
for (let x=-width/2; x<width/2; x+=bandHeight) {
if (touchesAnyLine(x, y, x+bandHeight, y+bandHeight)) {
fill(palette[4]);
rect(x, y, x+bandHeight, y+bandHeight);
}
}
}
// for (let y=-height/2; y<height/2; y+=bandHeight) {
// let pixRect = {
// top: [ createVector(-width/2, y), createVector(width/2, y) ],
// bottom: [ createVector(-width/2, y+bandHeight), createVector(width/2, y+bandHeight) ]
// };
// for (const l of lines) {
// // Compute intersections between each line segment of the cube
// // and the edges of the band.
// let isects = [
// intersection(l[0], l[1], pixRect.top[0], pixRect.top[1]),
// intersection(l[0], l[1], pixRect.bottom[0], pixRect.bottom[1])
// ].filter(a => a !== undefined);
// // If end(s) terminate within the band
// for (s of l) {
// if (s.y > y && s.y < y+bandHeight) {
// isects.push(s.copy());
// }
// }
// // There are two "pixel" endpoints, so we can draw a pixel rectangle
// if (isects.length === 2) {
// // Draw from "left" to "right"
// isects.sort((a, b) => a.x - b.x );
// // Set minimum width
// let w = isects[1].x - isects[0].x;
// if (w < bandHeight) {
// let d = (bandHeight - w) / 2;
// isects[0].x -= d;
// isects[1].x += d;
// }
// rect(isects[0].x, pixRect.top[0].y, isects[1].x, pixRect.bottom[0].y);
// }
// }
// }
// Advance the rotation
angle = HALF_PI * sin(angleInc - (0.75 * PI)) + 0.75 * PI;
angleInc += 0.02;
}
// Provide box in CORNERS format (two opposite corners)
function touchesAnyLine(x1, y1, x2, y2) {
const sides = [
[createVector(x1, y1), createVector(x2, y1)],
[createVector(x2, y1), createVector(x2, y2)],
[createVector(x2, y2), createVector(x1, y2)],
[createVector(x1, y2), createVector(x1, y1)]
];
for (const l of lines) {
for (const s of sides) {
if (intersection(l[0], l[1], s[0], s[1])) {
return true;
}
}
}
return false;
}
function normal3(v0, v1, v2) {
return p5.Vector.sub(v1, v0).cross(p5.Vector.sub(v2, v0));
}
function connect(i, j, points) {
const a = points[i];
const b = points[j];
lines.push([a, b]);
}
// Daniel Shiffman
// http://youtube.com/thecodingtrain
// http://codingtra.in
// Javascript transcription: Chuck England
// Coding Challenge #112: 3D Rendering with Rotation and Projection
// https://youtu.be/p4Iz0XJY-Qk
// Matrix Multiplication
// https://youtu.be/tzsgS19RRc8
function vecToMatrix(v) {
let m = [];
for (let i = 0; i < 3; i++) {
m[i] = [];
}
m[0][0] = v.x;
m[1][0] = v.y;
m[2][0] = v.z;
return m;
}
function matrixToVec(m) {
return createVector(m[0][0], m[1][0], m.length > 2 ? m[2][0] : 0);
}
function logMatrix(m) {
const cols = m[0].length;
const rows = m.length;
console.log(rows + 'x' + cols);
console.log('----------------');
let s = '';
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
s += m[i][j] + ' ';
}
console.log(s);
}
console.log();
}
function matmulvec(a, vec) {
let m = vecToMatrix(vec);
let r = matmul(a, m);
return matrixToVec(r);
}
function matmul(a, b) {
if (b instanceof p5.Vector) {
return matmulvec(a, b);
}
let colsA = a[0].length;
let rowsA = a.length;
let colsB = b[0].length;
let rowsB = b.length;
if (colsA !== rowsB) {
console.error('Columns of A must match rows of B');
return null;
}
result = [];
for (let j = 0; j < rowsA; j++) {
result[j] = [];
for (let i = 0; i < colsB; i++) {
let sum = 0;
for (let n = 0; n < colsA; n++) {
sum += a[j][n] * b[n][i];
}
result[j][i] = sum;
}
}
return result;
}
// 1FpGLLjZSZMx6k
// https://stackoverflow.com/questions/9043805/test-if-two-lines-intersect-javascript-function
// interface Point2D {
// x: number;
// y: number;
// }
function closest(origin, points) {
let dist = width*2;
let ret;
for (const p of points) {
let d = p5.Vector.dist(origin, p);
if (d < dist) {
dist = d;
ret = p;
}
}
return ret.copy();
}
function intersection(from1, to1, from2, to2) {
const dX = to1.x - from1.x;
const dY = to1.y - from1.y;
const determinant = dX * (to2.y - from2.y) - (to2.x - from2.x) * dY;
if (determinant === 0) return undefined; // parallel lines
const lambda = ((to2.y - from2.y) * (to2.x - from1.x) + (from2.x - to2.x) * (to2.y - from1.y)) / determinant;
const gamma = ((from1.y - to1.y) * (to2.x - from1.x) + dX * (to2.y - from1.y)) / determinant;
// check if there is an intersection
if (!(0 <= lambda && lambda <= 1) || !(0 <= gamma && gamma <= 1)) return undefined;
return createVector(
from1.x + lambda * dX,
from1.y + lambda * dY
);
}