Skip to content

Commit 712b3b6

Browse files
Normal Binary Search
0 parents  commit 712b3b6

File tree

26 files changed

+957
-0
lines changed

26 files changed

+957
-0
lines changed

.vscode/launch.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "pwa-chrome",
9+
"request": "launch",
10+
"name": "Launch Chrome against localhost",
11+
"url": "http://localhost:8080",
12+
"webRoot": "${workspaceFolder}"
13+
}
14+
]
15+
}

A* Algorithm/index.html

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Document</title>
7+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.js"></script>
8+
<script src="sketch.js"></script>
9+
<script src="spot.js"></script>
10+
11+
</head>
12+
<body>
13+
14+
</body>
15+
</html>

A* Algorithm/sketch.js

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
function removeFromArray(arr, elt) {
2+
for (var i = arr.length - 1; i >= 0; i--) {
3+
if (arr[i] == elt) {
4+
arr.splice(i, 1);
5+
}
6+
}
7+
}
8+
9+
function heuristic(a, b) {
10+
var d = dist(a.i, a.j, b.i, b.j);
11+
return d;
12+
}
13+
14+
var cols = 50;
15+
var rows = 50;
16+
17+
var grid = new Array(cols);
18+
19+
var openSet = [];
20+
var closedSet = [];
21+
22+
var start;
23+
var end;
24+
25+
var w, h;
26+
27+
var path = [];
28+
29+
function setup() {
30+
createCanvas(400, 400);
31+
console.log('A*');
32+
33+
w = width / cols;
34+
h = height / rows;
35+
36+
for (var i = 0; i < cols; i++) {
37+
grid[i] = new Array(rows);
38+
}
39+
40+
for (var i = 0; i < cols; i++) {
41+
for (var j = 0; j < rows; j++) {
42+
grid[i][j] = new Spot(i, j);
43+
}
44+
}
45+
46+
for (var i = 0; i < cols; i++) {
47+
for (var j = 0; j < rows; j++) {
48+
grid[i][j].addNeighbors(grid);
49+
}
50+
}
51+
52+
53+
start = grid[0][0];
54+
end = grid[cols - 1][rows - 1];
55+
start.wall = false;
56+
end.wall = false;
57+
58+
openSet.push(start);
59+
}
60+
61+
function draw() {
62+
63+
if (openSet.length > 0) {
64+
65+
var winner = 0;
66+
for (var i = 0; i < openSet.length; i++) {
67+
if (openSet[i].f < openSet[winner].f) {
68+
winner = i;
69+
}
70+
}
71+
var current = openSet[winner];
72+
73+
if (current === end) {
74+
noLoop();
75+
console.log("DONE!");
76+
}
77+
78+
removeFromArray(openSet, current);
79+
closedSet.push(current);
80+
81+
var neighbors = current.neighbors;
82+
for (var i = 0; i < neighbors.length; i++) {
83+
var neighbor = neighbors[i];
84+
85+
if (!closedSet.includes(neighbor) && !neighbor.wall) {
86+
var tempG = current.g + heuristic(neighbor, current);
87+
88+
var newPath = false;
89+
if (openSet.includes(neighbor)) {
90+
if (tempG < neighbor.g) {
91+
neighbor.g = tempG;
92+
newPath = true;
93+
}
94+
} else {
95+
neighbor.g = tempG;
96+
newPath = true;
97+
openSet.push(neighbor);
98+
}
99+
100+
if (newPath) {
101+
neighbor.h = heuristic(neighbor, end);
102+
neighbor.f = neighbor.g + neighbor.h;
103+
neighbor.previous = current;
104+
}
105+
}
106+
107+
}
108+
} else {
109+
console.log('no solution');
110+
noLoop();
111+
return;
112+
}
113+
114+
background(255);
115+
116+
for (var i = 0; i < cols; i++) {
117+
for (var j = 0; j < rows; j++) {
118+
grid[i][j].show();
119+
}
120+
}
121+
122+
for (var i = 0; i < closedSet.length; i++) {
123+
closedSet[i].show(color(255, 0, 0, 50));
124+
}
125+
126+
for (var i = 0; i < openSet.length; i++) {
127+
openSet[i].show(color(0, 255, 0, 50));
128+
}
129+
130+
131+
path = [];
132+
var temp = current;
133+
path.push(temp);
134+
while (temp.previous) {
135+
path.push(temp.previous);
136+
temp = temp.previous;
137+
}
138+
139+
140+
141+
noFill();
142+
stroke(255, 0, 200);
143+
strokeWeight(w / 2);
144+
beginShape();
145+
for (var i = 0; i < path.length; i++) {
146+
vertex(path[i].i * w + w / 2, path[i].j * h + h / 2);
147+
}
148+
endShape();
149+
150+
151+
152+
}

A* Algorithm/spot.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
function Spot(i, j) {
2+
3+
// Location
4+
this.i = i;
5+
this.j = j;
6+
7+
// f, g, and h values for A*
8+
this.f = 0;
9+
this.g = 0;
10+
this.h = 0;
11+
12+
// Neighbors
13+
this.neighbors = [];
14+
15+
// Where did I come from?
16+
this.previous = undefined;
17+
18+
// Am I a wall?
19+
this.wall = false;
20+
if (random(1) < 0.4) {
21+
this.wall = true;
22+
}
23+
24+
// Display me
25+
this.show = function(col) {
26+
if (this.wall) {
27+
fill(0);
28+
noStroke();
29+
ellipse(this.i * w + w / 2, this.j * h + h / 2, w / 2, h / 2);
30+
} else if (col) {
31+
fill(col);
32+
rect(this.i * w, this.j * h, w, h);
33+
}
34+
}
35+
36+
// Figure out who my neighbors are
37+
this.addNeighbors = function(grid) {
38+
var i = this.i;
39+
var j = this.j;
40+
if (i < cols - 1) {
41+
this.neighbors.push(grid[i + 1][j]);
42+
}
43+
if (i > 0) {
44+
this.neighbors.push(grid[i - 1][j]);
45+
}
46+
if (j < rows - 1) {
47+
this.neighbors.push(grid[i][j + 1]);
48+
}
49+
if (j > 0) {
50+
this.neighbors.push(grid[i][j - 1]);
51+
}
52+
if (i > 0 && j > 0) {
53+
this.neighbors.push(grid[i - 1][j - 1]);
54+
}
55+
if (i < cols - 1 && j > 0) {
56+
this.neighbors.push(grid[i + 1][j - 1]);
57+
}
58+
if (i > 0 && j < rows - 1) {
59+
this.neighbors.push(grid[i - 1][j + 1]);
60+
}
61+
if (i < cols - 1 && j < rows - 1) {
62+
this.neighbors.push(grid[i + 1][j + 1]);
63+
}
64+
}
65+
}

Binary Search/box.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
class Box {
3+
constructor(v,ind) {
4+
this.ind = ind;
5+
this.v = v;
6+
this.in = true;
7+
this.answer = false;
8+
this.current = false;
9+
}
10+
11+
show(){
12+
stroke(255);
13+
noFill();
14+
if(this.in==false){
15+
fill(255,0,0);
16+
}
17+
else fill(0,0,255);
18+
if(this.current) {
19+
// stroke(100);
20+
noStroke();
21+
fill(100,0,100);
22+
}
23+
if(this.answer){
24+
fill(0,255,0);
25+
}
26+
// fill(255);
27+
rect(this.ind * w,height/4,w,50);
28+
textAlign(CENTER);
29+
fill(255);
30+
// console.log(this.v);
31+
text(this.v,this.ind * w + w/2,height/4 + 20 + 2)
32+
}
33+
34+
35+
}

Binary Search/index.html

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Document</title>
7+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.js"></script>
8+
<script src="sketch.js"></script>
9+
<script src="box.js"></script>
10+
11+
</head>
12+
<body>
13+
14+
</body>
15+
</html>

0 commit comments

Comments
 (0)