Skip to content

Commit 1b52c03

Browse files
authored
Merge pull request #689 from CodingTrain/challenges-ml5
Coding Challenge 112-114 and ml5 video code
2 parents 1101197 + bfb7037 commit 1b52c03

File tree

10 files changed

+626
-0
lines changed

10 files changed

+626
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Daniel Shiffman
2+
// http://youtube.com/thecodingtrain
3+
// http://codingtra.in
4+
5+
// Coding Challenge #112: 3D Rendering with Rotation and Projection
6+
// https://youtu.be/p4Iz0XJY-Qk
7+
8+
float angle = 0;
9+
10+
PVector[] points = new PVector[8];
11+
12+
float[][] projection = {
13+
{1, 0, 0},
14+
{0, 1, 0}
15+
};
16+
17+
void setup() {
18+
size(600, 400);
19+
20+
points[0] = new PVector(-0.5, -0.5, -0.5);
21+
points[1] = new PVector(0.5, -0.5, -0.5);
22+
points[2] = new PVector(0.5, 0.5, -0.5);
23+
points[3] = new PVector(-0.5, 0.5, -0.5);
24+
points[4] = new PVector(-0.5, -0.5, 0.5);
25+
points[5] = new PVector(0.5, -0.5, 0.5);
26+
points[6] = new PVector(0.5, 0.5, 0.5);
27+
points[7] = new PVector(-0.5, 0.5, 0.5);
28+
}
29+
30+
void draw() {
31+
background(0);
32+
translate(width/2, height/2);
33+
34+
35+
float[][] rotationZ = {
36+
{ cos(angle), -sin(angle), 0},
37+
{ sin(angle), cos(angle), 0},
38+
{ 0, 0, 1}
39+
};
40+
41+
float[][] rotationX = {
42+
{ 1, 0, 0},
43+
{ 0, cos(angle), -sin(angle)},
44+
{ 0, sin(angle), cos(angle), 0}
45+
};
46+
47+
float[][] rotationY = {
48+
{ cos(angle), 0, -sin(angle)},
49+
{ 0, 1, 0},
50+
{ sin(angle), 0, cos(angle)}
51+
};
52+
53+
PVector[] projected = new PVector[8];
54+
55+
int index = 0;
56+
for (PVector v : points) {
57+
PVector rotated = matmul(rotationY, v);
58+
rotated = matmul(rotationX, rotated);
59+
rotated = matmul(rotationZ, rotated);
60+
PVector projected2d = matmul(projection, rotated);
61+
projected2d.mult(200);
62+
projected[index] = projected2d;
63+
//point(projected2d.x, projected2d.y);
64+
index++;
65+
}
66+
67+
for (PVector v : projected) {
68+
stroke(255);
69+
strokeWeight(16);
70+
noFill();
71+
point(v.x, v.y);
72+
}
73+
74+
// Connecting
75+
for (int i = 0; i < 4; i++) {
76+
connect(i, (i+1) % 4, projected);
77+
connect(i+4, ((i+1) % 4)+4, projected);
78+
connect(i, i+4, projected);
79+
}
80+
81+
82+
83+
84+
85+
angle += 0.03;
86+
}
87+
88+
void connect(int i, int j, PVector[] points) {
89+
PVector a = points[i];
90+
PVector b = points[j];
91+
strokeWeight(1);
92+
stroke(255);
93+
line(a.x, a.y, b.x, b.y);
94+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Daniel Shiffman
2+
// http://youtube.com/thecodingtrain
3+
// http://codingtra.in
4+
5+
// Coding Challenge #112: 3D Rendering with Rotation and Projection
6+
// https://youtu.be/p4Iz0XJY-Qk
7+
8+
// Matrix Multiplication
9+
// https://youtu.be/tzsgS19RRc8
10+
11+
float[][] vecToMatrix(PVector v) {
12+
float[][] m = new float[3][1];
13+
m[0][0] = v.x;
14+
m[1][0] = v.y;
15+
m[2][0] = v.z;
16+
return m;
17+
}
18+
19+
PVector matrixToVec(float[][] m) {
20+
PVector v = new PVector();
21+
v.x = m[0][0];
22+
v.y = m[1][0];
23+
if (m.length > 2) {
24+
v.z = m[2][0];
25+
}
26+
return v;
27+
}
28+
29+
void logMatrix(float[][] m) {
30+
int cols = m[0].length;
31+
int rows = m.length;
32+
println(rows + "x" + cols);
33+
println("----------------");
34+
for (int i = 0; i < rows; i++) {
35+
for (int j = 0; j < cols; j++) {
36+
print(m[i][j] + " ");
37+
}
38+
println();
39+
}
40+
println();
41+
}
42+
43+
44+
PVector matmul(float[][] a, PVector b) {
45+
float[][] m = vecToMatrix(b);
46+
return matrixToVec(matmul(a,m));
47+
}
48+
49+
float[][] matmul(float[][] a, float[][] b) {
50+
int colsA = a[0].length;
51+
int rowsA = a.length;
52+
int colsB = b[0].length;
53+
int rowsB = b.length;
54+
55+
if (colsA != rowsB) {
56+
println("Columns of A must match rows of B");
57+
return null;
58+
}
59+
60+
float result[][] = new float[rowsA][colsB];
61+
62+
for (int i = 0; i < rowsA; i++) {
63+
for (int j = 0; j < colsB; j++) {
64+
float sum = 0;
65+
for (int k = 0; k < colsA; k++) {
66+
sum += a[i][k] * b[k][j];
67+
}
68+
result[i][j] = sum;
69+
}
70+
}
71+
return result;
72+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Daniel Shiffman
2+
// http://youtube.com/thecodingtrain
3+
// http://codingtra.in
4+
5+
// Coding Challenge #113: 4D Hypercube
6+
// https://youtu.be/XE3YDVdQSPo
7+
8+
// Matrix Multiplication
9+
// https://youtu.be/tzsgS19RRc8
10+
11+
float angle = 0;
12+
13+
P4Vector[] points = new P4Vector[16];
14+
15+
void setup() {
16+
fullScreen(P3D);
17+
points[0] = new P4Vector(-1, -1, -1, 1);
18+
points[1] = new P4Vector(1, -1, -1, 1);
19+
points[2] = new P4Vector(1, 1, -1, 1);
20+
points[3] = new P4Vector(-1, 1, -1, 1);
21+
points[4] = new P4Vector(-1, -1, 1, 1);
22+
points[5] = new P4Vector(1, -1, 1, 1);
23+
points[6] = new P4Vector(1, 1, 1, 1);
24+
points[7] = new P4Vector(-1, 1, 1, 1);
25+
points[8] = new P4Vector(-1, -1, -1, -1);
26+
points[9] = new P4Vector(1, -1, -1, -1);
27+
points[10] = new P4Vector(1, 1, -1, -1);
28+
points[11] = new P4Vector(-1, 1, -1, -1);
29+
points[12] = new P4Vector(-1, -1, 1, -1);
30+
points[13] = new P4Vector(1, -1, 1, -1);
31+
points[14] = new P4Vector(1, 1, 1, -1);
32+
points[15] = new P4Vector(-1, 1, 1, -1);
33+
}
34+
35+
void draw() {
36+
background(0);
37+
translate(width/2, height/2);
38+
rotateX(-PI/2);
39+
PVector[] projected3d = new PVector[16];
40+
41+
for (int i = 0; i < points.length; i++) {
42+
P4Vector v = points[i];
43+
44+
float[][] rotationXY = {
45+
{cos(angle), -sin(angle), 0, 0},
46+
{sin(angle), cos(angle), 0, 0},
47+
{0, 0, 1, 0},
48+
{0, 0, 0, 1}
49+
};
50+
51+
float[][] rotationZW = {
52+
{1, 0, 0, 0},
53+
{0, 1, 0, 0},
54+
{0, 0, cos(angle), -sin(angle)},
55+
{0, 0, sin(angle), cos(angle)}
56+
};
57+
58+
59+
P4Vector rotated = matmul(rotationXY, v, true);
60+
rotated = matmul(rotationZW, rotated, true);
61+
62+
float distance = 2;
63+
float w = 1 / (distance - rotated.w);
64+
65+
float[][] projection = {
66+
{w, 0, 0, 0},
67+
{0, w, 0, 0},
68+
{0, 0, w, 0}
69+
};
70+
71+
PVector projected = matmul(projection, rotated);
72+
projected.mult(width/8);
73+
projected3d[i] = projected;
74+
75+
stroke(255, 200);
76+
strokeWeight(32);
77+
noFill();
78+
79+
point(projected.x, projected.y, projected.z);
80+
}
81+
82+
// Connecting
83+
for (int i = 0; i < 4; i++) {
84+
connect(0, i, (i+1) % 4, projected3d );
85+
connect(0, i+4, ((i+1) % 4)+4, projected3d);
86+
connect(0, i, i+4, projected3d);
87+
}
88+
89+
for (int i = 0; i < 4; i++) {
90+
connect(8, i, (i+1) % 4, projected3d );
91+
connect(8, i+4, ((i+1) % 4)+4, projected3d);
92+
connect(8, i, i+4, projected3d);
93+
}
94+
95+
for (int i = 0; i < 8; i++) {
96+
connect(0, i, i + 8, projected3d);
97+
}
98+
99+
//angle = map(mouseX, 0, width, 0, TWO_PI);
100+
angle += 0.02;
101+
}
102+
103+
void connect(int offset, int i, int j, PVector[] points) {
104+
PVector a = points[i+offset];
105+
PVector b = points[j+offset];
106+
strokeWeight(4);
107+
stroke(255);
108+
line(a.x, a.y, a.z, b.x, b.y, b.z);
109+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Daniel Shiffman
2+
// http://youtube.com/thecodingtrain
3+
// http://codingtra.in
4+
5+
// Coding Challenge #113: 4D Hypercube
6+
// https://youtu.be/XE3YDVdQSPo
7+
8+
// Matrix Multiplication
9+
// https://youtu.be/tzsgS19RRc8
10+
11+
12+
class P4Vector {
13+
float x, y, z, w;
14+
15+
P4Vector(float x, float y, float z, float w) {
16+
this.x = x;
17+
this.y = y;
18+
this.z = z;
19+
this.w = w;
20+
}
21+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Daniel Shiffman
2+
// http://youtube.com/thecodingtrain
3+
// http://codingtra.in
4+
5+
// Coding Challenge #113: 4D Hypercube
6+
// https://youtu.be/XE3YDVdQSPo
7+
8+
// Matrix Multiplication
9+
// https://youtu.be/tzsgS19RRc8
10+
11+
12+
float[][] vecToMatrix(P4Vector v) {
13+
float[][] m = new float[4][1];
14+
m[0][0] = v.x;
15+
m[1][0] = v.y;
16+
m[2][0] = v.z;
17+
m[3][0] = v.w;
18+
return m;
19+
}
20+
21+
PVector matrixToVec(float[][] m) {
22+
PVector v = new PVector();
23+
v.x = m[0][0];
24+
v.y = m[1][0];
25+
v.z = m[2][0];
26+
return v;
27+
}
28+
29+
P4Vector matrixToVec4(float[][] m) {
30+
P4Vector v = new P4Vector(0,0,0,0);
31+
v.x = m[0][0];
32+
v.y = m[1][0];
33+
v.z = m[2][0];
34+
v.w = m[3][0];
35+
return v;
36+
}
37+
38+
39+
void logMatrix(float[][] m) {
40+
int cols = m[0].length;
41+
int rows = m.length;
42+
println(rows + "x" + cols);
43+
println("----------------");
44+
for (int i = 0; i < rows; i++) {
45+
for (int j = 0; j < cols; j++) {
46+
print(m[i][j] + " ");
47+
}
48+
println();
49+
}
50+
println();
51+
}
52+
53+
54+
PVector matmul(float[][] a, P4Vector b) {
55+
float[][] m = vecToMatrix(b);
56+
return matrixToVec(matmul(a, m));
57+
}
58+
59+
P4Vector matmul(float[][] a, P4Vector b, boolean fourth) {
60+
float[][] m = vecToMatrix(b);
61+
return matrixToVec4(matmul(a, m));
62+
}
63+
64+
float[][] matmul(float[][] a, float[][] b) {
65+
int colsA = a[0].length;
66+
int rowsA = a.length;
67+
int colsB = b[0].length;
68+
int rowsB = b.length;
69+
70+
if (colsA != rowsB) {
71+
println("Columns of A must match rows of B");
72+
return null;
73+
}
74+
75+
float result[][] = new float[rowsA][colsB];
76+
77+
for (int i = 0; i < rowsA; i++) {
78+
for (int j = 0; j < colsB; j++) {
79+
float sum = 0;
80+
for (int k = 0; k < colsA; k++) {
81+
sum += a[i][k] * b[k][j];
82+
}
83+
result[i][j] = sum;
84+
}
85+
}
86+
return result;
87+
}

0 commit comments

Comments
 (0)