blob: dd594a4280a443da65a031f571b4319cd9079db3 (
plain)
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
|
import Coord from "../../math/Coord";
import { CellRef } from "../../types";
import { RenderedGrid, RenderedRow } from "./renderGrid";
function rowAtCoord(grid: RenderedGrid, coord: Coord): RenderedRow | undefined {
if (coord.y <= grid.rect.topLeft.y) {
return grid.renderedRows[0];
}
if (coord.y >= grid.rect.bottomRight.y) {
return grid.renderedRows.at(-1);
}
return grid.renderedRows.find((row) =>
row.rect.verticallyContainsCoord(coord),
);
}
export default function cellAtCoord(
grid: RenderedGrid,
x: number,
y: number,
): CellRef | undefined {
const coord = new Coord(x, y);
const row = rowAtCoord(grid, coord);
if (!row) return;
if (x <= row.rect.topLeft.x) {
return row.renderedCells[0]?.cellRef;
}
if (x >= row.rect.bottomRight.x) {
return row.renderedCells.at(-1)?.cellRef;
}
return row.renderedCells.find((cell) =>
cell.rect.horizontallyContainsCoord(coord),
)?.cellRef;
}
|