Skip to content

Commit

Permalink
Adjustable precision
Browse files Browse the repository at this point in the history
  • Loading branch information
MuTsunTsai committed May 6, 2024
1 parent b73a6e2 commit 11b5b8c
Show file tree
Hide file tree
Showing 19 changed files with 51 additions and 29 deletions.
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!doctype html><html lang="en"><head><title>ReferenceFinder</title><meta http-equiv="content-type" content="text/html; charset=UTF-8"><meta name="viewport" content="initial-scale=1,maximum-scale=5,width=device-width"><meta name="description" content="Find reference points and lines in crease patterns."><link rel="icon" href="/reference-finder/favicon.ico"/><!-- Global site tag (gtag.js) - Google Analytics --><script async src="https://www.googletagmanager.com/gtag/js?id=G-GG1TEZGBCQ"></script><script>function gtag(){dataLayer.push(arguments)}window.dataLayer=window.dataLayer||[],gtag("js",new Date),"localhost"!=location.hostname&&gtag("config","G-GG1TEZGBCQ",{page_title:document.title,page_path:"/reference-finder/"});const settings=localStorage.getItem("settings");settings&&0!=JSON.parse(settings).state.theme||!matchMedia("(prefers-color-scheme: dark)").matches||(document.documentElement.dataset.bsTheme="dark"),"serviceWorker"in navigator&&"31213"!=location.port&&navigator.serviceWorker.register("/reference-finder/service-worker.js",{scope:"/reference-finder/"})</script><meta charset="UTF-8"><script defer="defer" src="/reference-finder/static/js/react.7f3e94f4.js"></script><script defer="defer" src="/reference-finder/static/js/locale.44053c9c.js"></script><script defer="defer" src="/reference-finder/static/js/rabbit-ear.16b592a7.js"></script><script defer="defer" src="/reference-finder/static/js/vendor.f5dfc1db.js"></script><script defer="defer" src="/reference-finder/static/js/index.e76ef1a3.js"></script><link href="/reference-finder/static/font/fa-solid-900.2584ff1a.woff2" rel="preload" as="font" crossorigin=""><link href="/reference-finder/static/css/index.fc316719.css" rel="stylesheet"></head><body><noscript class="p-3">You need to enable JavaScript to run this app.</noscript><div id="root" class="d-flex flex-column h-100"></div></body></html>
<!doctype html><html lang="en"><head><title>ReferenceFinder</title><meta http-equiv="content-type" content="text/html; charset=UTF-8"><meta name="viewport" content="initial-scale=1,maximum-scale=5,width=device-width"><meta name="description" content="Find reference points and lines in crease patterns."><link rel="icon" href="/reference-finder/favicon.ico"/><!-- Global site tag (gtag.js) - Google Analytics --><script async src="https://www.googletagmanager.com/gtag/js?id=G-GG1TEZGBCQ"></script><script>function gtag(){dataLayer.push(arguments)}window.dataLayer=window.dataLayer||[],gtag("js",new Date),"localhost"!=location.hostname&&gtag("config","G-GG1TEZGBCQ",{page_title:document.title,page_path:"/reference-finder/"});const settings=localStorage.getItem("settings");settings&&0!=JSON.parse(settings).state.theme||!matchMedia("(prefers-color-scheme: dark)").matches||(document.documentElement.dataset.bsTheme="dark"),"serviceWorker"in navigator&&"31213"!=location.port&&navigator.serviceWorker.register("/reference-finder/service-worker.js",{scope:"/reference-finder/"})</script><meta charset="UTF-8"><script defer="defer" src="/reference-finder/static/js/locale.ba8a96ac.js"></script><script defer="defer" src="/reference-finder/static/js/rabbit-ear.16b592a7.js"></script><script defer="defer" src="/reference-finder/static/js/react.cdaeb661.js"></script><script defer="defer" src="/reference-finder/static/js/vendor.f5dfc1db.js"></script><script defer="defer" src="/reference-finder/static/js/index.a75b7e91.js"></script><link href="/reference-finder/static/font/fa-solid-900.2584ff1a.woff2" rel="preload" as="font" crossorigin=""><link href="/reference-finder/static/css/index.fc316719.css" rel="stylesheet"></head><body><noscript class="p-3">You need to enable JavaScript to run this app.</noscript><div id="root" class="d-flex flex-column h-100"></div></body></html>
2 changes: 1 addition & 1 deletion docs/service-worker.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/static/js/index.a75b7e91.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion docs/static/js/index.e76ef1a3.js

This file was deleted.

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion docs/static/js/react.7f3e94f4.js

This file was deleted.

1 change: 1 addition & 0 deletions docs/static/js/react.cdaeb661.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ function App() {
<button type="button" onClick={() => setSol(i)}
className={"w-100 p-1 btn btn-ld col solution-preview " + (sol == i ? "active" : "")}>
<Diagram data={s.diagrams[s.diagrams.length - 1]} />
<div>{formatSolution(s)}</div>
<div>{t("phrase.error")} {s.err.toFixed(4)}</div>
<div>{formatSolution(s, settings.precision)}</div>
<div>{t("phrase.error")} {s.err.toFixed(settings.precision)}</div>
<div>rank {s.rank}</div>
</button>
</div>
Expand Down
4 changes: 3 additions & 1 deletion src/app/components/form/exp-input.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { parse } from "../../parser";
import { useSettings } from "../../store";

import "./exp-input.scss";

Expand All @@ -27,13 +28,14 @@ function getTextWidth(input: HTMLInputElement, text: string): number {
export function ExpInput({ max, value, exp, onInput }: ExpInputProps) {
const { t } = useTranslation();
const el = useRef<HTMLInputElement>(null);
const settings = useSettings();
const suffix = useRef<HTMLSpanElement>(null);
const [temp, setTemp] = useState(exp ?? value.toString());
const [tempValue, setTempValue] = useState(value);
const [cache, setCache] = useState(exp ?? value.toString());

function format(v: number): string {
return v.toFixed(4).replace(/0+$/, "").replace(/\.$/, "").replace(/^$/, "0");
return v.toFixed(settings.precision).replace(/0+$/, "").replace(/\.$/, "").replace(/^$/, "0");
}

function isValid(v = tempValue): boolean {
Expand Down
18 changes: 11 additions & 7 deletions src/app/components/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { Theme, defaultDB, defaultSettings, useDB, useSettings } from "../store"
import { resetWorker } from "../worker";
import { SettingsRow } from "./form/settings-row";
import { Checkbox } from "./form/checkbox";
import { InfoTooltip } from "./tooltip";

import "./settings.scss";
import { InfoTooltip } from "./tooltip";

export function Settings() {
const { t } = useTranslation();
Expand Down Expand Up @@ -124,12 +124,12 @@ export function Settings() {
</SettingsRow>
<div className="mt-n2" style={{ gridColumn: "1/3" }}><hr /></div>
<SettingsRow label="Line error mode" help={t("help.lineError")}>
<select className="form-select" value={settings.worstCaseError ? "1" : ""}
onChange={e => useSettings.setState({ worstCaseError: Boolean(e.currentTarget.value) })}>
<option value="1">Worst-case error</option>
<option value="">Pythagorean error</option>
</select>
</SettingsRow>
<select className="form-select" value={settings.worstCaseError ? "1" : ""}
onChange={e => useSettings.setState({ worstCaseError: Boolean(e.currentTarget.value) })}>
<option value="1">Worst-case error</option>
<option value="">Pythagorean error</option>
</select>
</SettingsRow>
<SettingsRow label="Good enough error" help={t("help.goodEnoughError")}>
<ExpInput value={settings.error} exp={settings.errorExp}
onInput={(v, exp) => useSettings.setState({ error: v, errorExp: exp })} />
Expand All @@ -150,6 +150,10 @@ export function Settings() {
<option value={Theme.rabbitEar}>RabbitEar</option>
</select>
</SettingsRow>
<SettingsRow label={t("settings.display.precision")}>
<IntInput min={1} max={6} value={settings.precision}
onInput={v => useSettings.setState({ precision: v })} />
</SettingsRow>
</div>
<div className="mt-3 mb-1">
<Checkbox value={settings.showInit}
Expand Down
24 changes: 16 additions & 8 deletions src/app/components/solution.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useTranslation } from "react-i18next";
import { Solution } from "../store";
import { Solution, useSettings } from "../store";
import { StepComponent } from "./step";
import { Diagram } from "./svg/diagram";
import { useRef } from "react";
Expand All @@ -10,29 +10,37 @@ interface SolutionComponentProps {
onSelect: () => void;
}

function getDegree(p: IPoint): string {
/**
* Calculates the degree using the normal vector of a line.
*/
function getDegree(p: IPoint, precision: number): string {
let result = Math.atan2(-p[0], p[1]) / Math.PI * 180;
if(result <= -90) result += 180;
if(result > 90) result -= 180;
return result.toFixed(4) + "°";
return result.toFixed(precision) + "°";
}

export function formatSolution(data: Solution): string {
/**
* Original ReferenceFinder represents a line as (distance to origin, normal unit vector),
* and I think using (distance to origin, degree of the line) is more intuitive.
*/
export function formatSolution(data: Solution, precision: number): string {
const item = data.solution[1];
const text = typeof item == "number" ? item.toFixed(4) : getDegree(item);
return `(${data.solution[0].toFixed(4)}, ${text})`;
const text = typeof item == "number" ? item.toFixed(precision) : getDegree(item, precision);
return `(${data.solution[0].toFixed(precision)}, ${text})`;
}

export function SolutionComponent({ data, show, onSelect }: SolutionComponentProps) {
const { t } = useTranslation();
const settings = useSettings();
const ref = useRef<HTMLDivElement>(null);
const handleSelect = () => {
onSelect();
setTimeout(() => ref.current?.scrollIntoView(), 0);
}
const solution = formatSolution(data);
const solution = formatSolution(data, settings.precision);

const err = data.err.toFixed(4);
const err = data.err.toFixed(settings.precision);

return (
<div className={"card mt-3 " + (show ? "" : "d-sm-none")} style={{ overflow: "hidden" }}>
Expand Down
1 change: 1 addition & 0 deletions src/app/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const defaultSettings = {
errorExp: "1/200",
worstCaseError: true,
count: 5,
precision: 4,
};

export interface InitInfo {
Expand Down
1 change: 1 addition & 0 deletions src/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
},
"showAxiom": "Show axiom number in description.",
"showInit": "Show initialization progress.",
"precision": "Precision",
"_": "Display"
}
},
Expand Down
3 changes: 2 additions & 1 deletion src/locale/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"_": "Tema"
},
"_": "Mostrar",
"showInit": "Mostrar el progreso de la inicialización."
"showInit": "Mostrar el progreso de la inicialización.",
"precision": "Precisión"
},
"advanced": {
"_": "Avanzado"
Expand Down
3 changes: 2 additions & 1 deletion src/locale/jp.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"_": "テーマ"
},
"_": "表示",
"showInit": "初期化の進行状況を表示します。"
"showInit": "初期化の進行状況を表示します。",
"precision": "精度"
},
"advanced": {
"_": "高度"
Expand Down
3 changes: 2 additions & 1 deletion src/locale/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"_": "Tema"
},
"_": "Mostrar",
"showInit": "Mostrar o progresso da inicialização."
"showInit": "Mostrar o progresso da inicialização.",
"precision": "Precisão"
},
"advanced": {
"_": "Avançado"
Expand Down
3 changes: 2 additions & 1 deletion src/locale/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"_": "Тема"
},
"_": "Отображать",
"showInit": "Показать ход инициализации."
"showInit": "Показать ход инициализации.",
"precision": "Точность"
},
"advanced": {
"_": "Передовой"
Expand Down
3 changes: 2 additions & 1 deletion src/locale/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"_": "主题"
},
"_": "显示",
"showInit": "显示初始化进度。"
"showInit": "显示初始化进度。",
"precision": "精确度"
},
"advanced": {
"_": "进阶"
Expand Down
3 changes: 2 additions & 1 deletion src/locale/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"_": "主題"
},
"_": "顯示",
"showInit": "顯示初始化進度。"
"showInit": "顯示初始化進度。",
"precision": "精確度"
},
"advanced": {
"_": "進階"
Expand Down

0 comments on commit 11b5b8c

Please sign in to comment.