-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
47 lines (40 loc) · 1.15 KB
/
app.js
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
import Ball from './Ball.js';
import World from './World.js';
window.onload = run;
function run() {
var canvas = document.getElementById('app');
var objects = randomBalls(10, canvas.clientWidth, canvas.clientHeight);
new World(canvas, objects).run();
}
function randomBalls(n, width, height) {
var balls = new Array(n);
for (var i = 0; i < balls.length; i++) {
balls[i] = randomNonIntersectingBall(width, height, balls);
}
return balls;
}
function randomNonIntersectingBall(width, height, balls) {
var ball;
do {
ball = randomBall(width, height);
}
while (intersecting(ball, balls));
return ball;
}
function intersecting(ball, balls) {
for (var i = 0; i < balls.length && balls[i] !== undefined; i++) {
if (ball.isBallColliding(balls[i])) {
return true;
}
}
return false;
}
function randomBall(width, height) {
var radius = 25 + Math.random() * 25;
var x = radius + Math.random() * (width - 2 * radius);
var y = radius + Math.random() * (height - 2 * radius);
var dx = Math.random() - 0.5;
var dy = Math.random() - 0.5;
var color = '#' + parseInt(Math.random() * 0xFFFFFF).toString(16);
return new Ball(x, y, radius, dx, dy, color);
}