Spaces:
Running
Running
File size: 5,985 Bytes
49549db |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
let boundaries = [];
let balls = [];
let drawingBoundary = false;
let boundaryDrawn = false;
let w, h;
let entropyGraph = [];
let startTime;
function setup() {
w = windowWidth;
h = windowHeight;
canvas = createCanvas(w, h);
startTime = millis();
}
function draw() {
background(0);
// Draw the boundaries
stroke(255);
strokeWeight(2);
if (boundaries.length >= 3) {
beginShape();
for (let v of boundaries) {
vertex(v.x, v.y);
}
endShape(CLOSE);
}
// Update and draw the balls
for (let ball of balls) {
ball.move();
ball.checkBoundaryCollision();
ball.display();
}
// Calculate and display entropy
if (boundaryDrawn && balls.length > 0) {
let entropy = calculateEntropy();
entropyGraph.push(entropy);
displayEntropyGraph();
}
// Display instructions
displayInstructions();
}
function mouseDragged() {
if (!drawingBoundary && !boundaryDrawn) {
boundaries = [];
drawingBoundary = true;
}
if (drawingBoundary) {
boundaries.push({ x: mouseX, y: mouseY });
}
}
function mouseReleased() {
drawingBoundary = false;
boundaryDrawn = true;
}
function mouseClicked() {
if (boundaryDrawn) {
balls.push(new Ball());
}
}
class Ball {
constructor() {
this.x = mouseX;
this.y = mouseY;
this.vx = random(-2, 2);
this.vy = random(-2, 2);
this.radius = 2;
this.color = color(random(100, 255), random(100, 255), random(100, 255));
}
move() {
this.x += this.vx;
this.y += this.vy;
}
checkBoundaryCollision() {
let isOutside = !this.isPointInPolygon(boundaries, this.x, this.y);
if (isOutside) {
let closestEdge = this.findClosestEdge(boundaries);
let edgeNormal = p5.Vector.fromAngle(closestEdge.angle + HALF_PI);
let reflectedVelocity = this.reflectVector(createVector(this.vx, this.vy), edgeNormal);
this.vx = reflectedVelocity.x;
this.vy = reflectedVelocity.y;
// Move the ball back inside the boundary
this.x = constrain(this.x, closestEdge.x1, closestEdge.x2);
this.y = constrain(this.y, closestEdge.y1, closestEdge.y2);
}
}
// findClosestEdge(polygon) {
// // ... (keep the existing implementation)
// }
// distanceToLine(edge) {
// // ... (keep the existing implementation)
// }
// reflectVector(vector, normal) {
// // ... (keep the existing implementation)
// }
// isPointInPolygon(polygon, px, py) {
// // ... (keep the existing implementation)
// }
findClosestEdge(polygon) {
let closestEdge = null;
let closestDistance = Infinity;
for (let i = 0; i < polygon.length; i++) {
let j = (i + 1) % polygon.length;
let x1 = polygon[i].x;
let y1 = polygon[i].y;
let x2 = polygon[j].x;
let y2 = polygon[j].y;
let edge = { x1, y1, x2, y2 };
let distance = this.distanceToLine(edge);
if (distance < closestDistance) {
closestDistance = distance;
closestEdge = edge;
}
}
let dx = closestEdge.x2 - closestEdge.x1;
let dy = closestEdge.y2 - closestEdge.y1;
closestEdge.angle = atan2(dy, dx);
return closestEdge;
}
distanceToLine(edge) {
let x1 = edge.x1;
let y1 = edge.y1;
let x2 = edge.x2;
let y2 = edge.y2;
let dx = x2 - x1;
let dy = y2 - y1;
let a = dy;
let b = -dx;
let c = dx * y1 - dy * x1;
let dist = Math.abs(a * this.x + b * this.y + c) / Math.sqrt(a * a + b * b);
return dist;
}
reflectVector(vector, normal) {
let dotProduct = vector.x * normal.x + vector.y * normal.y;
let reflectedVector = p5.Vector.sub(vector, p5.Vector.mult(normal, 2 * dotProduct));
return reflectedVector;
}
isPointInPolygon(polygon, px, py) {
const epsilon = 0.01; // Adjust this value as needed
const radius = this.radius;
let isInside = false;
let j = polygon.length - 1;
for (let i = 0; i < polygon.length; i++) {
let x1 = polygon[i].x;
let y1 = polygon[i].y;
let x2 = polygon[j].x;
let y2 = polygon[j].y;
if ((y1 > py + radius + epsilon) !== (y2 > py + radius + epsilon) &&
px + radius + epsilon < ((x2 - x1) * (py + radius + epsilon - y1)) / (y2 - y1) + x1) {
isInside = !isInside;
}
j = i;
}
return isInside;
}
display() {
fill(this.color);
noStroke();
ellipse(this.x, this.y, this.radius * 2, this.radius * 2);
}
}
function calculateEntropy() {
const gridSize = 10;
const grid = {};
const totalBalls = balls.length;
for (let ball of balls) {
const gridX = Math.floor(ball.x / gridSize);
const gridY = Math.floor(ball.y / gridSize);
const key = `${gridX},${gridY}`;
grid[key] = (grid[key] || 0) + 1;
}
let entropy = 0;
for (let count of Object.values(grid)) {
const probability = count / totalBalls;
entropy -= probability * Math.log2(probability);
}
return entropy;
}
function displayEntropyGraph() {
const graphWidth = 200;
const graphHeight = 100;
const x = width - graphWidth - 10;
const y = height - graphHeight - 10;
fill(0, 150);
rect(x, y, graphWidth, graphHeight);
stroke(255);
noFill();
beginShape();
for (let i = 0; i < entropyGraph.length; i++) {
const px = map(i, 0, entropyGraph.length - 1, x, x + graphWidth);
const py = map(entropyGraph[i], 0, 5, y + graphHeight, y);
vertex(px, py);
}
endShape();
fill(255);
noStroke();
textAlign(RIGHT);
text(`Entropy: ${entropyGraph[entropyGraph.length - 1].toFixed(2)}`, x + graphWidth, y - 5);
}
function displayInstructions() {
fill(255);
noStroke();
textAlign(LEFT);
textSize(14);
text("1. Draw a boundary by dragging the mouse", 10, 20);
text("2. Click inside the boundary to add balls", 10, 40);
text("3. Observe the entropy graph in the bottom right", 10, 60);
}
function windowResized() {
w = windowWidth;
h = windowHeight;
resizeCanvas(w, h);
}
|