Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: micro-optimize Bounds #1706

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions lib/src/misc/bounds.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class Bounds<T extends num> {
final Point<T> max;

factory Bounds(Point<T> a, Point<T> 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<T>(minx, miny), Point<T>(maxx, maxy));
}

const Bounds._(this.min, this.max);
Expand All @@ -38,14 +38,8 @@ class Bounds<T extends num> {
/// point.
Bounds<T> extend(Point<T> 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)),
);
}

Expand Down Expand Up @@ -73,9 +67,10 @@ class Bounds<T extends num> {
}

bool contains(Point<T> 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<T> b) {
Expand All @@ -102,7 +97,7 @@ class Bounds<T extends num> {
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;
Expand Down
Loading