Skip to content

Commit

Permalink
Update Exercise “13dfd5b5-f0af-4783-9fe1-3ba7773bcb89” (#5143)
Browse files Browse the repository at this point in the history
Automatically generated. Merged on Decap CMS.
  • Loading branch information
kapostints authored Apr 12, 2024
2 parents 5bd6f20 + 2bebd12 commit 0b0b698
Showing 1 changed file with 3 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
"textColor": "#2E2E2E",
"backgroundColor": "#55e069"
},
"hidden": true,
"hidden": false,
"locked": false,
"name": "Compassionate intention",
"live": true,
Expand Down Expand Up @@ -137,7 +137,8 @@
"p5JsScript": {
"lang": "javascript",
"code": "let H;\nlet W;\n\nlet G;\n\nlet SEED;\n\nlet flocks = [];\nlet ID, ID2;\nlet PRE_COL;\n\nlet POSX = [];\nlet POSY = [];\nlet C1 = 7;\nlet C4 = 100;\nlet C02;\nlet C2 = 4;\n\nlet FRAME1 = 360; //360;\nlet FRAME2 = 180;\nlet FRAME3 = 180;\nlet FRAME4 = 90;\n\nlet BACK_COL;\n\nlet T_TYPE = [];\nlet T_SPEED = [];\nlet T_RAD = [];\nlet T_MCOH = [];\nlet T_ICOH = [];\n\nlet T_RED = [];\nlet T_GREEN = [];\nlet T_BLUE = [];\n\nlet OS_X = [];\nlet OS_Y = [];\n\nfunction setup() {\n W = windowWidth;\n H = windowHeight;\n createCanvas(W, H);\n G = createGraphics(W, H);\n\n //SEED = fxrand()*998244353;\n SEED = random() * 998244353;\n\n randomSeed(SEED);\n noiseSeed(SEED);\n\n G.rectMode(CENTER);\n G.noStroke();\n let bsel = int(random(2));\n let bcol = [60, 200];\n BACK_COL = bcol[bsel];\n\n\n ID = [0, 1, 2];\n ID2 = [0, 1, 2];\n shuffle(ID, true);\n shuffle(ID2, true);\n\n PRE_COL = [random(75) + 70, random(75) + 35, random(35)];\n\n for (let i = 0; i < C1; i++) {\n POSX[i] = random(-W / 3, W / 3) + W / 2;\n POSY[i] = random(-H / 4, H / 2) + H / 2;\n }\n\n let cycle1 = [120, 180, 360];\n let csel = int(random(3));\n let mx_coh = 1.0; //random(1.3, 1.5);\n flocks.push(new Flock());\n for (let i = 0; i < 500; ++i) {\n flocks[0].add(\n new Particle(\n 0,\n 0,\n 7.5,\n 150,\n FRAME1,\n cycle1[csel],\n 0.0,\n mx_coh,\n 10,\n int(random(C1)),\n ),\n );\n }\n\n T_RED = [];\n T_GREEN = [];\n T_BLUE = [];\n for (let i = 0; i < C4; ++i) {\n POSX[i] = random(-W / 2, W / 2) + W / 2;\n POSY[i] = random(-H / 2, H / 2) + H / 2;\n T_RED.push(random(30, 220));\n T_GREEN.push(random(30, 220));\n T_BLUE.push(random(30, 220));\n }\n\n let cycle4 = [45, 90];\n mx_coh = 0.6;\n flocks.push(new Flock());\n for (let i = 0; i < 500; ++i) {\n let csel4 = int(random(2));\n flocks[1].add(\n new Particle(\n 2,\n 1,\n 1.5,\n 20,\n FRAME4,\n cycle4[csel4],\n 0.0,\n mx_coh,\n 10,\n int(random(C4)),\n ),\n );\n }\n\n let mode = 1;\n if (mode == 1) {\n for (let ii = 0; ii < 2; ++ii) {\n let X2 = [];\n let n_tree = int(random(6, 10));\n let fdiv = 2;\n if (ii == 1) {\n n_tree = int((n_tree / 3) * 2);\n fdiv = 2.5;\n }\n for (let i = 0; i < n_tree; ++i) {\n X2.push(random(-W / fdiv, W / fdiv));\n }\n C02 = n_tree;\n for (let i = 0; i < C02; ++i) {\n POSX[i] = X2[i] + W / 2 + random(-15, 15);\n POSY[i] = random(-H / 2, -H / 3) + H / 3 + (H / 6) * ii; //top\n }\n\n T_TYPE = [];\n T_SPEED = [];\n T_RAD = [];\n T_MCOH = [];\n T_ICOH = [];\n\n T_RED = [];\n T_GREEN = [];\n T_BLUE = [];\n\n OS_X = [];\n OS_Y = [];\n\n for (let i = 0; i < C02; ++i) {\n T_TYPE.push(0); //int(random(2)));\n T_RED.push(random(50, 200));\n T_GREEN.push(random(50, 200));\n T_BLUE.push(random(50, 200));\n\n OS_X.push(random(-100, 100));\n OS_Y.push(random(H / 3));\n }\n\n FRAME2 = 180;\n let cycle1 = [180];\n let csel1 = int(random(1));\n mx_coh = 3;\n flocks.push(new Flock());\n for (let i = 0; i < 300; ++i) {\n let speed = random(6, 12);\n flocks[ii * 2 + 2].add(\n new Particle(\n 1,\n 1,\n speed,\n 20,\n FRAME2,\n cycle1[csel1],\n 0.5,\n mx_coh,\n ii * 10 + 15,\n int(random(C02)),\n ),\n );\n }\n\n T_SPEED = [2.5, 2];\n T_MCOH = [0.3, 1.0];\n T_ICOH = [0.0, 0.0];\n T_RAD = [20, 20];\n\n FRAME3 = 180;\n let cycle2 = [90, 180];\n\n flocks.push(new Flock());\n for (let i = 0; i < 500; ++i) {\n let csel2 = int(random(2));\n let tp_sel = int(random(C02));\n let tp = T_TYPE[tp_sel];\n flocks[ii * 2 + 3].add(\n new Particle(\n 1,\n 2,\n T_SPEED[tp],\n T_RAD[tp],\n FRAME3,\n cycle2[csel2],\n T_ICOH[tp],\n T_MCOH[tp],\n ii * 10 + 5,\n tp_sel,\n ),\n );\n }\n }\n }\n\n //let X2 = random(-W/2, W/2) + W/2;\n let Y2 = random(-H / 4, H / 2) + H / 2;\n for (let i = 0; i < C2; ++i) {\n POSX[i] = random(-W / 2, W / 2) + W / 2;\n POSY[i] = random(-H / 4, H / 4) + Y2;\n }\n}\n\nfunction draw() {\n blendMode(BLEND);\n background(BACK_COL);\n\n if (frameCount < FRAME1) {\n flocks[0].run();\n } else if (frameCount < FRAME1 + FRAME4) {\n flocks[1].run();\n } else if (frameCount < FRAME1 + FRAME4 + FRAME2) {\n flocks[2].run();\n } else if (frameCount < FRAME1 + FRAME4 + FRAME2 + FRAME3) {\n flocks[3].run();\n } else if (frameCount < FRAME1 + FRAME4 + FRAME2 * 2 + FRAME3) {\n flocks[4].run();\n } else if (frameCount < FRAME1 + FRAME4 + FRAME2 * 2 + FRAME3 * 2) {\n flocks[5].run();\n }\n\n image(G, 0, 0, width, height, 0, 0, W, H);\n}\n\nclass Flock {\n particles;\n\n constructor() {\n this.particles = [];\n }\n\n add(particle) {\n this.particles.push(particle);\n }\n\n run() {\n this.update();\n this.display();\n }\n\n update() {\n for (let i = 0; i < this.particles.length; ++i) {\n this.particles[i].update(this.particles);\n }\n }\n\n display() {\n for (let i = 0; i < this.particles.length; ++i) {\n this.particles[i].display();\n }\n }\n}\n\nclass Particle {\n acceleration;\n velocity;\n position;\n radius;\n max_r;\n color;\n col_sep;\n maxforce = 1.0;\n maxspeed = 2.5;\n minspeed = 0.001;\n mode;\n base;\n ali_f = 0.0;\n coh_f = 0.0;\n max_coh;\n alpha;\n fcnt;\n cycle;\n lcnt;\n fin;\n\n constructor(mode, base, speed, max_r, lcnt, cycle, coh, mxcoh, alpha, sel) {\n this.acceleration = createVector(0, 0);\n\n this.mode = mode;\n this.base = base;\n let offset_x;\n let offset_y;\n\n this.radius = 1;\n this.alpha = alpha;\n\n //let sel;\n if (this.base == 0) {\n this.maxspeed = speed;\n this.col_sep = 0.5;\n this.position = createVector(POSX[sel], POSY[sel]);\n this.max_r = max_r;\n this.velocity = createVector(random(-1, 1), random(-1, 1));\n }\n if (this.mode == 1) {\n if (this.base == 1) {\n offset_y = H / 2;\n this.maxspeed = speed;\n this.col_sep = 0.5;\n this.position = createVector(\n POSX[sel] + random(-5, 5),\n POSY[sel] + offset_y,\n );\n this.max_r = max_r;\n this.velocity = createVector(random(-1, 1), random(0.5, 1)); //;createVector(random(-1, 1), random(-1, 1));\n } else if (this.base == 2) {\n if (T_TYPE[sel] == 0) {\n offset_y = random(H / 2);\n this.maxspeed = speed * (offset_y / H) * (offset_y / H) * 5;\n this.col_sep = 0.5;\n this.position = createVector(POSX[sel], max(5, POSY[sel] + offset_y));\n this.max_r = max_r + (offset_y / H) * 50;\n this.velocity = createVector(random(-1, 1), random(0.1, 1));\n } else {\n offset_x = OS_X[sel];\n offset_y = OS_Y[sel];\n this.maxspeed = speed;\n this.col_sep = 0.5;\n this.position = createVector(\n POSX[sel] + offset_x,\n POSY[sel] + offset_y,\n );\n this.max_r = max_r;\n this.velocity = createVector(random(-1, 1), random(-1, 1));\n }\n }\n } else if (this.mode == 2) {\n let ratio = POSY[sel] / H + 0.2;\n this.maxspeed = speed * 1.2 * ratio;\n this.radius = this.radius * 1.2 * ratio;\n this.alpha = this.alpha * 1.2 * ratio;\n if (this.base == 1) {\n this.col_sep = 0.5;\n this.position = createVector(POSX[sel], POSY[sel]);\n this.max_r = max_r;\n this.velocity = createVector(random(-1, 1), random(-1, 1));\n }\n // else if(this.base == 2){\n // this.col_sep = 0.5;\n // this.position = createVector(POSX[sel], POSY[sel]);\n // this.max_r = max_r;\n // this.velocity = createVector(random(-1, 1), random(-1, 1));\n // }\n }\n\n //color\n if (this.base == 0) {\n this.color = createVector(\n max(0, PRE_COL[ID[0]] + random(-50, 30)),\n max(0, PRE_COL[ID[1]] + random(-50, 30)),\n max(0, PRE_COL[ID[2]] + random(-50, 30)),\n );\n }\n if (this.mode == 1) {\n if (this.base == 1) {\n this.color = createVector(\n min(\n 255,\n max(0, 200 - PRE_COL[ID[0]] + T_RED[sel] / 6 + random(-100, 0)),\n ),\n min(\n 255,\n max(0, 200 - PRE_COL[ID[1]] + T_GREEN[sel] / 6 + random(-100, 0)),\n ),\n min(\n 255,\n max(0, 200 - PRE_COL[ID[2]] + T_BLUE[sel] / 6 + random(-100, 0)),\n ),\n );\n } else if (this.base == 2) {\n this.color = createVector(\n min(255, max(0, T_RED[sel] + random(-50, 50))),\n min(255, max(0, T_GREEN[sel] + random(-50, 50))),\n min(255, max(0, T_BLUE[sel] + random(-50, 50))),\n );\n }\n } else if (this.mode == 2) {\n if (this.base == 1) {\n this.color = createVector(\n min(255, max(0, T_RED[sel] + random(-30, 30))),\n min(255, max(0, T_GREEN[sel] + random(-30, 30))),\n min(255, max(0, T_BLUE[sel] + random(-30, 30))),\n );\n } else if (this.base == 2) {\n this.color = createVector(\n max(100, min(255, PRE_COL[ID[0]] + random(0, 100))),\n max(100, min(255, PRE_COL[ID[1]] + random(0, 100))),\n max(100, min(255, PRE_COL[ID[2]] + random(0, 100))),\n );\n }\n }\n\n this.ali_f = coh;\n this.coh_f = coh;\n this.max_coh = mxcoh;\n\n this.fcnt = 0;\n this.cycle = cycle;\n this.fin = false;\n this.lcnt = lcnt;\n }\n\n applyForce(force) {\n this.acceleration.add(force);\n }\n\n inner_update(particles) {\n let sep = this.separate(particles);\n let ali = this.align(particles);\n let coh = this.cohesion(particles);\n\n sep.mult(1.5);\n ali.mult(this.ali_f);\n coh.mult(this.coh_f);\n\n this.applyForce(sep);\n this.applyForce(ali);\n this.applyForce(coh);\n }\n\n update(particles) {\n if (this.fcnt >= this.lcnt) {\n this.fin = true;\n }\n if (this.fin) {\n return;\n }\n this.inner_update(particles);\n\n this.velocity.add(this.acceleration);\n if (this.base == 0) {\n if (this.fcnt < this.lcnt / 2) {\n this.velocity.add(createVector(0, -0.1));\n }\n } else if (this.mode == 1 && this.base == 1) {\n this.velocity.add(createVector(0.0, 0.2));\n } else {\n if (this.fcnt < this.lcnt / 2) {\n this.velocity.add(createVector(-0.0, 0));\n }\n }\n\n this.velocity.limit(this.maxspeed);\n this.position.add(this.velocity);\n this.border();\n this.acceleration.mult(0);\n\n this.radius = abs(sin((this.fcnt / this.cycle) * PI)) * this.max_r + 1;\n\n if (this.base == 0) {\n this.ali_f = min(1.0, this.ali_f + 0.05);\n this.coh_f = min(this.max_coh, this.coh_f + 0.05);\n } else if (this.mode == 1 && this.base == 1) {\n this.ali_f = min(1.5, this.ali_f + 0.03);\n this.coh_f = min(this.max_coh, this.coh_f + 0.05);\n } else {\n this.ali_f = min(0.7, this.ali_f + 0.1);\n this.coh_f = min(this.max_coh, this.coh_f + 0.05);\n }\n\n if (this.fcnt < this.lcnt) {\n this.fcnt += 1;\n }\n }\n\n border() {\n //if(this.base != 1 || this.mode != 1){\n if (this.position.x > W) {\n this.position.x -= W;\n } else if (this.position.x < 0) {\n this.position.x += W;\n }\n if ((this.base != 1 || this.mode != 1) && this.mode != 2) {\n if (this.position.y > H) {\n this.position.y -= H;\n } else if (this.position.y < 0) {\n this.position.y += H;\n }\n }\n }\n\n seek(target) {\n let v = p5.Vector.sub(target, this.position);\n v.normalize();\n v.mult(this.maxspeed);\n v.sub(this.velocity);\n v.limit(this.maxforce);\n return v;\n }\n\n separate(particles) {\n let inter_range = 128.0;\n let v = createVector(0, 0, 0);\n for (let i = 0; i < particles.length; i++) {\n let d = p5.Vector.dist(this.position, particles[i].position);\n if (d <= 0) continue;\n\n let col_diff = p5.Vector.sub(this.color, particles[i].color).mag() - 110;\n if (d < inter_range + col_diff * this.col_sep) {\n let diff = p5.Vector.sub(this.position, particles[i].position);\n diff.normalize();\n diff.div(d);\n v.add(diff);\n }\n }\n\n if (v.mag() > 0) {\n v.normalize();\n v.mult(this.maxspeed);\n v.sub(this.velocity);\n v.limit(this.maxforce);\n }\n\n return v;\n }\n\n align(particles) {\n let inter_range = 128.0;\n let v = createVector(0, 0, 0);\n let count = 0;\n for (let i = 0; i < particles.length; i++) {\n let d = p5.Vector.dist(this.position, particles[i].position);\n //let col_diff = abs(this.color - particles[i].color) - 128;\n let col_diff = p5.Vector.sub(this.color, particles[i].color).mag() - 110;\n if (d > 0 && d < inter_range - col_diff * this.col_sep) {\n v.add(particles[i].velocity);\n count++;\n }\n }\n if (count > 0) {\n v.div(count);\n v.normalize();\n v.mult(this.maxspeed);\n v.sub(this.velocity);\n v.limit(this.maxforce);\n return v;\n } else {\n return createVector(0, 0, 0);\n }\n }\n\n cohesion(particles) {\n let neighbor_range = 128.0;\n let v = createVector(0, 0, 0);\n let count = 0;\n for (let i = 0; i < particles.length; i++) {\n let d = p5.Vector.dist(this.position, particles[i].position);\n\n let col_diff = p5.Vector.sub(this.color, particles[i].color).mag() - 110;\n if (d > 0 && d < neighbor_range - col_diff * this.col_sep) {\n v.add(particles[i].position);\n\n count++;\n }\n }\n if (count > 0) {\n v.div(count);\n return this.seek(v);\n } else {\n return createVector(0, 0, 0);\n }\n }\n\n display() {\n if (this.fin) {\n return;\n }\n let ratio = 1.0;\n //if (this.base == 0){\n //ratio = (H - this.position.y) / H * 1. + random(1.0);\n ratio = 0.8 + random(0.5);\n //}\n if (this.base == 0) {\n ratio = ratio * (3.0 - 3 * sqrt(this.position.y / H) * random(0.8, 1.2));\n }\n if (this.mode == 1 && this.base == 2) {\n ratio = ratio + random(-0.2, 1);\n }\n G.strokeWeight(this.radius);\n G.stroke(\n this.color.x * ratio,\n this.color.y * ratio,\n this.color.z * ratio,\n this.alpha,\n );\n G.point(this.position.x, this.position.y);\n }\n}"
}
},
"audio": "https://res.cloudinary.com/cupcake-29k/video/upload/v1712905234/Audio/portal_audio_compassionate_intention_uniaec.mp3"
}
},
"tags": [
Expand Down

0 comments on commit 0b0b698

Please sign in to comment.