001/* ======================================================
002 * JFreeChart : a chart library for the Java(tm) platform
003 * ======================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert and Contributors.
006 *
007 * Project Info:  https://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    private LineUtils() {
048        // no requirement to instantiate
049    }
050
051    /**
052     * Clips the specified line to the given rectangle.  If any of the line
053     * coordinates is not finite, then the method returns {@code false} and
054     * the line is not modified.
055     *
056     * @param line  the line ({@code null} not permitted).
057     * @param rect  the clipping rectangle ({@code null} not permitted).
058     *
059     * @return {@code true} if the clipped line is visible, and
060     *     {@code false} 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        // check that the line can be worked with (bug#223)
070        if ((!Double.isFinite(x1) || !Double.isFinite(y1)) 
071                || !Double.isFinite(x2) || !Double.isFinite(y2)) {
072            return false;
073        }
074
075        double minX = rect.getMinX();
076        double maxX = rect.getMaxX();
077        double minY = rect.getMinY();
078        double maxY = rect.getMaxY();
079
080        int f1 = rect.outcode(x1, y1);
081        int f2 = rect.outcode(x2, y2);
082
083        while ((f1 | f2) != 0) {
084            if ((f1 & f2) != 0) {
085                return false;
086            }
087            double dx = (x2 - x1);
088            double dy = (y2 - y1);
089            // update (x1, y1), (x2, y2) and f1 and f2 using intersections
090            // then recheck
091            if (f1 != 0) {
092                // first point is outside, so we update it against one of the
093                // four sides then continue
094                if ((f1 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT
095                        && dx != 0.0) {
096                    y1 = y1 + (minX - x1) * dy / dx;
097                    x1 = minX;
098                } else if ((f1 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT
099                        && dx != 0.0) {
100                    y1 = y1 + (maxX - x1) * dy / dx;
101                    x1 = maxX;
102                } else if ((f1 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM
103                        && dy != 0.0) {
104                    x1 = x1 + (maxY - y1) * dx / dy;
105                    y1 = maxY;
106                } else if ((f1 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP
107                        && dy != 0.0) {
108                    x1 = x1 + (minY - y1) * dx / dy;
109                    y1 = minY;
110                }
111                f1 = rect.outcode(x1, y1);
112            } else if (f2 != 0) {
113                // second point is outside, so we update it against one of the
114                // four sides then continue
115                if ((f2 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT
116                        && dx != 0.0) {
117                    y2 = y2 + (minX - x2) * dy / dx;
118                    x2 = minX;
119                } else if ((f2 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT
120                        && dx != 0.0) {
121                    y2 = y2 + (maxX - x2) * dy / dx;
122                    x2 = maxX;
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                } else if ((f2 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP
128                        && dy != 0.0) {
129                    x2 = x2 + (minY - y2) * dx / dy;
130                    y2 = minY;
131                }
132                f2 = rect.outcode(x2, y2);
133            }
134        }
135
136        line.setLine(x1, y1, x2, y2);
137        return true;  // the line is visible - if it wasn't, we'd have
138                      // returned false from within the while loop above
139
140    }
141    
142    /**
143     * Creates a new line by extending an existing line.
144     *
145     * @param line  the line ({@code null} not permitted).
146     * @param startPercent  the amount to extend the line at the start point
147     *                      end.
148     * @param endPercent  the amount to extend the line at the end point end.
149     *
150     * @return A new line.
151     */
152    public static Line2D extendLine(Line2D line, double startPercent,
153            double endPercent) {
154        Args.nullNotPermitted(line, "line");
155        double x1 = line.getX1();
156        double x2 = line.getX2();
157        double deltaX = x2 - x1;
158        double y1 = line.getY1();
159        double y2 = line.getY2();
160        double deltaY = y2 - y1;
161        x1 = x1 - (startPercent * deltaX);
162        y1 = y1 - (startPercent * deltaY);
163        x2 = x2 + (endPercent * deltaX);
164        y2 = y2 + (endPercent * deltaY);
165        return new Line2D.Double(x1, y1, x2, y2);
166    }
167
168}