From 17c91429f42e1143fda68fff41d671857dbc7d08 Mon Sep 17 00:00:00 2001 From: Oscar Giacalone Date: Sat, 3 Oct 2026 19:59:12 +0100 Subject: [PATCH] cleaned up code; added some more comments --- src/main.js | 26 ++++++++++---------------- src/maths.js | 8 ++------ src/print.js | 9 --------- 3 files changed, 12 insertions(+), 31 deletions(-) diff --git a/src/main.js b/src/main.js index 02b2706..50bb0a5 100644 --- a/src/main.js +++ b/src/main.js @@ -8,25 +8,23 @@ let rsInput = document.getElementById("rs"); let npInput = document.getElementById("np"); let outputdiv = document.getElementById("output"); let stepInput = document.getElementById("steps"); -let lenOutput = document.getElementById("len"); let paperInput = document.getElementById("p"); +// parameters let height = 0; let radius = 0; let spillRadius = 0; let numberOfPanels = 0; - -let multiplier = 1; - let step = 0.001 * multiplier; -// const offset = 3*10**-15 * multiplier; const offset = 4 * 10 ** -5 * multiplier; +// temporary data let pointsP = []; let pointsQ = []; let distances = []; - let panelPoints = []; + +// print data let panelPaths = []; let svgSize = { width: 0, height: 0 }; let scaleFactor = 1; @@ -34,7 +32,6 @@ let paperSize = "a3"; let n = 0; let unit = "px"; -let strokeWidth = 1; // logic function getValues() { @@ -42,22 +39,22 @@ function getValues() { pointsQ = []; distances = []; panelPoints = []; - height = /* /* Math.floor */(parseFloat(hInput.value) * multiplier); - radius = /* Math.floor */(parseFloat(rInput.value) * multiplier); - spillRadius = /* Math.floor */(parseFloat(rsInput.value) * multiplier); - numberOfPanels = /* Math.floor */(parseFloat(npInput.value) * multiplier); + height = (parseFloat(hInput.value) * multiplier); + radius = (parseFloat(rInput.value) * multiplier); + spillRadius = (parseFloat(rsInput.value) * multiplier); + numberOfPanels = (parseFloat(npInput.value) * multiplier); step = radius / parseInt(stepInput.value); paperSize = paperInput.value; if (isNaN(spillRadius)) { - spillRadius = /* Math.floor */(0.05 * radius); + spillRadius = (0.05 * radius); rsInput.value = 0.05 * radius / multiplier; } n = ((radius - spillRadius) / (step - offset)) - 1; - console.log(`${height}, ${radius}, ${spillRadius}, ${step}, ${offset}`); } function getPoints() { for (let x = spillRadius; x < radius; x += step - offset) { + // round x to reduce oppourtunity for a NaN to occur from accuracy x = maths.approx(x); pointsP.push({ x: x, @@ -68,7 +65,6 @@ function getPoints() { function getPointsOfTangentIntersection() { for (let i = 0; i < n; i++) { - console.log(`${i}, ${pointsP[i].x} ${pointsP[i].y}, ${pointsP[i + 1].x} ${pointsP[i+1].y}`); pointsQ.push({ x: maths.j(pointsP[i], pointsP[i + 1], height, radius), y: maths.g(maths.j(pointsP[i], pointsP[i + 1], height, radius), pointsP[i + 1], height, radius) @@ -108,7 +104,6 @@ function createPerimeterPath(maxY) { path +=`${maths.approx(point.x)},${maths.approx((point.y + maxY))} `; } path += `${maths.approx(panelPoints[0].x)},${maths.approx((maxY) - panelPoints[0].y)}`; - console.log(path); return path; } @@ -158,7 +153,6 @@ function plotSVG() { /> `; - lenOutput.innerText = `Recommended length: ${maths.approx(maxX - minX)} ${unit}`; } function calculateScaleFactor(unit) { diff --git a/src/maths.js b/src/maths.js index 7d1084f..e08d82c 100644 --- a/src/maths.js +++ b/src/maths.js @@ -1,6 +1,7 @@ import { sanitiseArguments } from "./utils"; -let accuracy = import.meta.env.VITE_ACCURACY; +// decimal places +let accuracy = 8; export function approx(n) { return Math.round(n * (10 ** accuracy)) / (10 ** accuracy); @@ -13,7 +14,6 @@ export function approx(n) { export function f(x, h, r) { sanitiseArguments(x,h,r) let res = approx(h * Math.sqrt((1 - (Math.pow(x, 2) / Math.pow(r, 2))))); - console.log(`f(${x} ${h} ${r}) ${res}`); return res; } @@ -24,7 +24,6 @@ export function f(x, h, r) { export function fprime(x, h, r) { sanitiseArguments(x,h,r) let res = approx(-1 * ((Math.pow(h, 2) * x) / (Math.pow(r, 2) * f(x, h, r)))); - console.log(`fprime (${x} ${h} ${r}) ${res}`); return res; } @@ -36,7 +35,6 @@ export function fprime(x, h, r) { export function g(x, P, h, r) { sanitiseArguments(x,P.x, P.y,h,r) let res = approx(fprime(x, h, r) * (x - P.x) + P.y); - console.log(`g (${x} {${P.x} ${P.y}} ${h} ${r}) ${res}`); return res; } @@ -48,7 +46,6 @@ export function g(x, P, h, r) { export function j(A, B, h, r) { sanitiseArguments(A.x, A.y, B.x, B.y, h,r) let res = approx(A.x * fprime(A.x, h, r) - B.x * fprime(B.x, h, r) - A.y + B.y) / (fprime(A.x, h, r) - fprime(B.x, h, r)); - console.log(`j ({${A.x} ${A.y}} {${B.x} ${B.y}} ${h} ${r}) ${res}`); return res; } @@ -58,6 +55,5 @@ export function j(A, B, h, r) { export function d(A, B) { sanitiseArguments(A.x, A.y, B.x, B.y) let res = approx(Math.sqrt(Math.pow(A.x - B.x, 2) + Math.pow(A.y - B.y, 2))); - console.log(`d ({${A.x} ${A.y}} {${B.x} ${B.y}}) ${res}`); return res; } diff --git a/src/print.js b/src/print.js index 21b3bb5..a9674e2 100644 --- a/src/print.js +++ b/src/print.js @@ -6,8 +6,6 @@ export async function createPdf(paths, svgSize, scaleFactor, paperSize) { // calculate number of pages needed const margins = { x: 15, y: 15 }; - console.log(svgSize); - console.log(`Scale factor ${scaleFactor}`); // paper sizes in pt let sizes = { @@ -22,7 +20,6 @@ export async function createPdf(paths, svgSize, scaleFactor, paperSize) { } let nPages = Math.ceil((svgSize.width *scaleFactor) / sizes[paperSize].y); - console.log(`${svgSize.width} * ${scaleFactor} / ${sizes[paperSize].y} = ${((svgSize.width *scaleFactor) / sizes[paperSize].y)}`); let needsMarkers = false; if (1 < nPages) { needsMarkers = true; @@ -48,12 +45,6 @@ export async function createPdf(paths, svgSize, scaleFactor, paperSize) { scale: scaleFactor }; - // centre line - // page.drawLine({ - // start: { x: width/2, y: height }, - // end: { x: width/2, y: 0 }, - // }); - page.moveTo(5, height - 5); // panel page.drawSvgPath(paths[0], svgSettings);