Skip to content

Commit 448a463

Browse files
committed
Added discrete Wave
1 parent 366344e commit 448a463

13 files changed

Lines changed: 575 additions & 0 deletions

File tree

Arduino/SpiRob/index.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
3+
<html lang="en">
4+
5+
<head>
6+
<script src="https://cdn.jsdelivr.net/npm/p5@1.9.2/lib/p5.js"></script>
7+
<script src="https://cdn.jsdelivr.net/gh/ongzzzzzz/p5.web-serial/lib/p5.web-serial.js"></script>
8+
<script language="javascript" type="text/javascript" src="https://cdn.jsdelivr.net/npm/p5.serialserver@0.0.28/lib/p5.serialport.js"></script>
9+
<meta charset="UTF-8">
10+
<title>Arduino</title>
11+
<link rel="icon" href="../icon.png">
12+
<link rel="stylesheet" type="text/css" href="style.css">
13+
</head>
14+
15+
<body>
16+
<main>
17+
</main>
18+
<script src="sketch.js"></script>
19+
</body>
20+
</html>

Arduino/SpiRob/sketch.js

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
let maze;
2+
3+
function setup(){
4+
createCanvas(windowWidth, windowHeight);
5+
frameRate(60);
6+
angleMode(DEGREES);
7+
8+
maze = new Maze(20, 20);
9+
10+
}
11+
12+
function draw(){
13+
background(220);
14+
15+
maze.display();
16+
}
17+
18+
class Maze{
19+
constructor(cols, rows){
20+
this.cols = cols;
21+
this.rows = rows;
22+
23+
this.containerParameters = {x: 0, y: 0, contWidth: 600, contHeight: 600}
24+
25+
this.squareSize = this.containerParameters.contWidth / this.cols;
26+
27+
this.circleD = 10;
28+
this.circlePos = createVector(1.5 * this.squareSize, 1.5 * this.squareSize);
29+
this.circleVel = createVector(3, 3);
30+
this.circleAccel = createVector(0, 0);
31+
32+
this.mazeMap = [];
33+
this.generateMapArray();
34+
this.done = false;
35+
}
36+
37+
generateMapArray() {
38+
for (let i = 0; i < this.rows + 1; i++) {
39+
this.mazeMap.push([]);
40+
for (let j = 0; j < this.cols + 1; j++) {
41+
// randomize wall or not
42+
if (Math.random() > 0.25) {
43+
this.mazeMap[i].push(0) // not wall
44+
} else {
45+
if (i < 3 && j < 3) {
46+
this.mazeMap[i].push(0) // not wall
47+
} else if (this.rows - 4 < i && this.cols - 4 < j) {
48+
this.mazeMap[i].push(0) // not wall
49+
} else {
50+
this.mazeMap[i].push(1) // wall
51+
}
52+
}
53+
}
54+
}
55+
// set perimeter to be walls
56+
for (let i = 0; i < this.cols; i++) {
57+
this.mazeMap[0][i] = 1;
58+
this.mazeMap[this.rows - 1][i] = 1;
59+
}
60+
61+
for (let j = 0; j < this.cols; j++) {
62+
this.mazeMap[j][0] = 1;
63+
this.mazeMap[j][this.rows - 1] = 1;
64+
}
65+
}
66+
67+
display() {
68+
push();
69+
fill('#808080');
70+
stroke('#222222');
71+
translate(width / 2 - 300, height / 2 - 300);
72+
rect(this.containerParameters.x, this.containerParameters.y, this.containerParameters.contWidth, this.containerParameters.contHeight);
73+
for (let i = 0; i < this.rows; i++) {
74+
for (let j = 0; j < this.cols; j++) {
75+
if (this.mazeMap[i][j] === 1) {
76+
fill(0, 0, 0);
77+
} else {
78+
fill(150, 150, 150);
79+
}
80+
square(i * this.squareSize, j * this.squareSize, this.squareSize);
81+
}
82+
}
83+
this.displayCircle();
84+
pop();
85+
}
86+
87+
collisionDetection() {
88+
// find immediate s
89+
let neighs = this.generateNeighborCoords();
90+
neighs = Array.from(neighs);
91+
92+
neighs.forEach((neigh)=>{
93+
let a = JSON.parse(neigh);
94+
if (this.mazeMap[a[0]][a[1]] === 1) { // if neighbor is wall
95+
if (a[2] === "left") {
96+
this.circlePos.x = a[0] * this.squareSize + this.circleD / 2;
97+
this.circleVel.x = 0;
98+
this.circleAccel.x = 0;
99+
100+
} else if (a[2] === "top") {
101+
this.circlePos.y = a[1] * this.squareSize + this.circleD / 2;
102+
this.circleVel.y = 0;
103+
this.circleAccel.y = 0;
104+
105+
} else if (a[2] === "right") {
106+
this.circlePos.x = a[0] * this.squareSize - this.circleD / 2;
107+
this.circleVel.x = 0;
108+
this.circleAccel.x = 0;
109+
110+
} else {
111+
this.circlePos.y = a[1] * this.squareSize - this.circleD / 2;
112+
this.circleVel.y = 0;
113+
this.circleAccel.y = 0;
114+
}
115+
116+
fill('red');
117+
square(a[0] * this.squareSize, a[1] * this.squareSize, this.squareSize);
118+
}
119+
120+
})
121+
122+
console.log(neighs);
123+
}
124+
125+
generateNeighborCoords() {
126+
let squares = new Set();
127+
let coords = []
128+
129+
// the four "vertexes"
130+
let circleTop = this.circlePos.y - this.circleD / 2;
131+
let circleBottom = this.circlePos.y + this.circleD / 2;
132+
133+
let circleLeft = this.circlePos.x - this.circleD / 2;
134+
let circleRight = this.circlePos.x + this.circleD / 2;
135+
136+
squares.add(JSON.stringify([Math.floor(this.circlePos.x / this.squareSize), Math.floor(this.circlePos.y / this.squareSize), "middle"]));
137+
138+
let leftNei = JSON.stringify([Math.floor(circleLeft / this.squareSize), Math.floor(this.circlePos.y / this.squareSize), "left"]);
139+
let topNei = JSON.stringify([Math.floor(this.circlePos.x / this.squareSize), Math.floor(circleTop / this.squareSize), "top"]);
140+
let rightNei = JSON.stringify([Math.floor(circleRight / this.squareSize), Math.floor(this.circlePos.y / this.squareSize), "right"]);
141+
let bottomNei = JSON.stringify([Math.floor(this.circlePos.x / this.squareSize), Math.floor(circleBottom / this.squareSize), "bottom"]);
142+
143+
// let potentialNeis = [leftNei, topNei, rightNei, bottomNei];
144+
145+
squares.add(leftNei);
146+
squares.add(topNei);
147+
squares.add(rightNei);
148+
squares.add(bottomNei);
149+
150+
return squares;
151+
}
152+
153+
displayCircle() {
154+
this.circleVel.add(this.circleAccel);
155+
this.circlePos.add(this.circleVel);
156+
if ((this.circlePos.x - this.squareSize / 2 >= (this.cols - 1) * this.squareSize) && (this.circlePos.y - this.squareSize / 2 >= (this.rows - 1) * this.squareSize)) {
157+
this.stopCircle();
158+
this.done = true
159+
fill('green');
160+
square((this.rows - 1) * this.squareSize, (this.cols - 1) * this.squareSize, this.squareSize);
161+
}
162+
this.collisionDetection();
163+
fill(50, 50, 50);
164+
circle(this.circlePos.x, this.circlePos.y, this.circleD);
165+
}
166+
167+
stopCircle() {
168+
maze.circleVel.x = 0;
169+
maze.circleVel.y = 0;
170+
171+
maze.circleAccel.x = 0;
172+
maze.circleAccel.y = 0;
173+
}
174+
}

Arduino/SpiRob/sketch2.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
let port, reader, writer;
2+
let readings = []
3+
4+
async function connectSerial(){
5+
noLoop();
6+
({ port, reader, writer } = await getPort(115200));
7+
loop();
8+
}
9+
10+
async function setup() {
11+
createCanvas(windowWidth, windowHeight);
12+
frameRate(60);
13+
angleMode(DEGREES);
14+
textSize(100)
15+
textAlign(CENTER, RIGHT);
16+
17+
await connectSerial();
18+
}
19+
20+
async function draw() {
21+
while(true) {
22+
const {value, done} = await reader.read();
23+
24+
if (done) {
25+
reader.releaseLock();
26+
break;
27+
}
28+
29+
background(220);
30+
31+
readings.unshift(int(value));
32+
33+
if(readings.length > 25)
34+
readings.pop();
35+
36+
// let avg = 0;
37+
//
38+
// for(let i = 0; i < readings.length; i++)
39+
// avg += readings[i];
40+
//
41+
// avg /= readings.length;
42+
// text("A0: " + avg,width / 2, height / 2)
43+
44+
}
45+
}
46+
47+
function drawSpiral(){
48+
let R = 25 * 10;
49+
let a = 1;
50+
let b = 0.22;
51+
52+
text(exp(2), width / 2, height / 2);
53+
let r = a * exp()
54+
55+
56+
57+
}

Arduino/SpiRob/style.css

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
html, body {
2+
margin: 0;
3+
padding: 0;
4+
overflow: hidden;
5+
}
6+
canvas {
7+
display: block;
8+
}
9+
10+
/* Add style to checkRedWhenCollide checkbox */
11+
#checkRedWhenCollide {
12+
font-family: halvetica, serif;
13+
}
14+
15+
.prevent-select {
16+
-webkit-user-select: none; /* Safari */
17+
-ms-user-select: none; /* IE 10 and IE 11 */
18+
user-select: none; /* Standard syntax */
19+
}

Discrete-Wave/index.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
3+
<html lang="en">
4+
5+
<head>
6+
<script src="https://cdn.jsdelivr.net/npm/p5@1.6.0/lib/p5.js"></script>
7+
<meta charset="UTF-8">
8+
<title>Discrete Wave</title>
9+
<link rel="icon" href="../icon.png">
10+
<link rel="stylesheet" type="text/css" href="style.css">
11+
</head>
12+
13+
<body>
14+
<main>
15+
</main>
16+
<script src="sketch.js"></script>
17+
</body>
18+
</html>

Discrete-Wave/sketch.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const FPS = 60;
2+
3+
let prisms = [];
4+
let grid_size = 30;
5+
let side = 30;
6+
let spacing = 10;
7+
let prism_count = grid_size ** 2;
8+
9+
let t = 0;
10+
11+
function setup() {
12+
createCanvas(windowWidth, windowHeight, WEBGL);
13+
frameRate(FPS);
14+
angleMode(DEGREES);
15+
colorMode(RGB);
16+
17+
if(navigator.userAgent.match(/iPhone|iPad|iPod|Android|webOs|BlackBerry|Windows Phone/i))
18+
pixelDensity(1);
19+
20+
for(let i = 0; i < grid_size; i++){
21+
for(let j = 0; j < grid_size; j++){
22+
let grid_length = grid_size * (side + spacing);
23+
let x = (side + spacing) * i - grid_length / 2;
24+
let y = (side + spacing) * j - grid_length / 2;
25+
26+
prisms.push(new Prism(x, y, side));
27+
}
28+
}
29+
30+
}
31+
32+
function draw() {
33+
background(40);
34+
ortho();
35+
orbitControl();
36+
rotateX(45);
37+
rotate(45);
38+
39+
for(let i = 0; i < prism_count; ++i){
40+
prisms[i].display();
41+
}
42+
43+
t += 2;
44+
}
45+
46+
class Prism{
47+
constructor(x, y, side){
48+
this.x = x;
49+
this.y = y;
50+
this.side = side;
51+
}
52+
53+
display(){
54+
push();
55+
translate(this.x, this.y, 0);
56+
57+
let r = sqrt(sq(this.x) + sq(this.y));
58+
59+
fill(abs(cos(t + r/2)) * 255, abs(sin(1.5 * (t + r/2))) * 255, abs(cos(2 * (t + r/2))) * 200);
60+
box(this.side, this.side, 150 * cos(t + r * cos(0.9 * t)) + 100 * sin(t));
61+
pop();
62+
}
63+
}

Discrete-Wave/style.css

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
html, body {
2+
margin: 0;
3+
padding: 0;
4+
overflow: hidden;
5+
}
6+
canvas {
7+
display: block;
8+
}
9+
10+
/* Add style to checkRedWhenCollide checkbox */
11+
#checkRedWhenCollide {
12+
font-family: halvetica, serif;
13+
}
14+
15+
.prevent-select {
16+
-webkit-user-select: none; /* Safari */
17+
-ms-user-select: none; /* IE 10 and IE 11 */
18+
user-select: none; /* Standard syntax */
19+
}

Enigma-Cipher/index.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
3+
<html lang="en">
4+
5+
<head>
6+
<script src="https://cdn.jsdelivr.net/npm/p5@1.6.0/lib/p5.js"></script>
7+
<meta charset="UTF-8">
8+
<title>Bouncy Ball</title>
9+
<link rel="icon" href="../icon.png">
10+
<link rel="stylesheet" type="text/css" href="style.css">
11+
</head>
12+
13+
<body>
14+
<main>
15+
</main>
16+
<script src="sketch.js"></script>
17+
</body>
18+
</html>

0 commit comments

Comments
 (0)