mirror of
https://github.com/Terncode/pixel.horse.git
synced 2026-09-24 13:55:04 +02:00
* Revert "7f7efbb94bab8574e42d942ad82d414e007b2970" Code style changes should probably be part of a PR
51 lines
1.3 KiB
TypeScript
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,
|
|
};
|
|
}
|