xxxxxxxxxx
84
const PALETTE = [ "#152A84", "#776EAA", "#201315", "#DB1D6B", "#CDE4CF", "#008460", "#DBE479", "#F1C42F", "#E22733"]
let pal = [];
const HOR_ST = 64;
const VER_ST = 64;
const RES = 10;
let polygon = [{x:1,y:1},
{x:10,y:10},
{x:1,y:19}
]
function setup() {
createCanvas(640, 600);
stroke(255);
pal.push(PALETTE[floor(random(PALETTE.length))],PALETTE[floor(random(PALETTE.length))])
}
function draw() {
background(255);
polygon[1] = {
x:mouseX/RES,
y:mouseY/RES
}
for(let y=0;y<VER_ST;y++){
let drawing = 0;
fill(pal[drawing]);
// do a scanline
let scy=y+0.5;
let scx=[];
for(let i=0;i<polygon.length;i++){
//line segment is: plgn[i]>plgn[(i+1)%length]
let p1=polygon[i];
let p2=polygon[(i+1)%polygon.length]
if( (scy-p1.y) * (scy-p2.y) > 0 ){ continue; } //check if they intersect at all
scx.push( p1.x + (p2.x-p1.x) * ((scy-p1.y)/(p2.y-p1.y)) );
}
scx.sort(function (a, b) { return a-b; }); //this function return shit stops it from sorting alphabetically but idk why
for(let x=0;x<HOR_ST;x++){
//check for intersections
if(x+0.5 >= scx[0]){
drawing = 1 - drawing;
fill(pal[drawing]);
scx.shift();
}
// fill(pal[round(noise(x*0.1,y))]); // cute noise
drawStitch(x,y);
}
}
}
function drawStitch(x,y){
beginShape();
vertex((x+0.0)*RES,(y+0.0)*RES);
vertex((x+0.0)*RES,(y+1.0)*RES);
vertex((x+0.5)*RES,(y+1.5)*RES);
vertex((x+0.5)*RES,(y+0.5)*RES);
endShape(CLOSE);
beginShape();
vertex((x+1.0)*RES,(y+0.0)*RES);
vertex((x+0.5)*RES,(y+0.5)*RES);
vertex((x+0.5)*RES,(y+1.5)*RES);
vertex((x+1.0)*RES,(y+1.0)*RES);
endShape(CLOSE);
}