public static Rect fromPoints(List<Point> points) { Rect rect = new Rect(); double minX = Double.MAX_VALUE; double minY = Double.MAX_VALUE; double maxX = Double.MIN_VALUE; double maxY = Double.MIN_VALUE; for (Point pt : points) { if (pt.getX() < minX) { minX = pt.getX(); } if (pt.getY() < minY) { minY = pt.getY(); } if (pt.getX() > maxX) { maxX = pt.getX(); } if (pt.getY() > maxY) { maxY = pt.getY(); } } double w = maxX - minX; double h = maxY - minY; rect.set(minX, minY, w, h); return rect; }
public void copyTo(Rect rect) { rect.x = x; rect.y = y; rect.width = width; rect.height = height; }
public boolean contains(Rect r) { return contains(r.x, r.y) && contains(r.x + r.getWidth(), r.y + r.getHeight()); }
public Rect expandIn(int w, int h, int maxW, int maxH) { Rect r = expand(w, h); if (r.getX() < 0) { r.setWidth(r.getWidth() + r.getX()); r.setX(0); } if (r.getY() < 0) { r.setHeight(r.getHeight() + r.getY()); r.setX(y); } if (r.getRightPos() > maxW) { r.setWidth(maxW - r.getX()); } if (r.getBottomPos() > maxH) { r.setHeight(maxH - r.getY()); } return r; }