Clamp all objects/entrance to room bounds and draw entrance-to-object path flow in optimize mode
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import type { Entrance, FurnitureConfig, LayoutState, Rect, RoomConfig } from '../types';
|
||||
|
||||
export interface Bounds {
|
||||
x: number;
|
||||
y: number;
|
||||
width: number;
|
||||
height: number;
|
||||
}
|
||||
|
||||
export function clampRectToRoom(rect: Bounds, room: RoomConfig, minSize = 10): Bounds {
|
||||
const width = Math.min(Math.max(minSize, rect.width), room.width);
|
||||
const height = Math.min(Math.max(minSize, rect.height), room.height);
|
||||
const x = Math.min(Math.max(0, rect.x), Math.max(0, room.width - width));
|
||||
const y = Math.min(Math.max(0, rect.y), Math.max(0, room.height - height));
|
||||
return { x, y, width, height };
|
||||
}
|
||||
|
||||
export function clampStateToRoom(state: LayoutState): LayoutState {
|
||||
const room = state.room;
|
||||
const furniture: FurnitureConfig[] = state.furniture.map((f) => {
|
||||
const b = clampRectToRoom(
|
||||
{ x: f.shape.x, y: f.shape.y, width: f.shape.width, height: f.shape.height },
|
||||
room,
|
||||
);
|
||||
return { ...f, shape: { ...f.shape, ...b } };
|
||||
});
|
||||
let entrance: Entrance | null = state.entrance;
|
||||
if (entrance) {
|
||||
const ev = clampRectToRoom(
|
||||
{ x: entrance.x, y: entrance.y, width: entrance.width ?? 20, height: entrance.height ?? 20 },
|
||||
room,
|
||||
10,
|
||||
);
|
||||
entrance = { ...entrance, x: ev.x, y: ev.y, width: ev.width, height: ev.height };
|
||||
}
|
||||
return { ...state, furniture, entrance };
|
||||
}
|
||||
|
||||
export function rectsOverlap(a: Rect, b: Rect): boolean {
|
||||
return !(a.x + a.width <= b.x || b.x + b.width <= a.x || a.y + a.height <= b.y || b.y + b.height <= a.y);
|
||||
}
|
||||
Reference in New Issue
Block a user