-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
126 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Vector2 } from '../vector2.js' | ||
import { Rectangle } from './rectangle.js' | ||
import { Tag } from './tag.js' | ||
|
||
/** | ||
* @typedef {object} PolygonAttributes | ||
* @property {Vector2[]} points | ||
*/ | ||
|
||
export class Polygon extends Tag { | ||
/** | ||
* Initialize to "empty" rectangle | ||
* @type {Rectangle} | ||
*/ | ||
#boundingBox = new Rectangle({x: 0, y: 0, width: 0, height: 0}) | ||
/** | ||
* @param {PolygonAttributes} attributes | ||
*/ | ||
constructor(attributes = { points: [] }) { | ||
if (!Array.isArray(attributes.points) || attributes.points.length === 0) { | ||
// Maybe this will change in the future if I make a builder | ||
throw new Error('Cannot construct a Polygon without points') | ||
} | ||
super('polygon', attributes) | ||
} | ||
|
||
/** @returns {Rectangle} */ | ||
get boundingBox() { | ||
if (this.#boundingBox.empty()) { | ||
/** @type {Vector2[]} */ | ||
// @ts-expect-error JSDoc doesn't allow casting I guess | ||
const points = this.attributes.points | ||
const minX = Math.min(...points.map(({x}) => x)) | ||
const maxX = Math.max(...points.map(({x}) => x)) | ||
const minY = Math.min(...points.map(({y}) => y)) | ||
const maxY = Math.max(...points.map(({y}) => y)) | ||
this.#boundingBox = new Rectangle({ x: minX, y: minY, width: maxX - minX, height: maxY - minY}) | ||
} | ||
return this.#boundingBox | ||
} | ||
|
||
#formatAttributes() { | ||
return Object.entries(this.attributes) | ||
.map(([key, value]) => | ||
key === 'points' | ||
? // @ts-expect-error TS doesn't know that we're operating on the points property, | ||
// which is of type Vector2[] | ||
`${key}="${value.map((vec) => `${vec.x},${vec.y}`).join(' ')}"` | ||
: `${key}="${value}"`, | ||
) | ||
.join(' ') | ||
} | ||
|
||
/** | ||
* @returns {string} | ||
*/ | ||
render() { | ||
// unfortunately requires it's own render method because of the `points` format. | ||
// This probably deserves a more nuanced treatment by the Tag class, we'll see. | ||
return [ | ||
`<${this.tagName} ${this.#formatAttributes()}>`, | ||
this.children.map((child) => child.render()).join(''), | ||
`</${this.tagName}>`, | ||
].join('') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { describe, it } from 'node:test' | ||
import { Polygon } from './polygon.js' | ||
import assert from 'node:assert' | ||
import { vec2 } from '../vector2.js' | ||
|
||
describe('Polygon', () => { | ||
describe('render', () => { | ||
it('formats `points` correctly', () => { | ||
const poly = new Polygon({ points: [vec2(0, 0), vec2(100, 100)] }) | ||
const actual = poly.render() | ||
assert.strictEqual(actual, '<polygon points="0,0 100,100"></polygon>') | ||
}) | ||
|
||
it('includes other properties', () => { | ||
const poly = new Polygon({ | ||
points: [vec2(0, 0), vec2(100, 100)], | ||
fill: '#000', | ||
stroke: '#432', | ||
}) | ||
const actual = poly.render() | ||
assert.strictEqual( | ||
actual, | ||
'<polygon points="0,0 100,100" fill="#000" stroke="#432"></polygon>', | ||
) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters