Files
pixel.horse/src/ts/common/rect.ts
T
Eliot Partridge caf70a864f Revert codestyle changes (#40)
* Revert "7f7efbb94bab8574e42d942ad82d414e007b2970"

Code style changes should probably be part of a PR
2019-08-30 10:53:14 -05:00

51 lines
1.3 KiB
TypeScript

import { Rect, Point } from './interfaces';
import { intersect } from './utils';
export function rect(x: number, y: number, w: number, h: number): Rect {
return { x, y, w, h };
}
export function centerPoint(rect: Rect): Point {
return { x: rect.x + rect.w / 2, y: rect.y + rect.h / 2 };
}
export function copyRect(dst: Rect, src: Rect) {
dst.x = src.x;
dst.y = src.y;
dst.w = src.w;
dst.h = src.h;
}
export function withBorder({ x, y, w, h }: Rect, border: number) {
return rect(x - border, y - border, w + border * 2, h + border * 2);
}
export function withPadding({ x, y, w, h }: Rect, top: number, right: number, bottom: number, left: number) {
return rect(x - top, y - left, w + left + right, h + top + bottom);
}
export function rectsIntersect(a: Rect, b: Rect): boolean {
return intersect(a.x, a.y, a.w, a.h, b.x, b.y, b.w, b.h);
}
export function addRect(a: Rect, b: Rect) {
const x = Math.min(a.x, b.x);
const y = Math.min(a.y, b.y);
a.w = Math.max(a.x + a.w, b.x + b.w) - x;
a.h = Math.max(a.y + a.h, b.y + b.h) - y;
a.x = x;
a.y = y;
}
export function addRects(a: Rect, b: Rect): Rect {
const x = Math.min(a.x, b.x);
const y = Math.min(a.y, b.y);
return {
x, y,
w: Math.max(a.x + a.w, b.x + b.w) - x,
h: Math.max(a.y + a.h, b.y + b.h) - y,
};
}