Spaces:
Running
Running
shikharyashmaurya
commited on
Delete sketch.js
Browse files
sketch.js
DELETED
@@ -1,169 +0,0 @@
|
|
1 |
-
let boundaries = [];
|
2 |
-
let balls = [];
|
3 |
-
let drawingBoundary = false;
|
4 |
-
let boundaryDrawn = false;
|
5 |
-
var w = window.innerWidth;
|
6 |
-
var h = window.innerHeight;
|
7 |
-
|
8 |
-
function setup() {
|
9 |
-
canvas=createCanvas(w, h);
|
10 |
-
}
|
11 |
-
// function setup() {
|
12 |
-
// fullscreen(true);
|
13 |
-
// }
|
14 |
-
|
15 |
-
function draw() {
|
16 |
-
background(0);
|
17 |
-
|
18 |
-
// Draw the boundaries
|
19 |
-
// fill(220);
|
20 |
-
stroke(0);
|
21 |
-
strokeWeight(5);
|
22 |
-
if (boundaries.length >= 3) {
|
23 |
-
beginShape();
|
24 |
-
for (let v of boundaries) {
|
25 |
-
vertex(v.x, v.y);
|
26 |
-
}
|
27 |
-
endShape(CLOSE);
|
28 |
-
}
|
29 |
-
|
30 |
-
// Update and draw the balls
|
31 |
-
for (let ball of balls) {
|
32 |
-
ball.move();
|
33 |
-
ball.checkBoundaryCollision();
|
34 |
-
ball.display();
|
35 |
-
}
|
36 |
-
}
|
37 |
-
|
38 |
-
function mouseDragged() {
|
39 |
-
if (!drawingBoundary && !boundaryDrawn) {
|
40 |
-
boundaries = [];
|
41 |
-
drawingBoundary = true;
|
42 |
-
}
|
43 |
-
if (drawingBoundary) {
|
44 |
-
boundaries.push({ x: mouseX, y: mouseY });
|
45 |
-
}
|
46 |
-
}
|
47 |
-
|
48 |
-
function mouseReleased() {
|
49 |
-
drawingBoundary = false;
|
50 |
-
boundaryDrawn = true;
|
51 |
-
}
|
52 |
-
|
53 |
-
function mouseClicked() {
|
54 |
-
if (boundaryDrawn) {
|
55 |
-
balls.push(new Ball());
|
56 |
-
}
|
57 |
-
}
|
58 |
-
|
59 |
-
class Ball {
|
60 |
-
constructor() {
|
61 |
-
this.x = mouseX;
|
62 |
-
this.y = mouseY;
|
63 |
-
this.vx = random(-2, 2);
|
64 |
-
this.vy = random(-2, 2);
|
65 |
-
this.radius = 1;
|
66 |
-
}
|
67 |
-
|
68 |
-
move() {
|
69 |
-
this.x += this.vx;
|
70 |
-
this.y += this.vy;
|
71 |
-
}
|
72 |
-
|
73 |
-
|
74 |
-
checkBoundaryCollision() {
|
75 |
-
let isOutside = !this.isPointInPolygon(boundaries, this.x, this.y);
|
76 |
-
|
77 |
-
if (isOutside) {
|
78 |
-
// Find the closest boundary edge to the ball's center
|
79 |
-
let closestEdge = this.findClosestEdge(boundaries);
|
80 |
-
|
81 |
-
// Calculate the normal vector of the closest edge
|
82 |
-
let edgeNormal = p5.Vector.fromAngle(closestEdge.angle + HALF_PI);
|
83 |
-
|
84 |
-
// Reflect the velocity vector about the edge's normal vector
|
85 |
-
let reflectedVelocity = this.reflectVector(createVector(this.vx, this.vy), edgeNormal);
|
86 |
-
|
87 |
-
// Update the ball's velocity with the reflected vector
|
88 |
-
this.vx = reflectedVelocity.x;
|
89 |
-
this.vy = reflectedVelocity.y;
|
90 |
-
}
|
91 |
-
}
|
92 |
-
|
93 |
-
findClosestEdge(polygon) {
|
94 |
-
let closestEdge = null;
|
95 |
-
let closestDistance = Infinity;
|
96 |
-
|
97 |
-
for (let i = 0; i < polygon.length; i++) {
|
98 |
-
let j = (i + 1) % polygon.length;
|
99 |
-
let x1 = polygon[i].x;
|
100 |
-
let y1 = polygon[i].y;
|
101 |
-
let x2 = polygon[j].x;
|
102 |
-
let y2 = polygon[j].y;
|
103 |
-
|
104 |
-
let edge = { x1, y1, x2, y2 };
|
105 |
-
let distance = this.distanceToLine(edge);
|
106 |
-
|
107 |
-
if (distance < closestDistance) {
|
108 |
-
closestDistance = distance;
|
109 |
-
closestEdge = edge;
|
110 |
-
}
|
111 |
-
}
|
112 |
-
|
113 |
-
let dx = closestEdge.x2 - closestEdge.x1;
|
114 |
-
let dy = closestEdge.y2 - closestEdge.y1;
|
115 |
-
closestEdge.angle = atan2(dy, dx);
|
116 |
-
|
117 |
-
return closestEdge;
|
118 |
-
}
|
119 |
-
|
120 |
-
distanceToLine(edge) {
|
121 |
-
let x1 = edge.x1;
|
122 |
-
let y1 = edge.y1;
|
123 |
-
let x2 = edge.x2;
|
124 |
-
let y2 = edge.y2;
|
125 |
-
|
126 |
-
let dx = x2 - x1;
|
127 |
-
let dy = y2 - y1;
|
128 |
-
let a = dy;
|
129 |
-
let b = -dx;
|
130 |
-
let c = dx * y1 - dy * x1;
|
131 |
-
|
132 |
-
let dist = Math.abs(a * this.x + b * this.y + c) / Math.sqrt(a * a + b * b);
|
133 |
-
return dist;
|
134 |
-
}
|
135 |
-
|
136 |
-
reflectVector(vector, normal) {
|
137 |
-
let dotProduct = vector.x * normal.x + vector.y * normal.y;
|
138 |
-
let reflectedVector = p5.Vector.sub(vector, p5.Vector.mult(normal, 2 * dotProduct));
|
139 |
-
return reflectedVector;
|
140 |
-
}
|
141 |
-
|
142 |
-
isPointInPolygon(polygon, px, py) {
|
143 |
-
const epsilon = 0.01; // Adjust this value as needed
|
144 |
-
const radius = this.radius;
|
145 |
-
|
146 |
-
let isInside = false;
|
147 |
-
let j = polygon.length - 1;
|
148 |
-
|
149 |
-
for (let i = 0; i < polygon.length; i++) {
|
150 |
-
let x1 = polygon[i].x;
|
151 |
-
let y1 = polygon[i].y;
|
152 |
-
let x2 = polygon[j].x;
|
153 |
-
let y2 = polygon[j].y;
|
154 |
-
|
155 |
-
if ((y1 > py + radius + epsilon) !== (y2 > py + radius + epsilon) &&
|
156 |
-
px + radius + epsilon < ((x2 - x1) * (py + radius + epsilon - y1)) / (y2 - y1) + x1) {
|
157 |
-
isInside = !isInside;
|
158 |
-
}
|
159 |
-
|
160 |
-
j = i;
|
161 |
-
}
|
162 |
-
|
163 |
-
return isInside;
|
164 |
-
}
|
165 |
-
|
166 |
-
display() {
|
167 |
-
ellipse(this.x, this.y, this.radius * 2, this.radius * 2);
|
168 |
-
}
|
169 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|