class Dot {
constructor(x, y, color, speed) {
this.x = x
this.y = y
this.color = color
this.speed = speed
}}
let x = 320;
let y = 180;
let xspeed = 5;
let yspeed = 2;
let speeds = [[5, 2], [2, 5]]
let array = [[320, 180], [450, 120]]
let colors = [[255, 0, 0], [0, 255, 255]]
let r = 10;
let dots = [new Dot(320, 180, [255, 0, 0], [5, 2]), new Dot(450, 120, [255, 255, 0], [2, 5])]
function setup() {
createCanvas(640, 360);
}
function draw() {
background(255);
for (let i = 0; i <= 1; i++) {
let c = color(dots[i].color[0], dots[i].color[1], dots[i].color[2]);
noStroke();
fill(c);
let x = dots[i].x
let y = dots[i].y
ellipse(x, y, r*2, r*2);
x += dots[i].speed[0];
y += dots[i].speed[1];
if (x > width - r || x < r) {
dots[i].speed[0] = -dots[i].speed[0];
}
if (y > height - r || y < r) {
dots[i].speed[1] = -dots[i].speed[1];
}
dots[i].x = x
dots[i].y = y
intersect(i)
}
}
function intersect(i) {
for (let x = 0; x < dots.length; x++) {
if (i!==x); {
if (x.color === [255, 0, 0]); {
var d = dist(x.x, x.y, i.x, i.y);
if (d < r + r) {
i.color = [255, 0, 0]
} else {
return false;
}
}}
}
}