xxxxxxxxxx
81
p5.disableFriendlyErrors = true;
({max,floor,abs,sin,cos,min,PI}=Math);
function setup() {
l = min(windowWidth, windowHeight);
createCanvas(w = l, h = l);
bf = createGraphics(w, h);
drawBuffer(bf);
}
function draw() {
background("#0a0a0a");
image(bf, 0, 0);
}
// Functions
function sdf([x, y]) {
let fig_1 = abs(sdf_rep(sdf_circle([x, y], [-.2, 0], .1), .2)) - .05;
let fig_2 = abs(sdf_rep(sdf_circle([x, y], [.2,0], .1),.2)) - .05;
let fig_3 = abs(sdf_rep(sdf_box([x, y], [.2, 0], [0.1, 0.1]), .2)) - 0.05;
let fig_4 = abs(sdf_rep(sdf_box([x, y], [-.2, 0], [0.1, 0.1]), .2)) - 0.05;
let fig_5 = abs(sdf_rep(sdf_box([x, y], [0, .4], [0.1, 0.1]), .2)) - 0.05;
let fig_6 = abs(sdf_rep(sdf_box([x, y], [0, -.4], [0.1, 0.1]), .2)) - 0.05;
// return Math.max(fig_2, fig_1)
return Math.min(fig_3, fig_4, fig_5, fig_6)
}
function sdf_circle([x, y], [cx, cy], r) {
x -= cx;
y -= cy;
return L(x, y) - r;
}
function sdf_box([x,y], [cx,cy], [w,h]) {
x -= cx;
y -= cy;
return max(abs(x)-w, abs(y)-h);
}
function sdf_rep(x, r) {
x /= r;
x -= Math.floor(x) + 0.5;
x *= r;
return x;
}
// Drawing the buffer
function drawBuffer(buff) {
buff.loadPixels()
let d = pixelDensity()
let len = 4 * (w * d) * (h * d)
for (let idx = 0; idx < len; idx += 4) {
let x = 2 * (((idx / 4) % (w * d))) / (w * d) - 1
let y = 2 * Math.floor((idx / 4) / (w * d)) / (w * d) - 1
let res = sdf([x, y])
if (res < -0.01) drawPixel(buff.pixels, idx, 237, 34, 93)
if (res > 0.01) drawPixel(buff.pixels, idx, 230, 230, 230)
}
buff.updatePixels()
}
function drawPixel(arr, i, r, g, b) {
arr[i + 0] = r;
arr[i + 1] = g;
arr[i + 2] = b;
arr[i + 3] = 255
}
let R = (lim=1) => Mate.random() * lim
let L = (x, y) => (x * x + y * y) ** 0.5
let k = (a,b)=>a>0&&b>0?L(a,b):a>b?a:b;