aboutsummaryrefslogtreecommitdiff
path: root/lib/utils/math_utils.dart
diff options
context:
space:
mode:
authormirror <mirror@localhost>2026-11-22 08:41:00 +0900
committermirror <mirror@localhost>2026-11-22 08:41:00 +0900
commit449d8b74bd6d9dae98a07254cdfad677b920e7b0 (patch)
treec131c3528c4d517f56e19b854a7453fedb097d40 /lib/utils/math_utils.dart
downloadaves-src-master.tar.gz
aves-src-master.zip
snapshot of aves v1.13.12 (secrets removed: android/app/google-services.json)HEADv1.13.12master
Diffstat (limited to 'lib/utils/math_utils.dart')
-rw-r--r--lib/utils/math_utils.dart49
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/utils/math_utils.dart b/lib/utils/math_utils.dart
new file mode 100644
index 0000000..5a57506
--- /dev/null
+++ b/lib/utils/math_utils.dart
@@ -0,0 +1,49 @@
+import 'dart:math';
+import 'dart:ui';
+
+int highestPowerOf2(num x) => x < 1 ? 0 : pow(2, (log(x) / ln2).floor()).toInt();
+
+num smallestPowerOf2(num x, {bool allowNegativePower = false}) {
+ if ((x < 1 && !allowNegativePower) || x <= 0) return 1;
+ return pow(2, (log(x) / ln2).ceil());
+}
+
+double roundToPrecision(final double value, {required final int decimals}) => (value * pow(10, decimals)).round() / pow(10, decimals);
+
+// cf https://en.wikipedia.org/wiki/Intersection_(geometry)#Two_line_segments
+Offset? segmentIntersection((Offset, Offset) s1, (Offset, Offset) s2) {
+ final x1 = s1.$1.dx;
+ final y1 = s1.$1.dy;
+ final x2 = s1.$2.dx;
+ final y2 = s1.$2.dy;
+
+ final x3 = s2.$1.dx;
+ final y3 = s2.$1.dy;
+ final x4 = s2.$2.dx;
+ final y4 = s2.$2.dy;
+
+ final a1 = x2 - x1;
+ final b1 = -(x4 - x3);
+ final c1 = x3 - x1;
+ final a2 = y2 - y1;
+ final b2 = -(y4 - y3);
+ final c2 = y3 - y1;
+
+ final denom = a1 * b2 - a2 * b1;
+ if (denom == 0) {
+ // lines are parallel
+ return null;
+ }
+
+ final s0 = (c1 * b2 - c2 * b1) / denom;
+ final t0 = (a1 * c2 - a2 * c1) / denom;
+
+ if (!(0 <= s0 && s0 <= 1 && 0 <= t0 && t0 <= 1)) {
+ // segments do not intersect
+ return null;
+ }
+
+ final x0 = x1 + s0 * (x2 - x1);
+ final y0 = y1 + s0 * (y2 - y1);
+ return Offset(x0, y0);
+}