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;
}
export function smooth(vs) {
export function smooth(sorted) {
const smoothed = [];
// eslint-disable-next-line no-use-before-define
const sorted = ortho(unique(vs));
if (0 === sorted.length) {
return [];
}

View File

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

View File

@ -23,7 +23,7 @@ export default class TilesView extends Container {
this.tileset = tileset;
this.renderer = renderer;
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));
// if (mask) {
// this.addChild(mask);
@ -184,7 +184,7 @@ export default class TilesView extends Container {
renderMask(size) {
const {hulls} = this;
if (!hulls.length > 0) {
if (0 === hulls.length) {
return undefined;
}
const {tileSize} = this.tileset;
@ -192,9 +192,9 @@ export default class TilesView extends Container {
const canvas = window.document.createElement('canvas');
[canvas.width, canvas.height] = canvasSize;
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.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'rgba(255, 255, 255, 1)';
for (let i = 0; i < hulls.length; ++i) {
const scaled = [];
for (let j = 0; j < hulls[i].length; j++) {
@ -211,31 +211,24 @@ export default class TilesView extends Container {
(p0[0] > p1[0] && points[0][0] < points[points.length - 1][0])
|| (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 (
let k = (isReversed ? points.length - 1 : 0);
(isReversed ? k >= 0 : k < points.length);
k += (isReversed ? -1 : 1)
) {
const [x, y] = points[k];
const shift = Vector.add(
u,
Vector.scale(
Vector.mul(
[
noise(Vector.scale([x, y], 10)),
noise(Vector.scale([y, x], 10)),
],
[
noise(Vector.scale([x, y], 20)),
noise(Vector.scale([y, x], 20)),
],
),
5,
const shift = Vector.scale(
Vector.mul(
[
noise(Vector.scale([x, y], 10)),
noise(Vector.scale([y, x], 10)),
],
[
noise(Vector.scale([x, y], 20)),
noise(Vector.scale([y, x], 20)),
],
),
5,
);
const [vx, vy] = Vector.add(
points[k],