@flare-engine/math 0.4.10 → 0.4.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -267,6 +267,17 @@ const Color = {
267
267
  return (lMax + .05) / (lMin + .05);
268
268
  }
269
269
  };
270
+ /**
271
+ * Unpacks a 32-bit `0xAARRGGBB` int (the `Color.toSkColor` format) into `out`
272
+ * as `[r, g, b, a]`, each 0-1: the layout Skia paints and `clear` take.
273
+ * Zero-alloc.
274
+ */
275
+ function argbToFloat4Into(out, argb) {
276
+ out[0] = (argb >>> 16 & 255) / 255;
277
+ out[1] = (argb >>> 8 & 255) / 255;
278
+ out[2] = (argb & 255) / 255;
279
+ out[3] = (argb >>> 24 & 255) / 255;
280
+ }
270
281
  // src/curves.ts
271
282
  /**
272
283
  * Parametric curves — quadratic/cubic Bézier and Catmull-Rom — plus approximate
@@ -531,13 +542,18 @@ function easeInBounce(t) {
531
542
  function easeInOutBounce(t) {
532
543
  return t < .5 ? .5 * easeInBounce(t * 2) : .5 * easeOutBounce(t * 2 - 1) + .5;
533
544
  }
545
+ /** Alias of {@link easeInBack}, kept for compatibility. A Back curve, not a damped spring. */
534
546
  function springIn(t) {
535
- return t * t * (2.70158 * t - 1.70158);
547
+ return easeInBack(t);
536
548
  }
549
+ /** Alias of {@link easeOutBack}, kept for compatibility. A Back curve, not a damped spring. */
537
550
  function springOut(t) {
538
- const t1 = t - 1;
539
- return t1 * t1 * (2.70158 * t1 + 1.70158) + 1;
551
+ return easeOutBack(t);
540
552
  }
553
+ /**
554
+ * The Back in-out curve under a second name, kept for compatibility. A Back
555
+ * curve, not a damped spring.
556
+ */
541
557
  function springInOut(t) {
542
558
  if (t < .5) {
543
559
  const t2 = 2 * t;
@@ -546,73 +562,127 @@ function springInOut(t) {
546
562
  const t2 = 2 * t - 2;
547
563
  return .5 * (t2 * t2 * (3.5949095 * t2 + S2) + 2);
548
564
  }
565
+ // src/rect.ts
566
+ const Rect = {
567
+ /** Returns true if point is inside rect (inclusive). */
568
+ contains(r, p) {
569
+ return p.x >= r.x && p.x <= r.x + r.width && p.y >= r.y && p.y <= r.y + r.height;
570
+ },
571
+ /** Returns true if two rects overlap (share area). */
572
+ intersects(a, b) {
573
+ return a.x < b.x + b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.y + a.height > b.y;
574
+ },
575
+ /** Returns the overlapping rect, or null if no overlap. */
576
+ intersection(a, b) {
577
+ const x = Math.max(a.x, b.x);
578
+ const y = Math.max(a.y, b.y);
579
+ const right = Math.min(a.x + a.width, b.x + b.width);
580
+ const bottom = Math.min(a.y + a.height, b.y + b.height);
581
+ if (right <= x || bottom <= y) return null;
582
+ return {
583
+ x,
584
+ y,
585
+ width: right - x,
586
+ height: bottom - y
587
+ };
588
+ },
589
+ /** Returns the smallest rect containing both rects. */
590
+ union(a, b) {
591
+ const x = Math.min(a.x, b.x);
592
+ const y = Math.min(a.y, b.y);
593
+ const right = Math.max(a.x + a.width, b.x + b.width);
594
+ const bottom = Math.max(a.y + a.height, b.y + b.height);
595
+ return {
596
+ x,
597
+ y,
598
+ width: right - x,
599
+ height: bottom - y
600
+ };
601
+ },
602
+ /** Creates a rect from an array of points (bounding box). */
603
+ fromPoints(points) {
604
+ if (points.length === 0) return {
605
+ x: 0,
606
+ y: 0,
607
+ width: 0,
608
+ height: 0
609
+ };
610
+ let minX = points[0].x;
611
+ let minY = points[0].y;
612
+ let maxX = minX;
613
+ let maxY = minY;
614
+ for (let i = 1; i < points.length; i++) {
615
+ const p = points[i];
616
+ if (p.x < minX) minX = p.x;
617
+ if (p.y < minY) minY = p.y;
618
+ if (p.x > maxX) maxX = p.x;
619
+ if (p.y > maxY) maxY = p.y;
620
+ }
621
+ return {
622
+ x: minX,
623
+ y: minY,
624
+ width: maxX - minX,
625
+ height: maxY - minY
626
+ };
627
+ },
628
+ /** Returns the center point of the rect. */
629
+ center(r) {
630
+ return {
631
+ x: r.x + r.width * .5,
632
+ y: r.y + r.height * .5
633
+ };
634
+ },
635
+ /** Returns a new rect expanded by amount on each side. */
636
+ expand(r, amount) {
637
+ return {
638
+ x: r.x - amount,
639
+ y: r.y - amount,
640
+ width: r.width + amount * 2,
641
+ height: r.height + amount * 2
642
+ };
643
+ }
644
+ };
549
645
  // src/intersection.ts
550
646
  /** Returns true if point is inside rect (inclusive). */
551
647
  function pointInRect(p, r) {
552
- return p.x >= r.x && p.x <= r.x + r.width && p.y >= r.y && p.y <= r.y + r.height;
648
+ return Rect.contains(r, p);
553
649
  }
554
650
  /** Returns true if point is inside circle (inclusive). */
555
651
  function pointInCircle(p, c) {
556
- const dx = p.x - c.x;
557
- const dy = p.y - c.y;
558
- return dx * dx + dy * dy <= c.radius * c.radius;
652
+ return Circle.contains(c, p);
559
653
  }
560
654
  /** Returns true if two rects overlap. */
561
655
  function rectVsRect(a, b) {
562
- return a.x < b.x + b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.y + a.height > b.y;
656
+ return Rect.intersects(a, b);
563
657
  }
564
658
  /** Returns true if rect and circle overlap. */
565
659
  function rectVsCircle(r, c) {
566
- const closestX = c.x < r.x ? r.x : c.x > r.x + r.width ? r.x + r.width : c.x;
567
- const closestY = c.y < r.y ? r.y : c.y > r.y + r.height ? r.y + r.height : c.y;
568
- const dx = c.x - closestX;
569
- const dy = c.y - closestY;
570
- return dx * dx + dy * dy <= c.radius * c.radius;
660
+ return Circle.intersectsRect(c, r);
571
661
  }
572
662
  /** Returns true if two circles overlap. */
573
663
  function circleVsCircle(a, b) {
574
- const dx = b.x - a.x;
575
- const dy = b.y - a.y;
576
- const radSum = a.radius + b.radius;
577
- return dx * dx + dy * dy < radSum * radSum;
664
+ return Circle.intersects(a, b);
578
665
  }
579
666
  /** Line segment intersection with AABB. Uses Liang-Barsky algorithm. */
580
667
  function lineVsRect(x1, y1, x2, y2, r) {
581
668
  const dx = x2 - x1;
582
669
  const dy = y2 - y1;
583
- let tMin = 0;
584
- let tMax = 1;
585
- const edges = [
586
- {
587
- p: -dx,
588
- q: x1 - r.x
589
- },
590
- {
591
- p: dx,
592
- q: r.x + r.width - x1
593
- },
594
- {
595
- p: -dy,
596
- q: y1 - r.y
597
- },
598
- {
599
- p: dy,
600
- q: r.y + r.height - y1
601
- }
602
- ];
603
- for (const { p, q } of edges) if (Math.abs(p) < 1e-12) {
604
- if (q < 0) return false;
670
+ clipT[0] = 0;
671
+ clipT[1] = 1;
672
+ return clipEdge(-dx, x1 - r.x) && clipEdge(dx, r.x + r.width - x1) && clipEdge(-dy, y1 - r.y) && clipEdge(dy, r.y + r.height - y1) && clipT[0] <= clipT[1];
673
+ }
674
+ const clipT = /* @__PURE__ */ new Float64Array(2);
675
+ function clipEdge(p, q) {
676
+ if (Math.abs(p) < 1e-12) return !(q < 0);
677
+ const t = q / p;
678
+ if (p < 0) {
679
+ if (t > clipT[1]) return false;
680
+ if (t > clipT[0]) clipT[0] = t;
605
681
  } else {
606
- const t = q / p;
607
- if (p < 0) {
608
- if (t > tMax) return false;
609
- if (t > tMin) tMin = t;
610
- } else {
611
- if (t < tMin) return false;
612
- if (t < tMax) tMax = t;
613
- }
682
+ if (t < clipT[0]) return false;
683
+ if (t < clipT[1]) clipT[1] = t;
614
684
  }
615
- return tMin <= tMax;
685
+ return true;
616
686
  }
617
687
  /** Line segment intersection with circle. */
618
688
  function lineVsCircle(x1, y1, x2, y2, c) {
@@ -634,16 +704,36 @@ function lineVsCircle(x1, y1, x2, y2, c) {
634
704
  if (t1 < 0 && t2 > 1) return true;
635
705
  return false;
636
706
  }
637
- /** Ray intersection with AABB. Returns t (distance) or null if no hit. */
707
+ /**
708
+ * Ray intersection with AABB. Returns t (distance in units of the direction's
709
+ * length) or null if no hit. A ray starting inside returns the exit distance.
710
+ *
711
+ * Edges are inclusive: a ray that only touches an edge or corner hits, and an
712
+ * axis-parallel ray running exactly along an edge hits. A zero direction
713
+ * (`dirX === 0 && dirY === 0`) returns null.
714
+ */
638
715
  function rayVsRect(originX, originY, dirX, dirY, r) {
639
- const invDx = dirX !== 0 ? 1 / dirX : Number.POSITIVE_INFINITY;
640
- const invDy = dirY !== 0 ? 1 / dirY : Number.POSITIVE_INFINITY;
641
- const tx1 = (r.x - originX) * invDx;
642
- const tx2 = (r.x + r.width - originX) * invDx;
643
- const ty1 = (r.y - originY) * invDy;
644
- const ty2 = (r.y + r.height - originY) * invDy;
645
- const tMin = Math.max(Math.min(tx1, tx2), Math.min(ty1, ty2));
646
- const tMax = Math.min(Math.max(tx1, tx2), Math.max(ty1, ty2));
716
+ if (dirX === 0 && dirY === 0) return null;
717
+ let tMin = Number.NEGATIVE_INFINITY;
718
+ let tMax = Number.POSITIVE_INFINITY;
719
+ if (dirX === 0) {
720
+ if (originX < r.x || originX > r.x + r.width) return null;
721
+ } else {
722
+ const invDx = 1 / dirX;
723
+ const tx1 = (r.x - originX) * invDx;
724
+ const tx2 = (r.x + r.width - originX) * invDx;
725
+ tMin = Math.min(tx1, tx2);
726
+ tMax = Math.max(tx1, tx2);
727
+ }
728
+ if (dirY === 0) {
729
+ if (originY < r.y || originY > r.y + r.height) return null;
730
+ } else {
731
+ const invDy = 1 / dirY;
732
+ const ty1 = (r.y - originY) * invDy;
733
+ const ty2 = (r.y + r.height - originY) * invDy;
734
+ tMin = Math.max(tMin, Math.min(ty1, ty2));
735
+ tMax = Math.min(tMax, Math.max(ty1, ty2));
736
+ }
647
737
  if (tMax < 0 || tMin > tMax) return null;
648
738
  return tMin >= 0 ? tMin : tMax;
649
739
  }
@@ -1011,86 +1101,6 @@ function seededIndex(seed, count) {
1011
1101
  if (count <= 1) return 0;
1012
1102
  return hashU32(seed) % count;
1013
1103
  }
1014
- // src/rect.ts
1015
- const Rect = {
1016
- /** Returns true if point is inside rect (inclusive). */
1017
- contains(r, p) {
1018
- return p.x >= r.x && p.x <= r.x + r.width && p.y >= r.y && p.y <= r.y + r.height;
1019
- },
1020
- /** Returns true if two rects overlap (share area). */
1021
- intersects(a, b) {
1022
- return a.x < b.x + b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.y + a.height > b.y;
1023
- },
1024
- /** Returns the overlapping rect, or null if no overlap. */
1025
- intersection(a, b) {
1026
- const x = Math.max(a.x, b.x);
1027
- const y = Math.max(a.y, b.y);
1028
- const right = Math.min(a.x + a.width, b.x + b.width);
1029
- const bottom = Math.min(a.y + a.height, b.y + b.height);
1030
- if (right <= x || bottom <= y) return null;
1031
- return {
1032
- x,
1033
- y,
1034
- width: right - x,
1035
- height: bottom - y
1036
- };
1037
- },
1038
- /** Returns the smallest rect containing both rects. */
1039
- union(a, b) {
1040
- const x = Math.min(a.x, b.x);
1041
- const y = Math.min(a.y, b.y);
1042
- const right = Math.max(a.x + a.width, b.x + b.width);
1043
- const bottom = Math.max(a.y + a.height, b.y + b.height);
1044
- return {
1045
- x,
1046
- y,
1047
- width: right - x,
1048
- height: bottom - y
1049
- };
1050
- },
1051
- /** Creates a rect from an array of points (bounding box). */
1052
- fromPoints(points) {
1053
- if (points.length === 0) return {
1054
- x: 0,
1055
- y: 0,
1056
- width: 0,
1057
- height: 0
1058
- };
1059
- let minX = points[0].x;
1060
- let minY = points[0].y;
1061
- let maxX = minX;
1062
- let maxY = minY;
1063
- for (let i = 1; i < points.length; i++) {
1064
- const p = points[i];
1065
- if (p.x < minX) minX = p.x;
1066
- if (p.y < minY) minY = p.y;
1067
- if (p.x > maxX) maxX = p.x;
1068
- if (p.y > maxY) maxY = p.y;
1069
- }
1070
- return {
1071
- x: minX,
1072
- y: minY,
1073
- width: maxX - minX,
1074
- height: maxY - minY
1075
- };
1076
- },
1077
- /** Returns the center point of the rect. */
1078
- center(r) {
1079
- return {
1080
- x: r.x + r.width * .5,
1081
- y: r.y + r.height * .5
1082
- };
1083
- },
1084
- /** Returns a new rect expanded by amount on each side. */
1085
- expand(r, amount) {
1086
- return {
1087
- x: r.x - amount,
1088
- y: r.y - amount,
1089
- width: r.width + amount * 2,
1090
- height: r.height + amount * 2
1091
- };
1092
- }
1093
- };
1094
1104
  // src/reward.ts
1095
1105
  /**
1096
1106
  * Timed-window and capped-bonus math — pure, zero-alloc scoring helpers.
@@ -1377,12 +1387,38 @@ const Vec2 = {
1377
1387
  y
1378
1388
  };
1379
1389
  },
1380
- /** Spherical linear interpolation for unit vectors. */
1390
+ /** Spherical linear interpolation for unit vectors. Antiparallel inputs (angle within 1e-6 of
1391
+ * π) have no unique shortest arc, so `a` is rotated by `t·π` turning toward `Vec2.perp(a)` —
1392
+ * clockwise in y-down screen space — and `t = 0.5` yields `Vec2.perp(a)`. `t = 0` and `t = 1`
1393
+ * return `a` and `b` exactly. Writes into out if provided. */
1381
1394
  slerp(a, b, t, out) {
1382
1395
  let d = a.x * b.x + a.y * b.y;
1383
1396
  d = d < -1 ? -1 : d > 1 ? 1 : d;
1384
1397
  const theta = Math.acos(d);
1385
1398
  if (theta < 1e-6) return Vec2.lerp(a, b, t, out);
1399
+ if (theta > Math.PI - 1e-6) {
1400
+ let x = a.x;
1401
+ let y = a.y;
1402
+ if (t === 1) {
1403
+ x = b.x;
1404
+ y = b.y;
1405
+ } else if (t !== 0) {
1406
+ const phi = t * Math.PI;
1407
+ const c = Math.cos(phi);
1408
+ const s = Math.sin(phi);
1409
+ x = a.x * c - a.y * s;
1410
+ y = a.y * c + a.x * s;
1411
+ }
1412
+ if (out) {
1413
+ out.x = x;
1414
+ out.y = y;
1415
+ return out;
1416
+ }
1417
+ return {
1418
+ x,
1419
+ y
1420
+ };
1421
+ }
1386
1422
  const sinTheta = Math.sin(theta);
1387
1423
  const wa = Math.sin((1 - t) * theta) / sinTheta;
1388
1424
  const wb = Math.sin(t * theta) / sinTheta;
@@ -1485,6 +1521,6 @@ const Vec2 = {
1485
1521
  return out;
1486
1522
  }
1487
1523
  };
1488
- export { Circle, Color, Mat3, Rect, SeededRNG, Transform2D, Vec2, advanceWindowedStreak, angleDelta, approxEquals, bezier2, bezier2Into, bezier2Length, bezier3, bezier3Into, bezier3Length, cappedBonus, catmullRom, catmullRomInto, catmullRomLength, choose, circleVsCircle, clamp, degToRad, easeInBack, easeInBounce, easeInCirc, easeInCubic, easeInElastic, easeInExpo, easeInOutBack, easeInOutBounce, easeInOutCirc, easeInOutCubic, easeInOutElastic, easeInOutExpo, easeInOutQuad, easeInOutQuart, easeInOutQuint, easeInOutSine, easeInQuad, easeInQuart, easeInQuint, easeInSine, easeOutBack, easeOutBounce, easeOutCirc, easeOutCubic, easeOutElastic, easeOutExpo, easeOutQuad, easeOutQuart, easeOutQuint, easeOutSine, hashU32, inverseLerp, lerp, lineVsCircle, lineVsRect, linear, map, pingPong, pointInCircle, pointInRect, radToDeg, randomFloat, randomInt, randomPointInCircle, randomVec2InRect, rayVsRect, rectVsCircle, rectVsRect, remainingInWindow, rotateTowards, seededIndex, segmentIntersection, segmentIntersectionInto, segmentsIntersect, shuffle, sign, smoothStep, springIn, springInOut, springOut, step, vec2, weightedIndex, weightedIndexBy, weightedPick, windowExpired, wrap };
1524
+ export { Circle, Color, Mat3, Rect, SeededRNG, Transform2D, Vec2, advanceWindowedStreak, angleDelta, approxEquals, argbToFloat4Into, bezier2, bezier2Into, bezier2Length, bezier3, bezier3Into, bezier3Length, cappedBonus, catmullRom, catmullRomInto, catmullRomLength, choose, circleVsCircle, clamp, degToRad, easeInBack, easeInBounce, easeInCirc, easeInCubic, easeInElastic, easeInExpo, easeInOutBack, easeInOutBounce, easeInOutCirc, easeInOutCubic, easeInOutElastic, easeInOutExpo, easeInOutQuad, easeInOutQuart, easeInOutQuint, easeInOutSine, easeInQuad, easeInQuart, easeInQuint, easeInSine, easeOutBack, easeOutBounce, easeOutCirc, easeOutCubic, easeOutElastic, easeOutExpo, easeOutQuad, easeOutQuart, easeOutQuint, easeOutSine, hashU32, inverseLerp, lerp, lineVsCircle, lineVsRect, linear, map, pingPong, pointInCircle, pointInRect, radToDeg, randomFloat, randomInt, randomPointInCircle, randomVec2InRect, rayVsRect, rectVsCircle, rectVsRect, remainingInWindow, rotateTowards, seededIndex, segmentIntersection, segmentIntersectionInto, segmentsIntersect, shuffle, sign, smoothStep, springIn, springInOut, springOut, step, vec2, weightedIndex, weightedIndexBy, weightedPick, windowExpired, wrap };
1489
1525
 
1490
1526
  //# sourceMappingURL=index.mjs.map