All files / src/geometry Rectangle.ts

100% Statements 39/39
100% Branches 6/6
100% Functions 5/5
100% Lines 39/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 491x 1x 1x   1x 20x 20x 20x   20x 20x 20x 20x 20x 20x   20x 11x 11x   20x 5x   5x 5x 5x 5x 5x 5x 5x 5x   20x 9x 9x   9x 9x   9x 9x   9x 9x 9x 9x 9x 20x  
import { Line } from './Line.js';
import { Point } from './Point.js';
import { Shape } from './Shape.js';
 
export class Rectangle extends Shape {
	public from: Point;
	public width: number;
	public height: number;
 
	constructor(from: Point, width: number, height: number) {
		super();
		this.from = from;
		this.width = width;
		this.height = height;
	}
 
	public get to(): Point {
		return new Point(this.from.x + this.width, this.from.y + this.height);
	}
 
	public getLines(): Line[] {
		const to = this.to;
 
		const lines: Array<Line> = [
			new Line(this.from, new Point(to.x, this.from.y)),
			new Line(this.from, new Point(this.from.x, to.y)),
			new Line(new Point(to.x, this.from.y), to),
			new Line(new Point(this.from.x, to.y), to),
		];
		return lines.filter((l) => l.valid);
	}
 
	public transform(matrix: Array<number>): this {
		const p1 = Shape.applyTransform([this.from.x, this.from.y], matrix);
		const p2 = Shape.applyTransform([this.from.x + this.width, this.from.y + this.height], matrix);
 
		const x = Math.min(p1[0], p2[0]);
		const y = Math.min(p1[1], p2[1]);
 
		const width = Math.abs(p1[0] - p2[0]);
		const height = Math.abs(p1[1] - p2[1]);
 
		this.from = new Point(x, y);
		this.width = width;
		this.height = height;
		return this;
	}
}