001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2020, 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 * [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-2020, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
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 *
045 * @since 1.0.12
046 */
047public class LineUtils {
048
049    /**
050     * Clips the specified line to the given rectangle.
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        double minX = rect.getMinX();
066        double maxX = rect.getMaxX();
067        double minY = rect.getMinY();
068        double maxY = rect.getMaxY();
069
070        int f1 = rect.outcode(x1, y1);
071        int f2 = rect.outcode(x2, y2);
072
073        while ((f1 | f2) != 0) {
074            if ((f1 & f2) != 0) {
075                return false;
076            }
077            double dx = (x2 - x1);
078            double dy = (y2 - y1);
079            // update (x1, y1), (x2, y2) and f1 and f2 using intersections
080            // then recheck
081            if (f1 != 0) {
082                // first point is outside, so we update it against one of the
083                // four sides then continue
084                if ((f1 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT
085                        && dx != 0.0) {
086                    y1 = y1 + (minX - x1) * dy / dx;
087                    x1 = minX;
088                }
089                else if ((f1 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT
090                        && dx != 0.0) {
091                    y1 = y1 + (maxX - x1) * dy / dx;
092                    x1 = maxX;
093                }
094                else if ((f1 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM
095                        && dy != 0.0) {
096                    x1 = x1 + (maxY - y1) * dx / dy;
097                    y1 = maxY;
098                }
099                else if ((f1 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP
100                        && dy != 0.0) {
101                    x1 = x1 + (minY - y1) * dx / dy;
102                    y1 = minY;
103                }
104                f1 = rect.outcode(x1, y1);
105            }
106            else if (f2 != 0) {
107                // second point is outside, so we update it against one of the
108                // four sides then continue
109                if ((f2 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT
110                        && dx != 0.0) {
111                    y2 = y2 + (minX - x2) * dy / dx;
112                    x2 = minX;
113                }
114                else if ((f2 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT
115                        && dx != 0.0) {
116                    y2 = y2 + (maxX - x2) * dy / dx;
117                    x2 = maxX;
118                }
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                }
124                else if ((f2 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP
125                        && dy != 0.0) {
126                    x2 = x2 + (minY - y2) * dx / dy;
127                    y2 = minY;
128                }
129                f2 = rect.outcode(x2, y2);
130            }
131        }
132
133        line.setLine(x1, y1, x2, y2);
134        return true;  // the line is visible - if it wasn't, we'd have
135                      // returned false from within the while loop above
136
137    }
138    
139    /**
140     * Creates a new line by extending an existing line.
141     *
142     * @param line  the line ({@code null} not permitted).
143     * @param startPercent  the amount to extend the line at the start point
144     *                      end.
145     * @param endPercent  the amount to extend the line at the end point end.
146     *
147     * @return A new line.
148     * 
149     * @since 1.0.18
150     */
151    public static Line2D extendLine(Line2D line, double startPercent,
152                              double endPercent) {
153        Args.nullNotPermitted(line, "line");
154        double x1 = line.getX1();
155        double x2 = line.getX2();
156        double deltaX = x2 - x1;
157        double y1 = line.getY1();
158        double y2 = line.getY2();
159        double deltaY = y2 - y1;
160        x1 = x1 - (startPercent * deltaX);
161        y1 = y1 - (startPercent * deltaY);
162        x2 = x2 + (endPercent * deltaX);
163        y2 = y2 + (endPercent * deltaY);
164        return new Line2D.Double(x1, y1, x2, y2);
165    }
166
167}