xxxxxxxxxx
182
// ##################
// ###### Setup #####
// ##################
function setup() {
createCanvas(600, 600);
frameRate(5);
}
// #################
// ###### Draw #####
// #################
function draw() {
//console.log(Maths.PHI);dkdddd
//
grainCircle(width/2, height/2, 500, color(250,244,233,120),PI/3);
grainCircle(width/2, height/2, 250, color(190,80,70,120),PI/3);
grainCircle(width/2, height/2, 250, color(40,30,125,120),4*PI/3);
// let MAX = 10;
// for(let i = 0 ; i < MAX ; i++){
// for(let j = 0 ; j < MAX ; j++){
//
// let p1x = i*width/(MAX+1);
// let p1y = j*width/(MAX+1);
//
// let p2x = (i+1)*width/(MAX+1);
// let p2y = j*width/(MAX+1);
//
// let p3x = i*width/(MAX+1);
// let p3y = (j+1)*width/(MAX+1);
//
// grainTriangle(p1x,p1y,p2x,p2y,p3x,p3y);
//
// }
// }
save();
noLoop();
}
function grainCircle(cx,cy,cr,c= color(0), dir=TWO_PI){
stroke(c);
const AREA = PI * cr * cr;
// LIGHTING SYSTEM
const v = p5.Vector.fromAngle(dir,cr); // pick random point on edge of sphere
const light = createVector(cx+v.x, cy+v.y);
// GENERATE POINTS
for(let i = 0; i < AREA / 1.5; i++){
strokeWeight(random(1,2)); // so that it is not pixels
while(true){
const p = Maths.pointInCircle(cx, cy, cr);
const probability = map(dist(p.x, p.y, light.x, light.y),0,(cr*2),1,0);
if(random() < probability == 0){ // I DONT UDBNERSTAND WHY I HAVE AN EQL 0 here
let rx = 5
let ry = 2
point(p.x+random(-rx,rx),p.y+random(-ry,ry));
break;
}
}
}
}
function grainTriangle(x0, y0, x1, y1, x2, y2){
strokeWeight(1.1); // so that it is not pixels
const AREA = ( x0 * ( y1 - y2 ) + x1 * ( y2 - y0 ) + x2 * (y0 - y1) ) / 2;
// LIGHTING SYSTEM
// const v = p5.Vector.fromAngle(dir,cr); // pick random point on edge of sphere
// const light = createVector(cx+v.x, cy+v.y);
// GENERATE POINTS
for(let i = 0; i < AREA / 2 ; i++){
//while(true){
//const p = pointInCircle(cx, cy, cr);
const p = Maths.pointInTriangle(x0,y0,x1,y1,x2,y2);
//const probability = map(dist(p.x, p.y, light.x, light.y),0,(cr*2),1,0);
//if(random() < probability == 0){ // I DONT UDBNERSTAND WHY I HAVE AN EQL 0 here
point(p.x,p.y);
//break;
//}
//}
}
}
///////////////////////////////////////////////
class Maths{
//static PHI = 1.6543;
static distSq(x1, y1, x2, y2){ return (x2 - x1)**2 + (y2 - y1)**2; }
// Vector Functions
static distSq(v1, v2) { return (v2.x - v1.x) ** 2 + (v2.y - v1.y) ** 2; }
static midpoint(v1, v2) { return (createVector((v1.x + v2.x) / 2, (v1.y + v2.y) / 2)); }
// PROBABILITY
// static randInt(min, max){ return int(random(min, max+1)); }
// LINEAR AND NON LINEAR MAPPING
// EXPIN EXPOUT EXPINOUT
// QUADRATICIN OUT
// static expInOut(x) {
// if(x == 0.0 || x == 1.0) return x;
//
// if(x < 0.5){
// return(0.5 * pow(2, (20 * x) - 10));
// }
// else{
// return(-0.5 * pow(2, (-20 * x) + 10) + 1);
// }
// }
// GEOMETRY
// # Geometry
// # Check if points are inside polygons
static isPointinCircle(px, py, cx, cy, cr){ return (distSq(px, py, cx, cy) < cr**2); }
static isPointinSquare(){}
// # Generate uniform points inside polygons
static pointInQuad(){}
static pointInSquare(){}
static pointInTriangle(x0, y0, x1, y1, x2, y2){
// we must put x1,x2 and y1 y2 at the origin before transformation
// and put them back afterwards
let a1 = random(); // nice fades if i put an sqrt here
let a2 = random();
if((a1 + a2) >=1 ){ // reflect the points that are outside 1st triangle
a1 = 1 - a1; // https://blogs.sas.com/content/iml/2020/10/19/random-points-in-triangle.html
a2 = 1 - a2;
}
const px = a1 * (x1-x0) + a2 * (x2-x0) + x0;
const py = a1 * (y1-y0) + a2 * (y2-y0) + y0;
return createVector(px, py);
}
static pointInCircle(cx, cy, cr){
// uniformely generate points in a circle
// https://stackoverflow.com/questions/5837572/generate-a-random-point-within-a-circle-uniformly
const r = cr * Math.sqrt(Math.random()); // if i add a second square root here I get something more intense along the curves
const theta = Math.random() * 2 * Math.PI; // if we add an sqrt here it does a neat spiral
const px = cx + r * Math.cos(theta)
const py = cy + r * Math.sin(theta)
return {x : px, y : py};
}
}
// DRAW
// object = mathematical function
// functions : draw
// functions : derive
// functions : integral
// find solutions
// intersect with other objects