commit 4a75cb10996d34354e602fb5aaee89f0505d88fa
parent b630e7b3f9142ef58e2882e47f7c68b2843e053b
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Sat, 12 Oct 2024 20:11:44 -0700
Move setLength to geometry library
Also rename it to simply length and allow it to get the length of
a line aswell.
Diffstat:
2 files changed, 21 insertions(+), 18 deletions(-)
diff --git a/files/floorplans/floorplan/geometry.js b/files/floorplans/floorplan/geometry.js
@@ -175,3 +175,22 @@ export function rad(deg) {
return deg * Math.PI / 180
}
+export function length(a, b, length) {
+ if (!length) {
+ return a.distanceTo(b)
+ }
+
+ /*
+ * Not sure if a zero length line is worth supporting, it doesn't
+ * really work naturally. To support it you would need another
+ * store of information in addition to the vector
+ */
+ if (length <= 0) {
+ throw new Error("Zero length line wouldn't be able to be lengthened again")
+ }
+ /*
+ * Basically make it's origin zero, normalize it to be from
+ * 0-1, multiply it by length, then add the origin back to it.
+ */
+ return b.sub(a).normalize().multiplyScalar(length).add(a)
+}
diff --git a/files/floorplans/floorplan/main.js b/files/floorplans/floorplan/main.js
@@ -4,7 +4,7 @@ import * as ui from "/lib/ui.js"
import * as etc from "/lib/etc.js"
import * as lib from "./editor.js" // Confusing, but I don't want to fix variable conflict
import { Vector2 } from "/lib/github.com/mrdoob/three.js/math/Vector2.js"
-import "./geometry.js"
+import * as geometry from "./geometry.js"
import * as backend from "./backend.js"
import * as api from "/lib/api.js"
@@ -535,7 +535,7 @@ function precisePointHandler(event, editor, state) {
return
}
if (len> 0) {
- vecs[1] = setLength(vecs[0], vecs[1], len)
+ vecs[1] = geometry.length(vecs[0], vecs[1], len)
updatePoint(vecs[1], { leave_input: true })
}
})
@@ -1214,22 +1214,6 @@ function snap(point, on, directions) {
)
}
-function setLength(a, b, length) {
- /*
- * Not sure if a zero length line is worth supporting, it doesn't
- * really work naturally. To support it you would need another
- * store of information in addition to the vector
- */
- if (length <= 0) {
- throw new Error("Zero length line wouldn't be able to be lengthened again")
- }
- /*
- * Basically make it's origin zero, normalize it to be from
- * 0-1, multiply it by length, then add the origin back to it.
- */
- return b.sub(a).normalize().multiplyScalar(length).add(a)
-}
-
function preventDefaultHandler(event) {
handled(event)
}