xxxxxxxxxx
140
function inv(T) {
const det = T[0] * T[4] - T[1] * T[3];
return [
T[4] / det,
-T[1] / det,
(T[1] * T[5] - T[2] * T[4]) / det,
-T[3] / det,
T[0] / det,
(T[2] * T[3] - T[0] * T[5]) / det,
];
}
function area(pts) {
let total = 0.0;
let p = pts[pts.length - 1];
for (let idx = 0; idx < pts.length; ++idx) {
const q = pts[idx];
total += p.x * q.y - p.y * q.x;
p = q;
}
return abs(total / 2);
}
function getOutline( tiling ) {
outline = [];
function dofmul(T, p) {
const tp = mul(T, p);
return { x: tp.x, y: tp.y, dofs: p.dofs };
}
for (let i of tiling.parts()) {
const ej = edges[i.id];
let cur = i.rev ? ej.length - 2 : 1;
const inc = i.rev ? -1 : 1;
for (let idx = 0; idx < ej.length - 1; ++idx) {
outline.push(dofmul(i.T, ej[cur]));
cur += inc;
}
}
return outline;
}
let s = 1;
function makeTile() {
const tp = tilingTypes[int(random(tilingTypes.length))];
const tiling = new IsohedralTiling(tp);
// console.log( tp );
edges = [];
for (let idx = 0; idx < tiling.numEdgeShapes(); ++idx) {
const es = tiling.getEdgeShape(idx);
if (es == EdgeShape.U) {
edges.push([
{ x: 0, y: 0, dofs: 0 },
{ x : random(0.25,0.75), y: random(-0.3,0.3), dofs: 2 },
{ x: 1, y: random(-0.3,0.3), dofs: 2 },
]);
} else if (es == EdgeShape.S || es == EdgeShape.J ) {
edges.push([
{ x: 0, y: 0, dofs: 0 },
{ x : random(0.25,0.75), y: random(-0.3,0.3), dofs: 2 },
{ x: 1, y: 0, dofs: 2 },
]);
} else {
edges.push([
{ x: 0, y: 0, dofs: 0 },
{ x: 1, y: 0, dofs: 0 },
]);
}
}
return { tiling: tiling, edges: edges };
}
function makeSwatch( w, h )
{
const t = makeTile();
const pg = createGraphics( w, h );
outline = getOutline( t.tiling );
const ar = area(outline);
const num = random( 20,60 );
scaling = sqrt((w*h) / (num * ar));
pg.scale( scaling );
pg.strokeWeight( random(0.5,3) / scaling );
pg.stroke( color( 0, 0, random(200,256) ) );
cols = [];
for( let idx = 0; idx < 3; ++idx ) {
cols.push( color( random(255), 255, random(100,256) ) );
}
// TODO: the bounds could be computed based on the tiling.
// Meh.
for (let i of t.tiling.fillRegionBounds(-5, -5, 15, 15 ) ) {
const T = i.T;
const col = t.tiling.getColour(i.t1, i.t2, i.aspect);
pg.fill(cols[col]);
pg.beginShape();
for (let p of outline) {
const tp = mul(T, p);
if (p.dofs == 2) {
pg.curveVertex(tp.x, tp.y);
} else {
pg.vertex(tp.x, tp.y);
}
}
pg.endShape(CLOSE);
}
return pg;
}
function setup() {
createCanvas(500, 500);
background( 255 );
frameRate( 3 );
colorMode( HSB, 255 );
for( let y = 0; y < 5; ++y ) {
for( let x = 0; x < 5; ++x ) {
const swatch = makeSwatch( width/5, height/5 );
image( swatch, x*width/5, y*height/5 );
}
}
}
function draw() {
const swatch = makeSwatch( width/5, height/5 );
image( swatch, int(random(5))*width/5, int(random(5))*height/5 );
}