Rectangle<T extends num>.fromPoints constructor

Rectangle<T extends num>.fromPoints(
  1. Point<T> a,
  2. Point<T> b
)

Create a rectangle spanned by the points a and b;

The rectangle contains the points with x-coordinate between a.x and b.x, and with y-coordinate between a.y and b.y, both inclusive.

If the distance between a.x and b.x is not representable (which can happen if one or both is a double), the actual right edge might be slightly off from max(a.x, b.x). Similar for the y-coordinates and the bottom edge.

Example:

var leftTop = const Point(20, 50);
var rightBottom = const Point(300, 600);

var rectangle = Rectangle.fromPoints(leftTop, rightBottom);
print(rectangle); // Rectangle (20, 50) 280 x 550
print(rectangle.left); // 20
print(rectangle.top); // 50
print(rectangle.right); // 300
print(rectangle.bottom); // 600

Implementation

factory Rectangle.fromPoints(Point<T> a, Point<T> b) {
  T left = min(a.x, b.x);
  T width = (max(a.x, b.x) - left) as T;
  T top = min(a.y, b.y);
  T height = (max(a.y, b.y) - top) as T;
  return Rectangle<T>(left, top, width, height);
}