Skip to content
This repository was archived by the owner on Jul 5, 2022. It is now read-only.

Commit 15244a8

Browse files
edorfausgruselhaus
andauthored
Add port to p5.js of Coding Challenge 142.1: Rubiks Cube 1 (#2181)
* Add port to p5.js of Coding Challenge 142.1: Rubiks Cube 1 * Change Cubie class to use separate shapes as QUADS isn't implemented. This is a workaround for issue p5.js#4401 : in WebGL mode, the QUADS mode of beginShape() has not been implemented yet. As a replacement, this code now uses a separate shape for each face of the cubie. Co-authored-by: Nico Finkernagel <[email protected]>
1 parent d67725b commit 15244a8

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Rubiks Cube 1
2+
// The Coding Train / Daniel Shiffman
3+
// https://thecodingtrain.com/CodingChallenges/142.1-rubiks-cube.html
4+
// https://youtu.be/9PGfL4t-uqE
5+
6+
class Cubie {
7+
constructor(x, y, z, len_) {
8+
this.pos = createVector(x, y, z);
9+
this.len = len_;
10+
}
11+
show() {
12+
fill(255);
13+
stroke(0);
14+
strokeWeight(8);
15+
push();
16+
translate(this.pos.x, this.pos.y, this.pos.z);
17+
const r = this.len / 2;
18+
19+
// When this was ported, p5.js (version 1.0.0) had not yet
20+
// implemented support for beginShape(QUADS) in WEBGL mode.
21+
// See: https://github.com/processing/p5.js/issues/4401
22+
// So instead, we use separate shapes for each face of the cubie.
23+
24+
// z-fixed
25+
beginShape();
26+
fill(colors[BCK]);
27+
vertex(-r, -r, -r);
28+
vertex(r, -r, -r);
29+
vertex(r, r, -r);
30+
vertex(-r, r, -r);
31+
endShape(CLOSE);
32+
33+
beginShape();
34+
fill(colors[FRT]);
35+
vertex(-r, -r, r);
36+
vertex(r, -r, r);
37+
vertex(r, r, r);
38+
vertex(-r, r, r);
39+
endShape(CLOSE);
40+
41+
// y-fixed
42+
beginShape();
43+
fill(colors[DWN]);
44+
vertex(-r, -r, -r);
45+
vertex(r, -r, -r);
46+
vertex(r, -r, r);
47+
vertex(-r, -r, r);
48+
endShape(CLOSE);
49+
50+
beginShape();
51+
fill(colors[UPP]);
52+
vertex(-r, r, -r);
53+
vertex(r, r, -r);
54+
vertex(r, r, r);
55+
vertex(-r, r, r);
56+
endShape(CLOSE);
57+
58+
// x-fixed
59+
beginShape();
60+
fill(colors[LFT]);
61+
vertex(-r, -r, -r);
62+
vertex(-r, r, -r);
63+
vertex(-r, r, r);
64+
vertex(-r, -r, r);
65+
endShape(CLOSE);
66+
67+
beginShape();
68+
fill(colors[RGT]);
69+
vertex(r, -r, -r);
70+
vertex(r, r, -r);
71+
vertex(r, r, r);
72+
vertex(r, -r, r);
73+
endShape(CLOSE);
74+
75+
//box(this.len);
76+
pop();
77+
}
78+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="UTF-8" />
6+
7+
<title>Rubiks Cube 1 - Coding Challenge #142.1</title>
8+
9+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.min.js"></script>
10+
<script src="https://cdn.jsdelivr.net/gh/freshfork/[email protected]/p5.easycam.min.js"></script>
11+
</head>
12+
13+
<body>
14+
<script src="Cubie.js"></script>
15+
<script src="sketch.js"></script>
16+
</body>
17+
18+
</html>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Rubiks Cube 1
2+
// The Coding Train / Daniel Shiffman
3+
// https://thecodingtrain.com/CodingChallenges/142.1-rubiks-cube.html
4+
// https://youtu.be/9PGfL4t-uqE
5+
6+
// Since PeasyCam is only for Java, this instead uses p5.EasyCam,
7+
// which is based on PeasyCam but is made for p5.js.
8+
// It can be found here: https://github.com/freshfork/p5.EasyCam
9+
10+
let cam;
11+
12+
// UP, DOWN, RIGHT, LEFT, FRONT, BACK
13+
const UPP = 0;
14+
const DWN = 1;
15+
const RGT = 2;
16+
const LFT = 3;
17+
const FRT = 4;
18+
const BCK = 5;
19+
20+
const colors = [
21+
'#FFFFFF',
22+
'#FFFF00',
23+
'#FFA500',
24+
'#FF0000',
25+
'#00FF00',
26+
'#0000FF'
27+
];
28+
29+
const dim = 3;
30+
const cube = []; // Cubie[dim][dim][dim]; initialized in setup()
31+
32+
function setup() {
33+
// Disable the context menu on the canvas so the camera can use the right mouse button
34+
createCanvas(600, 600, WEBGL).elt.oncontextmenu = () => false;
35+
36+
cam = createEasyCam({ distance: 400 });
37+
38+
for (let i = 0; i < dim; i++) {
39+
cube[i] = [];
40+
for (let j = 0; j < dim; j++) {
41+
cube[i][j] = [];
42+
for (let k = 0; k < dim; k++) {
43+
const len = 50;
44+
const offset = (dim - 1) * len * 0.5;
45+
const x = len * i - offset;
46+
const y = len * j - offset;
47+
const z = len * k - offset;
48+
cube[i][j][k] = new Cubie(x, y, z, len);
49+
}
50+
}
51+
}
52+
}
53+
54+
function draw() {
55+
background(51);
56+
for (let i = 0; i < dim; i++) {
57+
for (let j = 0; j < dim; j++) {
58+
for (let k = 0; k < dim; k++) {
59+
cube[i][j][k].show();
60+
}
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)