Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function iterate() {
- context.clearRect(0, 0, canvas.width, canvas.height);
- let detections = 0;
- if (mouseIsDown) {
- let velocityX = Math.random() * (100 + 50) + -50;
- let velocityY = Math.random() * (100 + 50) + -50;
- let x = mouseX;
- let y = mouseY;
- let r = 10.0;
- if (x + r > canvas.width) {
- let change = (x + r) - canvas.width;
- x -= change;
- } else if (x - r < 0) {
- let change = Math.abs(x - r);
- x += change;
- }
- if (y + r > canvas.height) {
- let change = (y + r) - canvas.height;
- y -= change;
- } else if (y - r < 0) {
- let change = Math.abs(y - r);
- y += change;
- }
- balls.push(new ball(x, y, r, velocityX, velocityY));
- }
- let quadTree = new QuadTree(boundaryAABB);
- for (let c of balls) {
- c.isColided = false;
- quadTree.insert(c);
- }
- for (let c of balls) {
- let searchedAABB = new AABB(c.x, c.y, c.r + 1);
- let foundballs = quadTree.queryRange(searchedAABB);
- for (let fb of foundballs) {
- if (c == fb) {
- continue;
- }
- detections++;
- if (c.intersects(fb)) {
- c.isColided = true;
- fb.isColided = true;
- break;
- }
- }
- }
- quadTree.draw(context, drawGridCheckbox.checked);
- d = new Date();
- changeTimeS = (d.getTime() / 1000.0) - lastTimeS;
- lastTimeS = d.getTime() / 1000.0;
- for (let c of balls) {
- let nextX = c.x + c.velocityX * changeTimeS;
- let nextY = c.y - c.velocityY * changeTimeS;
- if (nextX - c.r <= 0 || nextX + c.r >= canvas.width) {
- c.velocityX *= -1;
- c.x += c.velocityX * changeTimeS;
- } else {
- c.x = nextX;
- }
- if (nextY - c.r <= 0 || nextY + c.r >= canvas.height) {
- c.velocityY *= -1;
- c.y -= c.velocityY * changeTimeS;
- } else {
- c.y = nextY;
- }
- }
- ballCounter.textContent = balls.length;
- detectionCounter.textContent = detections;
- bruteforceCounter.textContent = Math.pow(balls.length, 2);
- ratio.textContent=Math.round((Math.pow(balls.length, 2)-detections)/(detections))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement