001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025 * in the United States and other countries.]
026 *
027 * ------------------
028 * LineUtilities.java
029 * ------------------
030 * (C) Copyright 2008, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes
036 * -------
037 * 05-Nov-2008 : Version 1 (DG);
038 *
039 */
040
041package org.jfree.chart.util;
042
043import java.awt.geom.Line2D;
044import java.awt.geom.Rectangle2D;
045
046/**
047 * Some utility methods for {@link Line2D} objects.
048 *
049 * @since 1.0.12
050 */
051public class LineUtilities {
052
053    /**
054     * Clips the specified line to the given rectangle.
055     *
056     * @param line  the line (<code>null</code> not permitted).
057     * @param rect  the clipping rectangle (<code>null</code> not permitted).
058     *
059     * @return <code>true</code> if the clipped line is visible, and
060     *     <code>false</code> otherwise.
061     */
062    public static boolean clipLine(Line2D line, Rectangle2D rect) {
063
064        double x1 = line.getX1();
065        double y1 = line.getY1();
066        double x2 = line.getX2();
067        double y2 = line.getY2();
068
069        double minX = rect.getMinX();
070        double maxX = rect.getMaxX();
071        double minY = rect.getMinY();
072        double maxY = rect.getMaxY();
073
074        int f1 = rect.outcode(x1, y1);
075        int f2 = rect.outcode(x2, y2);
076
077        while ((f1 | f2) != 0) {
078            if ((f1 & f2) != 0) {
079                return false;
080            }
081            double dx = (x2 - x1);
082            double dy = (y2 - y1);
083            // update (x1, y1), (x2, y2) and f1 and f2 using intersections
084            // then recheck
085            if (f1 != 0) {
086                // first point is outside, so we update it against one of the
087                // four sides then continue
088                if ((f1 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT
089                        && dx != 0.0) {
090                    y1 = y1 + (minX - x1) * dy / dx;
091                    x1 = minX;
092                }
093                else if ((f1 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT
094                        && dx != 0.0) {
095                    y1 = y1 + (maxX - x1) * dy / dx;
096                    x1 = maxX;
097                }
098                else if ((f1 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM
099                        && dy != 0.0) {
100                    x1 = x1 + (maxY - y1) * dx / dy;
101                    y1 = maxY;
102                }
103                else if ((f1 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP
104                        && dy != 0.0) {
105                    x1 = x1 + (minY - y1) * dx / dy;
106                    y1 = minY;
107                }
108                f1 = rect.outcode(x1, y1);
109            }
110            else if (f2 != 0) {
111                // second point is outside, so we update it against one of the
112                // four sides then continue
113                if ((f2 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT
114                        && dx != 0.0) {
115                    y2 = y2 + (minX - x2) * dy / dx;
116                    x2 = minX;
117                }
118                else if ((f2 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT
119                        && dx != 0.0) {
120                    y2 = y2 + (maxX - x2) * dy / dx;
121                    x2 = maxX;
122                }
123                else if ((f2 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM
124                        && dy != 0.0) {
125                    x2 = x2 + (maxY - y2) * dx / dy;
126                    y2 = maxY;
127                }
128                else if ((f2 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP
129                        && dy != 0.0) {
130                    x2 = x2 + (minY - y2) * dx / dy;
131                    y2 = minY;
132                }
133                f2 = rect.outcode(x2, y2);
134            }
135        }
136
137        line.setLine(x1, y1, x2, y2);
138        return true;  // the line is visible - if it wasn't, we'd have
139                      // returned false from within the while loop above
140
141    }
142}