remove isClosed

This commit is contained in:
Orion Reed 2024-12-01 05:55:08 -05:00
parent 62454dcd8a
commit 0f3ee9fa3f
2 changed files with 12 additions and 14 deletions

View File

@ -121,10 +121,10 @@ export class DistanceField extends HTMLElement {
}; };
} }
addShape(points: Vector2[], isClosed = true) { addShape(points: Vector2[]) {
// Transform each point from screen coordinates to field coordinates // Transform each point from screen coordinates to field coordinates
const transformedPoints = points.map((point) => this.transformToFieldCoordinates(point)); const transformedPoints = points.map((point) => this.transformToFieldCoordinates(point));
this.fields.addShape(transformedPoints, isClosed); this.fields.addShape(transformedPoints);
this.renderDistanceField(); this.renderDistanceField();
} }
@ -176,16 +176,16 @@ export class DistanceField extends HTMLElement {
]; ];
if (index < this.fields.shapes.length) { if (index < this.fields.shapes.length) {
this.updateShape(index, points, true); this.updateShape(index, points);
} else { } else {
this.addShape(points, true); this.addShape(points);
} }
}; };
updateShape(index: number, points: Vector2[], isClosed = true) { updateShape(index: number, points: Vector2[]) {
// Transform each point from screen coordinates to field coordinates // Transform each point from screen coordinates to field coordinates
const transformedPoints = points.map((point) => this.transformToFieldCoordinates(point)); const transformedPoints = points.map((point) => this.transformToFieldCoordinates(point));
this.fields.updateShape(index, transformedPoints, isClosed); this.fields.updateShape(index, transformedPoints);
this.renderDistanceField(); this.renderDistanceField();
} }
} }

View File

@ -11,7 +11,6 @@ export class Fields {
shapes: Array<{ shapes: Array<{
points: Vector2[]; points: Vector2[];
color: number; color: number;
isClosed: boolean;
}> = []; }> = [];
constructor(resolution: number) { constructor(resolution: number) {
@ -40,9 +39,9 @@ export class Fields {
return this.colorField[x][y]; return this.colorField[x][y];
} }
addShape(points: Vector2[], isClosed: boolean = true) { addShape(points: Vector2[]) {
const color = Math.floor(Math.random() * 255); const color = Math.floor(Math.random() * 255);
this.shapes.push({ points, color, isClosed }); this.shapes.push({ points, color });
this.updateFields(); this.updateFields();
console.log(this.shapes); console.log(this.shapes);
} }
@ -129,10 +128,9 @@ export class Fields {
}; };
for (const shape of this.shapes) { for (const shape of this.shapes) {
const { points, color, isClosed } = shape; const { points, color } = shape;
const length = isClosed ? points.length : points.length - 1;
for (let i = 0; i < length; i++) { for (let i = 0; i < points.length; i++) {
const start = points[i]; const start = points[i];
const end = points[(i + 1) % points.length]; const end = points[(i + 1) % points.length];
drawLine(start, end, color); drawLine(start, end, color);
@ -240,10 +238,10 @@ export class Fields {
canvas.putImageData(imageData, 0, 0); canvas.putImageData(imageData, 0, 0);
} }
updateShape(index: number, points: Vector2[], isClosed: boolean = true) { updateShape(index: number, points: Vector2[]) {
if (index >= 0 && index < this.shapes.length) { if (index >= 0 && index < this.shapes.length) {
const existingColor = this.shapes[index].color; const existingColor = this.shapes[index].color;
this.shapes[index] = { points, color: existingColor, isClosed }; this.shapes[index] = { points, color: existingColor };
this.updateFields(); this.updateFields();
} }
} }