From 05494e2eddd5d7eefa3263859e435c3ae5e8f205 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 27 Oct 2023 17:04:02 +0200 Subject: [PATCH] Simplify bounds. It's a very frequently initiated class, i.e. many times per frame. Try to create less garbage. --- lib/src/misc/bounds.dart | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/lib/src/misc/bounds.dart b/lib/src/misc/bounds.dart index e5fec1eac..731f91031 100644 --- a/lib/src/misc/bounds.dart +++ b/lib/src/misc/bounds.dart @@ -11,9 +11,9 @@ class Bounds { final Point max; factory Bounds(Point a, Point b) { - final bounds1 = Bounds._(a, b); - final bounds2 = bounds1.extend(a); - return bounds2.extend(b); + final (minx, maxx) = a.x > b.x ? (b.x, a.x) : (a.x, b.x); + final (miny, maxy) = a.y > b.y ? (b.y, a.y) : (a.y, b.y); + return Bounds._(Point(minx, miny), Point(maxx, maxy)); } const Bounds._(this.min, this.max); @@ -38,14 +38,8 @@ class Bounds { /// point. Bounds extend(Point point) { return Bounds._( - Point( - math.min(point.x, min.x), - math.min(point.y, min.y), - ), - Point( - math.max(point.x, max.x), - math.max(point.y, max.y), - ), + Point(math.min(point.x, min.x), math.min(point.y, min.y)), + Point(math.max(point.x, max.x), math.max(point.y, max.y)), ); } @@ -73,9 +67,10 @@ class Bounds { } bool contains(Point point) { - final min = point; - final max = point; - return containsBounds(Bounds(min, max)); + return (point.x >= min.x) && + (point.x <= max.x) && + (point.y >= min.y) && + (point.y <= max.y); } bool containsBounds(Bounds b) { @@ -102,7 +97,7 @@ class Bounds { final bottomY = math.min(max.y, b.max.y); if (leftX <= rightX && topY <= bottomY) { - return Bounds(Point(leftX, topY), Point(rightX, bottomY)); + return Bounds._(Point(leftX, topY), Point(rightX, bottomY)); } return null;