xxxxxxxxxx
205
let numero_cerchi_max = 100;
let numero_cerchi = 6;
let x = [numero_cerchi_max];
let y = [numero_cerchi_max];
let target_x = [numero_cerchi_max];
let target_y = [numero_cerchi_max];
let raggio;
let trigger = [numero_cerchi_max] , trigger_pre = [numero_cerchi_max];
let lunghezza = [numero_cerchi_max];
let soglia_trigger = 0.05;
let sintesi_osc;
let sintesi_mono;
let sintesi_poly;
let freq = [numero_cerchi_max];
let durata = [numero_cerchi_max];
let amp = 1 / numero_cerchi;
let slider;
function setup() {
createCanvas(400, 400);
// https://p5js.org/reference/#/p5/curve
let col = color(0)
slider = createSlider(3, numero_cerchi_max, 6);
slider.position(0, height + 5);
slider.style('width', '400px');
slider.style('color', 'col');
slider.style('background-color', 'col');
/*sintesi_osc = new p5.Oscillator('sine');
sintesi_mono = new p5.MonoSynth(sintesi_osc);
sintesi_poly = new p5.PolySynth(sintesi_mono, numero_cerchi); */
sintesi_poly = new p5.PolySynth();
sintesi_poly.setADSR(0.05, 0.2, 0.5, 1);
raggio = width * 0.01;
frameRate(30);
for(let i = 0; i < numero_cerchi_max; i++) {
x[i] = 0;
y[i] = 0;
target_x[i] = floor(random(width));
target_y[i] = floor(random(height));
trigger[i] = 0;
trigger_pre[i] = 0;
}
} //fine setup()
function draw() {
background(100);
numero_cerchi = slider.value();
for (let i = 0; i < numero_cerchi; i++) {
if (i < (numero_cerchi - 1)) {
stroke(0);
fill(0);
ellipse(x[i], y[i], raggio, raggio);
stroke(0 + 255 * trigger[i]);
line(x[i], y[i], x[i + 1], y[i + 1]);
lunghezza[i] = calcola_lunghezza(x[i], x[i + 1], y[i], y[i + 1]);
}
else {
ellipse(x[i], y[i], raggio, raggio);
stroke(255 - 255 * trigger[i]);
line(x[i], y[i], x[0], y[0]);
lunghezza[i] = calcola_lunghezza(x[i], x[0], y[i], y[0]);
}
if (x[i] < target_x[i]) {
x[i]++;
}
if (x[i] > target_x[i]) {
x[i]--;
}
if (x[i] == target_x[i]) {
target_x[i] = floor(random(width));
}
if (y[i] < target_y[i]) {
y[i]++;
}
if (y[i] > target_y[i]) {
y[i]--;
}
if (y[i] == target_y[i]) {
target_y[i] = floor(random(height));
}
freq[i] = map(lunghezza[i], 0, height, 400, 80);
// durata[i] = map(lunghezza[i], 0, height, 0.2, 1);
durata[i] = 0.1;
if (trigger[i] == 1 && trigger_pre[i] == 0) {
//attiva inviluppo sintesi sonora
userStartAudio();
sintesi_poly.noteAttack(freq[i], amp);
//sintesi_poly.play(freq[i], amp, 0, durata[i]);
}
if (trigger[i] == 0 && trigger_pre[i] == 1) {
sintesi_poly.noteRelease();
}
trigger_pre[i] = trigger[i];
trigger[i] = 0;
} //fine for generazione grafica e sonora
//-------------calcolo punto su linea----------------//
for (let i = 0; i < numero_cerchi; i++) {
for (let j = 0; j < numero_cerchi; j++) {
if (i < (numero_cerchi - 1)) {
if (j != i && j != (i + 1)) {
trigger[i] = calcola_punto_retta(x[i], y[i], x[i + 1], y[i + 1], x[j], y[j],
trigger[i]);
}
}
else {
trigger[numero_cerchi - 1] = calcola_punto_retta(x[numero_cerchi - 1],
y[numero_cerchi - 1],
x[0], y[0],
x[j], y[j],
trigger[numero_cerchi - 1]);
}
}
}
} //fine draw()
function calcola_lunghezza(x1, x2, y1, y2) {
return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
function calcola_punto_retta(x0, y0, x1, y1, x2, y2, trigga) {
let A = (x2 - x1) / (x0 - x1);
let B = (y2 - y1) / (y0 - y1);
if (trigga == 0) {
if ((abs(x2 - x1) <= soglia_trigger) && (abs(x0 - x1) <= soglia_trigger)) {
// il segmento tende a essere verticale
if (y0 < y1) {
if (y2 > y0 && y2 < y1) {
trigga = 1;
}
else {
if (y2 < y0 && y2 > y1) {
trigga = 1;
}
}
}
}
if ((abs(y2 - y1) <= soglia_trigger) && (abs(y0 - y1) <= soglia_trigger)) {
// il segmento tende a essere orizzontale
if (x0 < x1) {
if (x2 > x0 && x2 < x1) {
trigga = 1;
}
else {
if (x2 < x0 && x2 > x1) {
trigga = 1;
}
}
}
}
if (abs(A - B) <= soglia_trigger) {
if (x0 < x1) {
if ((x2 > x0) && (x2 < x1)) {
trigga = 1;
}
}
else {
if ((x2 < x0) && (x2 > x1)) {
trigga = 1;
}
}
}
}
return trigga;
}