diff --git a/src/App.tsx b/src/App.tsx index a0a3a7d..f76cecf 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -8,30 +8,10 @@ import Path from "./components/Path"; import { SettingsBox } from "./SettingsContext"; import { useCallback } from "react"; import { atom, useAtom, useSetAtom } from "jotai"; +import { defaultValues, parseCoord, parsePath } from "./lib/points"; // https://threejs.org/manual/#en/align-html-elements-to-3d -type coord = [number, number, number]; -const coords: coord[] = [ - [0, 0, 0], - [0, 0, 1], - [0, 1, 0], - [0, 1, 1], - [1, 0, 0], - [1, 0, 1], - [1, 1, 0], - [1, 1, 1], -]; - -const ppCoord = (c: coord): string => c.map((n) => n.toString()).join(""); - -const offset = (a: coord): coord => [a[0] - 0.5, a[1] - 0.5, a[2] - 0.5]; - -const offsetCoords: [string, coord][] = coords.map((a) => [ - ppCoord(a), - offset(a), -]); - function getInitialValue() { try { const h = location.hash; @@ -58,37 +38,6 @@ function AdjustCamera() { return <>; } -const paths: [string, [coord, coord]][] = offsetCoords - .flatMap((a) => offsetCoords.map((b) => [a, b])) - .filter( - ([[_a, [a1, a2, a3]], [_b, [b1, b2, b3]]]) => - [a1 === b1 ? 1 : 0, a2 === b2 ? 1 : 0, a3 === b3 ? 1 : 0].reduce( - (x, y) => x + y - ) === 2 && - a1 <= b1 && - a2 <= b2 && - a3 <= b3 - ) - .map(([[aname, acoord], [bname, bcoord]]) => [ - aname + bname, - [acoord, bcoord], - ]); - -const defaultValues: [string, string] = Object.fromEntries([ - ...offsetCoords.map(([a]) => [a, ""]), - ...paths.map(([a]) => [a, ""]), -]); - -function parseCoord(s: string): coord { - // @ts-ignore - const t: [number, number, number] = s.split("").map((n) => parseInt(n)); - return offset(t); -} - -function parsePath(s: string): [coord, coord] { - return [parseCoord(s.substring(0, 3)), parseCoord(s.substring(3, 6))]; -} - function App() { const [state] = useAtom(stateAtom); const updateStateFunc = useSetAtom(updateStateAtom); diff --git a/src/lib/points.ts b/src/lib/points.ts new file mode 100644 index 0000000..7a516c4 --- /dev/null +++ b/src/lib/points.ts @@ -0,0 +1,51 @@ +export type coord = [number, number, number]; +const coords: coord[] = [ + [0, 0, 0], + [0, 0, 1], + [0, 1, 0], + [0, 1, 1], + [1, 0, 0], + [1, 0, 1], + [1, 1, 0], + [1, 1, 1], +]; + +const ppCoord = (c: coord): string => c.map((n) => n.toString()).join(""); + +const offset = (a: coord): coord => [a[0] - 0.5, a[1] - 0.5, a[2] - 0.5]; + +const offsetCoords: [string, coord][] = coords.map((a) => [ + ppCoord(a), + offset(a), +]); + +const paths: [string, [coord, coord]][] = offsetCoords + .flatMap((a) => offsetCoords.map((b) => [a, b])) + .filter( + ([[_a, [a1, a2, a3]], [_b, [b1, b2, b3]]]) => + [a1 === b1 ? 1 : 0, a2 === b2 ? 1 : 0, a3 === b3 ? 1 : 0].reduce( + (x, y) => x + y + ) === 2 && + a1 <= b1 && + a2 <= b2 && + a3 <= b3 + ) + .map(([[aname, acoord], [bname, bcoord]]) => [ + aname + bname, + [acoord, bcoord], + ]); + +export const defaultValues: [string, string] = Object.fromEntries([ + ...offsetCoords.map(([a]) => [a, ""]), + ...paths.map(([a]) => [a, ""]), +]); + +export function parseCoord(s: string): coord { + // @ts-ignore + const t: [number, number, number] = s.split("").map((n) => parseInt(n)); + return offset(t); +} + +export function parsePath(s: string): [coord, coord] { + return [parseCoord(s.substring(0, 3)), parseCoord(s.substring(3, 6))]; +}