mathjo - commit 063466a
Added function to get the distance between two points [x1,y1] and [x2,y2]063466a2522f5cb830689635c12eff52d77cf71aLukePJ25<luke.jones.2567@gmail.com>2025-02-02T14:24:21Zdiff --git a/mathjo.js b/mathjo.js
index 48edc36..5368cd1 100644
--- a/mathjo.js
+++ b/mathjo.js
@@ -24,6 +24,18 @@ const MathJo = {
return [x / length, y / length];
},
+ /**
+ * Return the 2D Euclidian distance between points (x1,y1) and (x2,y2)
+ * @param x1 the x-position of point 1
+ * @param y1 the y-position of point 1
+ * @param x2 the x-position of point 2
+ * @param y2 the y-position of point 2
+ * @return the distance between (x1,y1) and (x2,y2)
+ */
+ getDistanceTwoPoints: function (x1, y1, x2, y2) {
+ return Math.hypot(x1 - x2, y1 - y2);
+ },
+
/**
* Converts an angle in radians to a normalized direction vector.
* @param a The angle in radians.
@@ -59,6 +71,16 @@ const MathJo = {
return (v <= mx && v >= mn);
},
+ /**
+ * Truncates all numbers in an array to a specified number of decimal places.
+ * @param c The array of numbers.
+ * @param y The number of decimal places.
+ * @return A new array with truncated values.
+ */
+ truncateArray: function (c, y) {
+ return c.map(num => Maths.truncateNumber(num, y));
+ },
+
/**
* Truncates a number to a specified number of decimal places.
* @param x The number to truncate.
@@ -70,16 +92,6 @@ const MathJo = {
return Math.trunc(x * factor) / factor;
},
- /**
- * Truncates all numbers in an array to a specified number of decimal places.
- * @param c The array of numbers.
- * @param y The number of decimal places.
- * @return A new array with truncated values.
- */
- truncateArray: function (c, y) {
- return c.map(num => Maths.truncateNumber(num, y));
- },
-
/**
* Wraps a value around within a given range [0, max).
* @param v The value to wrap.