001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * --------------
028 * LineUtils.java
029 * --------------
030 * (C) Copyright 2008-present, by David Gilbert and Contributors.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.util;
038
039import java.awt.geom.Line2D;
040import java.awt.geom.Rectangle2D;
041
042/**
043 * Some utility methods for {@link Line2D} objects.
044 */
045public class LineUtils {
046
047    /**
048     * Clips the specified line to the given rectangle.  If any of the line
049     * coordinates is not finite, then the method returns {@code false} and
050     * the line is not modified.
051     *
052     * @param line  the line ({@code null} not permitted).
053     * @param rect  the clipping rectangle ({@code null} not permitted).
054     *
055     * @return {@code true} if the clipped line is visible, and
056     *     {@code false} otherwise.
057     */
058    public static boolean clipLine(Line2D line, Rectangle2D rect) {
059
060        double x1 = line.getX1();
061        double y1 = line.getY1();
062        double x2 = line.getX2();
063        double y2 = line.getY2();
064
065        // check that the line can be worked with (bug#223)
066        if ((!Double.isFinite(x1) || !Double.isFinite(y1)) 
067                || !Double.isFinite(x2) || !Double.isFinite(y2)) {
068            return false;
069        }
070
071        double minX = rect.getMinX();
072        double maxX = rect.getMaxX();
073        double minY = rect.getMinY();
074        double maxY = rect.getMaxY();
075
076        int f1 = rect.outcode(x1, y1);
077        int f2 = rect.outcode(x2, y2);
078
079        while ((f1 | f2) != 0) {
080            if ((f1 & f2) != 0) {
081                return false;
082            }
083            double dx = (x2 - x1);
084            double dy = (y2 - y1);
085            // update (x1, y1), (x2, y2) and f1 and f2 using intersections
086            // then recheck
087            if (f1 != 0) {
088                // first point is outside, so we update it against one of the
089                // four sides then continue
090                if ((f1 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT
091                        && dx != 0.0) {
092                    y1 = y1 + (minX - x1) * dy / dx;
093                    x1 = minX;
094                } else if ((f1 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT
095                        && dx != 0.0) {
096                    y1 = y1 + (maxX - x1) * dy / dx;
097                    x1 = maxX;
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                } else if ((f1 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP
103                        && dy != 0.0) {
104                    x1 = x1 + (minY - y1) * dx / dy;
105                    y1 = minY;
106                }
107                f1 = rect.outcode(x1, y1);
108            } else if (f2 != 0) {
109                // second point is outside, so we update it against one of the
110                // four sides then continue
111                if ((f2 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT
112                        && dx != 0.0) {
113                    y2 = y2 + (minX - x2) * dy / dx;
114                    x2 = minX;
115                } else if ((f2 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT
116                        && dx != 0.0) {
117                    y2 = y2 + (maxX - x2) * dy / dx;
118                    x2 = maxX;
119                } else if ((f2 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM
120                        && dy != 0.0) {
121                    x2 = x2 + (maxY - y2) * dx / dy;
122                    y2 = maxY;
123                } else if ((f2 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP
124                        && dy != 0.0) {
125                    x2 = x2 + (minY - y2) * dx / dy;
126                    y2 = minY;
127                }
128                f2 = rect.outcode(x2, y2);
129            }
130        }
131
132        line.setLine(x1, y1, x2, y2);
133        return true;  // the line is visible - if it wasn't, we'd have
134                      // returned false from within the while loop above
135
136    }
137    
138    /**
139     * Creates a new line by extending an existing line.
140     *
141     * @param line  the line ({@code null} not permitted).
142     * @param startPercent  the amount to extend the line at the start point
143     *                      end.
144     * @param endPercent  the amount to extend the line at the end point end.
145     *
146     * @return A new line.
147     */
148    public static Line2D extendLine(Line2D line, double startPercent,
149            double endPercent) {
150        Args.nullNotPermitted(line, "line");
151        double x1 = line.getX1();
152        double x2 = line.getX2();
153        double deltaX = x2 - x1;
154        double y1 = line.getY1();
155        double y2 = line.getY2();
156        double deltaY = y2 - y1;
157        x1 = x1 - (startPercent * deltaX);
158        y1 = y1 - (startPercent * deltaY);
159        x2 = x2 + (endPercent * deltaX);
160        y2 = y2 + (endPercent * deltaY);
161        return new Line2D.Double(x1, y1, x2, y2);
162    }
163
164}