refactor: hulls

This commit is contained in:
cha0s 2021-03-28 11:23:17 -05:00
parent 3e7233ec46
commit 909b1215a2
3 changed files with 26 additions and 46 deletions

View File

@ -191,10 +191,8 @@ export function removeCollinear(vs) {
return trimmed; return trimmed;
} }
export function smooth(vs) { export function smooth(sorted) {
const smoothed = []; const smoothed = [];
// eslint-disable-next-line no-use-before-define
const sorted = ortho(unique(vs));
if (0 === sorted.length) { if (0 === sorted.length) {
return []; return [];
} }

View File

@ -66,29 +66,18 @@ export default (latus) => {
const xx = k % w; const xx = k % w;
const yy = Math.floor(k / w); const yy = Math.floor(k / w);
const [xxs, yys] = [xx * 4, yy * 4]; const [xxs, yys] = [xx * 4, yy * 4];
const u = k - w; body.push([xxs + 1, yys + 1]);
if ((u < 0 || yy < 1) || !indices.has(this.data[u])) { body.push([xxs + 3, yys + 1]);
body.push([xxs, yys]); body.push([xxs + 3, yys + 3]);
body.push([xxs + 4, yys]); body.push([xxs + 1, yys + 3]);
}
const r = k + 1;
if ((r < 0 || xx + 1 >= w) || !indices.has(this.data[r])) {
body.push([xxs + 4, yys]);
body.push([xxs + 4, yys + 4]);
}
const d = k + w;
if ((d < 0 || yy + 1 >= h) || !indices.has(this.data[d])) {
body.push([xxs + 4, yys + 4]);
body.push([xxs, yys + 4]);
}
const l = k - 1;
if ((l < 0 || xx < 1) || !indices.has(this.data[l])) {
body.push([xxs, yys + 4]);
body.push([xxs, yys]);
} }
} }
} const hull = Vertice.removeCollinear(
const hull = Vertice.removeCollinear(Vertice.ortho(Vertice.unique(body), 4)); Vertice.ortho(
Vertice.unique(body),
2,
),
);
for (let i = 0; i < hull.length; i++) { for (let i = 0; i < hull.length; i++) {
hull[i] = Vector.scale(hull[i], 0.25); hull[i] = Vector.scale(hull[i], 0.25);
} }

View File

@ -23,7 +23,7 @@ export default class TilesView extends Container {
this.tileset = tileset; this.tileset = tileset;
this.renderer = renderer; this.renderer = renderer;
this.rendered = []; this.rendered = [];
// this.hulls = this.tiles.indexHulls(0); // this.hulls = this.tiles.indexHulls(new Set([342, 456, 103, 104]));
// const mask = this.renderMask(Vector.mul(this.tiles.size, this.tileset.tileSize)); // const mask = this.renderMask(Vector.mul(this.tiles.size, this.tileset.tileSize));
// if (mask) { // if (mask) {
// this.addChild(mask); // this.addChild(mask);
@ -184,7 +184,7 @@ export default class TilesView extends Container {
renderMask(size) { renderMask(size) {
const {hulls} = this; const {hulls} = this;
if (!hulls.length > 0) { if (0 === hulls.length) {
return undefined; return undefined;
} }
const {tileSize} = this.tileset; const {tileSize} = this.tileset;
@ -192,9 +192,9 @@ export default class TilesView extends Container {
const canvas = window.document.createElement('canvas'); const canvas = window.document.createElement('canvas');
[canvas.width, canvas.height] = canvasSize; [canvas.width, canvas.height] = canvasSize;
const ctx = canvas.getContext('2d'); const ctx = canvas.getContext('2d');
ctx.fillStyle = 'rgba(255, 255, 255, 1)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'rgba(0, 0, 0, 1)'; ctx.fillStyle = 'rgba(0, 0, 0, 1)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'rgba(255, 255, 255, 1)';
for (let i = 0; i < hulls.length; ++i) { for (let i = 0; i < hulls.length; ++i) {
const scaled = []; const scaled = [];
for (let j = 0; j < hulls[i].length; j++) { for (let j = 0; j < hulls[i].length; j++) {
@ -211,19 +211,13 @@ export default class TilesView extends Container {
(p0[0] > p1[0] && points[0][0] < points[points.length - 1][0]) (p0[0] > p1[0] && points[0][0] < points[points.length - 1][0])
|| (p0[1] > p1[1] && points[0][1] < points[points.length - 1][1]) || (p0[1] > p1[1] && points[0][1] < points[points.length - 1][1])
); );
const u = Vector.scale(
Vector.fromRadians((Math.PI * 0.5) + Vector.toRadians(Vector.sub(p1, p0))),
2,
);
for ( for (
let k = (isReversed ? points.length - 1 : 0); let k = (isReversed ? points.length - 1 : 0);
(isReversed ? k >= 0 : k < points.length); (isReversed ? k >= 0 : k < points.length);
k += (isReversed ? -1 : 1) k += (isReversed ? -1 : 1)
) { ) {
const [x, y] = points[k]; const [x, y] = points[k];
const shift = Vector.add( const shift = Vector.scale(
u,
Vector.scale(
Vector.mul( Vector.mul(
[ [
noise(Vector.scale([x, y], 10)), noise(Vector.scale([x, y], 10)),
@ -235,7 +229,6 @@ export default class TilesView extends Container {
], ],
), ),
5, 5,
),
); );
const [vx, vy] = Vector.add( const [vx, vy] = Vector.add(
points[k], points[k],