xxxxxxxxxx
//Adapted from Daniel Shiffman's Mathematical Marbling tutorial- https://thecodingtrain.com/challenges/183-mathematical-marbling
let drops=[]
let colors=["#2a274a","#3d3829","#729880","#d0d074","#719c67","#70732b","#a6690e","#a53c0b"]
let VerticesNO=300
let xLine, yLine, bgColor
function setup() {
let m= min(windowWidth, windowHeight)
createCanvas(m, m);
let bgIndex= floor(random(colors.length))
bgColor= colors[bgIndex]
colors.splice(bgIndex, 1)
xLine= width*0.1
yLine= random(height*0.5)
}
function draw() {
background(bgColor)
if(frameCount<220 && frameCount%5==0){
addInk(noise(frameCount/10)*width, noise(frameCount/11)*height, random(width*0.029))
}
if(frameCount>220 && frameCount<270){
addInk(noise(frameCount/10)*width, noise(frameCount/11)*height, random(width*0.007))
}
if(frameCount>270 && frameCount <290){
addInk(xLine+random(-5, 5), yLine+ ((frameCount-270)*10)+random(-2, 2), random(width*0.007))
}
if(frameCount== 300){
let v=createVector(1, 0)
tineLine(v, 1, height*0.1, 70, 5)
tineLine(v, 1, height*0.75, 70, 5)
let v2=createVector(0, 1)
tineLine(v2, width*0.1, 1, 70, 5)
tineLine(v2, width*0.9, 1, 70, 5)
noLoop()
}
for(let d of drops){
d.show()
d.grow()
}
}
function addInk(x, y, r){
let droppy = new drop(x, y, r)
for(let other of drops){
other.marble(droppy)
}
drops.push(droppy)
}
function tineLine(v, x, y, z, c){
for(let d of drops){
d.tine(v, x, y, z, c)
}
}
class drop{
constructor(x, y, r){
this.pos= createVector(x, y)
this.r=1
this.finalR=r
this.color= random(colors)
this.points=[]
for(let i=0; i<VerticesNO; i++){
let angle=map(i, 0, VerticesNO, 0, TAU)
let v=createVector(x+cos(angle)*r, y+sin(angle)*r)
this.points.push(v)
}
}
grow(){
if(this.r<this.finalR){
this.r++
for(let other of drops){
other.marble(this)
}
}
}
tine(m, x, y, z, c){
let u=1/pow(2, 1/c)
for(let pt of this.points){
let b= createVector(x, y)
let pb= p5.Vector.sub(pt, b)
let n= m.copy().rotate(HALF_PI)
let d= abs(pb.dot(n))
let mag=z*pow(u, d)
pt.add(m.copy().mult(mag))
}
}
hTine(x, z, c){
let u=1/pow(2, 1/c)
for(let pt of this.points){
pt.y=pt.y+z*pow(u, abs(pt.x-x))
}
}
marble(other){
for(let pt of this.points){
let c=other.pos
let p= pt.copy()
let r= other.r
p.sub(c)
let m= p.mag()
let root= sqrt(1+ (r*r)/(m*m))
p.mult(root)
p.add(c)
pt.set(p)
}
}
show(){
fill(this.color)
noStroke()
beginShape()
for(let i=0; i<this.points.length; i++){
vertex(this.points[i].x, this.points[i].y)
}
endShape()
}
}