2
* $Id: RectUtils.java,v 1.2 2008/02/28 14:38:48 david Exp $
4
* Copyright (c) 2008 Gaudenz Alder
8
package org.jgraph.util;
10
import java.awt.geom.Rectangle2D;
12
public class RectUtils {
14
* Unions the pair of source <code>Rectangle2D</code> objects and puts the
15
* result into the returned <code>Rectangle2D</code> object. This method
16
* extends the Rectangle2D version by checking for null parameters, the
17
* returned value will also be <code>null</code> if the two input
18
* rectangles are <code>null</code>
21
* the first of a pair of <code>Rectangle2D</code> objects to
22
* be combined with each other
24
* the second of a pair of <code>Rectangle2D</code> objects to
25
* be combined with each other
28
public static Rectangle2D union(Rectangle2D src1, Rectangle2D src2) {
29
Rectangle2D result = null;
30
if (src1 == null && src2 == null) {
32
} else if (src1 != null && src2 != null) {
33
double x1 = Math.min(src1.getMinX(), src2.getMinX());
34
double y1 = Math.min(src1.getMinY(), src2.getMinY());
35
double x2 = Math.max(src1.getMaxX(), src2.getMaxX());
36
double y2 = Math.max(src1.getMaxY(), src2.getMaxY());
37
result = new Rectangle2D.Double();
38
result.setFrameFromDiagonal(x1, y1, x2, y2);
39
} else if (src1 != null) {
40
double x1 = src1.getMinX();
41
double y1 = src1.getMinY();
42
double x2 = src1.getMaxX();
43
double y2 = src1.getMaxY();
44
result = new Rectangle2D.Double();
45
result.setFrameFromDiagonal(x1, y1, x2, y2);
47
// only src2 is non-null
48
double x1 = src2.getMinX();
49
double y1 = src2.getMinY();
50
double x2 = src2.getMaxX();
51
double y2 = src2.getMaxY();
52
result = new Rectangle2D.Double();
53
result.setFrameFromDiagonal(x1, y1, x2, y2);