# 円の交点

```/**
* intersection points of circles
*
* @date 2017/03/04
*/

var circles;

function setup() {
createCanvas(windowWidth, windowHeight);
circles = [];
for (var i = 0; i < 30; i++) {
circles.push(new Circle());
}
}

function draw() {
background(255);
var interPoints = [];
noFill();
stroke(210);
for (var i = 0; i < circles.length; i++) {
var c1 = circles[i];
c1.render();
for (var j = i + 1; j < circles.length; j++) {
var c2 = circles[j];
Array.prototype.push.apply(interPoints, c1.getIntersectionPoints(c2));
}
}

for (var i = 0; i < interPoints.length; i++) {
var p = interPoints[i];
noStroke();
fill(167, 117, 160);
ellipse(p.x, p.y, 4, 4);
}

for (var i = 0; i < circles.length; i++) {
var circle = circles[i];
circle.update();
}
}

function Circle() {
var velSize = random(0.2, 2);
var velAng = random(TWO_PI);
this.vel = createVector(velSize * cos(velAng), velSize * sin(velAng));
}

Circle.prototype = {
render: function() {
},

update: function() {
this.vel.x *= -1;
this.loc.x += this.vel.x;
}
if (this.loc.x > width - this.radius) {
this.vel.x *= -1;
this.loc.x += this.vel.x;
}
this.vel.y *= -1;
this.loc.y += this.vel.y;
}
if (this.loc.y > height - this.radius) {
this.vel.y *= -1;
this.loc.y += this.vel.y;
}
},

getIntersectionPoints: function(c) {
var relLoc = p5.Vector.sub(c.loc, this.loc);