001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.visual.util;
042:
043: import java.awt.*;
044: import java.awt.geom.Rectangle2D;
045: import java.util.logging.Logger;
046:
047: /**
048: * @author David Kaspar
049: */
050: public final class GeomUtil {
051:
052: public static final Logger LOG = Logger
053: .getLogger("org.netbeans.api.visual"); // NOI18N
054:
055: public static final double M_PI_2 = Math.PI / 2;
056:
057: private GeomUtil() {
058: }
059:
060: /**
061: * Rounds Rectangle2D to Rectangle.
062: * @param rectangle the rectangle2D
063: * @return the rectangle
064: */
065: public static Rectangle roundRectangle(Rectangle2D rectangle) {
066: int x1 = (int) Math.floor(rectangle.getX());
067: int y1 = (int) Math.floor(rectangle.getY());
068: int x2 = (int) Math.ceil(rectangle.getMaxX());
069: int y2 = (int) Math.ceil(rectangle.getMaxY());
070: return new Rectangle(x1, y1, x2 - x1, y2 - y1);
071: }
072:
073: /**
074: * Returns a center point of a rectangle.
075: * @param rectangle the rectangle
076: * @return the center point
077: */
078: public static Point center(Rectangle rectangle) {
079: return new Point(rectangle.x + rectangle.width / 2, rectangle.y
080: + rectangle.height / 2);
081: }
082:
083: /**
084: * Returns a x-axis center of rectangle.
085: * @param rectangle the rectangle
086: * @return the x-axis center
087: */
088: public static int centerX(Rectangle rectangle) {
089: return rectangle.x + rectangle.width / 2;
090: }
091:
092: /**
093: * Returns a y-axis center of rectangle.
094: * @param rectangle the rectangle
095: * @return the y-axis center
096: */
097: public static int centerY(Rectangle rectangle) {
098: return rectangle.y + rectangle.height / 2;
099: }
100:
101: /**
102: * Returns a square distance of two points.
103: * @param p1 the first point
104: * @param p2 the second point
105: * @return the square distance
106: */
107: public static double distanceSq(Point p1, Point p2) {
108: int w = p2.x - p1.x;
109: int h = p2.y - p1.y;
110: return Math.sqrt(w * w + h * h);
111: }
112:
113: /**
114: * Returns whether two objects are equal
115: * @param o1 the first object; cound be null
116: * @param o2 the second object; cound be null
117: * @return true, if they are equal
118: */
119: public static boolean equals(Object o1, Object o2) {
120: return o1 == null ? o2 == null : o1.equals(o2);
121: }
122:
123: }
|