xxxxxxxxxx
110
// inspired and modified from bellow
//
// M_1_5_02
//
// Generative Gestaltung – Creative Coding im Web
// ISBN: 978-3-87439-902-9, First Edition, Hermann Schmidt, Mainz, 2018
// Benedikt Groß, Hartmut Bohnacker, Julia Laub, Claudius Lazzeroni
// with contributions by Joey Lee and Niels Poldervaart
// Copyright 2018
//
// http://www.generative-gestaltung.de
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
var minus;
var plus;
var sketch = function( p ) {
var agents = [];
var agentCount = 5000;
var overlayAlpha = 5;
var strokeWidth = 1.5;
minus = p.createVector(p.windowWidth * 0.5, p.windowHeight*0.33);
plus = p.createVector(p.windowWidth * 0.5, p.windowHeight*0.66);
p.setup = function() {
p.createCanvas(p.windowWidth, p.windowHeight);
p.colorMode('RGB', 255);
for(var i = 0; i < agentCount; i++) {
agents[i] = new Agent();
}
}
p.draw = function() {
p.fill(0, 0, 25, overlayAlpha);
p.noStroke();
p.rect(0, 0, p.width, p.height);
// Draw agents
p.stroke(100, 255, 20, 60);
for (var i = 0; i < agentCount; i++) {
agents[i].update(strokeWidth);
}
}
};
var myp5 = new p5(sketch);
// -----2つの電荷を仮定する
//var plus = myp5.createVector(myp5.width*0.5, myp5.height/3);
//var minus = myp5.createVector(myp5.width*0.5, 400);
//var plus = myp5.createVector(300, 200);
//var minus = myp5.createVector(300, 400);
var Agent = function() {
this.vector = myp5.createVector(myp5.random(myp5.width), myp5.random(myp5.height));
this.vectorOld = this.vector.copy();
this.stepSize = myp5.random(1, 5);
this.isOutside = false;
this.angle;
this.ttl = 30 + Math.random()*100;
}
Agent.prototype.update = function(strokeWidth) {
this.ttl--;
let k_p = 30.0 /myp5.max(5, distance2( this.vector, plus));
let k_m = -30.0 /myp5.max(5, distance2( this.vector, minus));
this.vector.x += k_p * (this.vector.x - plus.x)
this.vector.x += k_m * (this.vector.x - minus.x)
this.vector.y += k_p * (this.vector.y - plus.y)
this.vector.y += k_m * (this.vector.y - minus.y)
this.isOutside = this.vector.x < 0 || this.vector.x > myp5.width || this.vector.y < 0 || this.vector.y > myp5.height;
if (this.isOutside || this.ttl <0) {
let x = myp5.width * myp5.random(100) * 0.01;
let y = myp5.height * myp5.random(100) * 0.01;
this.ttl = 50 + Math.random()*30;
this.vector.set(x, y);
this.vectorOld = this.vector.copy();
}
myp5.strokeWeight(strokeWidth);
myp5.line(this.vectorOld.x, this.vectorOld.y, this.vector.x, this.vector.y);
this.vectorOld = this.vector.copy();
this.isOutside = false;
}
function distance2(v1, v2){
return (v2.x-v1.x) * (v2.x-v1.x) + (v2.y-v1.y) * (v2.y-v1.y);
}