-
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
9 changed files
with
200 additions
and
47 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 was deleted.
Oops, something went wrong.
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
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,107 @@ | ||
import { toFixedPrecision } from '../math.js' | ||
import { Vector2 } from '../vector2.js' | ||
import { Rectangle } from './rectangle.js' | ||
import { Tag } from './tag.js' | ||
|
||
/** | ||
* @typedef {object} PolylineAttributes | ||
* @property {Vector2[]} [points=[]] | ||
*/ | ||
|
||
export class Polyline extends Tag { | ||
/** @type {Vector2[]} */ | ||
points = [] | ||
/** | ||
* Initialize to "empty" rectangle | ||
* @type {Rectangle} | ||
*/ | ||
#boundingBox = new Rectangle({ x: 0, y: 0, width: 0, height: 0 }) | ||
/** | ||
* @param {PolylineAttributes} attributes | ||
*/ | ||
constructor({ points = [], ...attributes } = { points: [] }) { | ||
super('polyline', attributes) | ||
this.points = points | ||
} | ||
|
||
/** @returns {Rectangle} */ | ||
get boundingBox() { | ||
if (this.#boundingBox.empty()) { | ||
const xs = this.points.map(({ x }) => x) | ||
const ys = this.points.map(({ y }) => y) | ||
const minX = Math.min(...xs) | ||
const maxX = Math.max(...xs) | ||
const minY = Math.min(...ys) | ||
const maxY = Math.max(...ys) | ||
this.#boundingBox = new Rectangle({ | ||
x: minX, | ||
y: minY, | ||
width: maxX - minX, | ||
height: maxY - minY, | ||
}) | ||
} | ||
return this.#boundingBox | ||
} | ||
|
||
render() { | ||
if (!Array.isArray(this.points) || this.points.length === 0) { | ||
throw new Error('Cannot render a Polyline without points') | ||
} | ||
this.setAttributes({ | ||
points: this.points | ||
.map((vec) => | ||
[ | ||
toFixedPrecision(vec.x, this.numericPrecision), | ||
toFixedPrecision(vec.y, this.numericPrecision), | ||
].join(','), | ||
) | ||
.join(' '), | ||
}) | ||
return super.render() | ||
} | ||
} | ||
|
||
/** | ||
* @overload | ||
* @param {PolylineAttributes} attrsOrBuilder | ||
* @returns {Polyline} | ||
*/ | ||
/** | ||
* @overload | ||
* @param {(Polyline: Polyline) => void} attrsOrBuilder | ||
* @returns {Polyline} | ||
*/ | ||
/** | ||
* @param {PolylineAttributes | ((Polyline: Polyline) => void)} attrsOrBuilder | ||
* @returns {Polyline} | ||
*/ | ||
export function polyline(attrsOrBuilder) { | ||
if (typeof attrsOrBuilder === 'function') { | ||
const poly = new Polyline() | ||
attrsOrBuilder(poly) | ||
return poly | ||
} | ||
return new Polyline(attrsOrBuilder) | ||
} | ||
|
||
// I would prefer this to live in it's own file but it creates a circular dependency. oh well. | ||
export class LineSegment extends Polyline { | ||
/** | ||
* @param {Vector2} start | ||
* @param {Vector2} end | ||
*/ | ||
constructor(start, end) { | ||
super({ | ||
points: [start, end], | ||
}) | ||
} | ||
} | ||
|
||
/** | ||
* @param {Vector2} start | ||
* @param {Vector2} end | ||
* @returns {LineSegment} | ||
*/ | ||
export function lineSegment(start, end) { | ||
return new LineSegment(start, end) | ||
} |
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,51 @@ | ||
import { describe, it } from 'node:test' | ||
import assert from 'node:assert' | ||
import { Polyline, lineSegment } from './polyline.js' | ||
import { vec2 } from '../vector2.js' | ||
|
||
describe('Polyline', () => { | ||
describe('render', () => { | ||
it('formats `points` correctly', () => { | ||
const poly = new Polyline({ points: [vec2(0, 0), vec2(100, 100)] }) | ||
const actual = poly.render() | ||
assert.strictEqual(actual, '<polyline points="0,0 100,100"></polyline>') | ||
}) | ||
|
||
it('includes other properties', () => { | ||
const poly = new Polyline({ | ||
points: [vec2(0, 0), vec2(100, 100)], | ||
fill: '#000', | ||
stroke: '#432', | ||
}) | ||
const actual = poly.render() | ||
assert.strictEqual( | ||
actual, | ||
'<polyline fill="#000" stroke="#432" points="0,0 100,100"></polyline>', | ||
) | ||
}) | ||
|
||
it('uses correct precision', () => { | ||
const poly = new Polyline({ | ||
points: [vec2(0.1234, 0.1234), vec2(100.1234, 100.1234)], | ||
}) | ||
poly.numericPrecision = 2 | ||
const actual = poly.render() | ||
assert.strictEqual( | ||
actual, | ||
'<polyline points="0.12,0.12 100.12,100.12"></polyline>', | ||
) | ||
}) | ||
|
||
it('throws if points is empty', () => { | ||
const poly = new Polyline() | ||
assert.throws(() => poly.render()) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('LineSegment', () => { | ||
it('renders a polyline tag', () => { | ||
const actual = lineSegment(vec2(0, 0), vec2(100, 100)).render() | ||
assert.strictEqual(actual, '<polyline points="0,0 100,100"></polyline>') | ||
}) | ||
}) |
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
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