Skip to content

Commit

Permalink
refactor: add point hashing function, offsetNeighbors
Browse files Browse the repository at this point in the history
  • Loading branch information
andretchen0 committed Aug 3, 2023
1 parent ba65fa3 commit 227f62b
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/utility/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,42 @@ import { Point } from './pointfacade';

export const HEX_WIDTH_PX = 90;
export const HEX_HEIGHT_PX = (HEX_WIDTH_PX / Math.sqrt(3)) * 2 * 0.75;

export function offsetCoordsToPx(point: Point) {
return {
x: (point.y % 2 === 0 ? point.x + 0.5 : point.x) * HEX_WIDTH_PX,
y: point.y * HEX_HEIGHT_PX,
};
}

const n2_16 = Math.pow(2, 16);

function isValid(point: Point) {
return 0 <= point.x && point.x < n2_16 && 0 <= point.y && point.y < n2_16;
}

export function offsetNeighbors(point: Point): Point[] {
if (point.y % 2 === 0) {
return [
{ x: point.x + 1, y: point.y },
{ x: point.x + 1, y: point.y + 1 },
{ x: point.x, y: point.y + 1 },
{ x: point.x - 1, y: point.y },
{ x: point.x, y: point.y - 1 },
{ x: point.x + 1, y: point.y - 1 },
].filter(isValid);
} else {
return [
{ x: point.x + 1, y: point.y },
{ x: point.x, y: point.y + 1 },
{ x: point.x - 1, y: point.y + 1 },
{ x: point.x - 1, y: point.y },
{ x: point.x - 1, y: point.y - 1 },
{ x: point.x, y: point.y - 1 },
].filter(isValid);
}
}

export function hashOffsetCoords(point: Point) {
return (point.x << 16) ^ point.y;
}

0 comments on commit 227f62b

Please sign in to comment.