fix issue with function order

This commit is contained in:
Michael Zhang 2024-10-19 20:06:16 -05:00
parent 3b0926476f
commit 5c15d78e53
2 changed files with 52 additions and 52 deletions

View file

@ -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);

51
src/lib/points.ts Normal file
View file

@ -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))];
}