001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2014, 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 * LineUtilities.java
029 * ------------------
030 * (C) Copyright 2008, 2014, 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 * 28-Feb-2014 : Added extendLine() (DG);
039 *
040 */
041
042package org.jfree.chart.util;
043
044import java.awt.geom.Line2D;
045import java.awt.geom.Rectangle2D;
046
047/**
048 * Some utility methods for {@link Line2D} objects.
049 *
050 * @since 1.0.12
051 */
052public class LineUtilities {
053
054    /**
055     * Clips the specified line to the given rectangle.
056     *
057     * @param line  the line (<code>null</code> not permitted).
058     * @param rect  the clipping rectangle (<code>null</code> not permitted).
059     *
060     * @return <code>true</code> if the clipped line is visible, and
061     *     <code>false</code> otherwise.
062     */
063    public static boolean clipLine(Line2D line, Rectangle2D rect) {
064
065        double x1 = line.getX1();
066        double y1 = line.getY1();
067        double x2 = line.getX2();
068        double y2 = line.getY2();
069
070        double minX = rect.getMinX();
071        double maxX = rect.getMaxX();
072        double minY = rect.getMinY();
073        double maxY = rect.getMaxY();
074
075        int f1 = rect.outcode(x1, y1);
076        int f2 = rect.outcode(x2, y2);
077
078        while ((f1 | f2) != 0) {
079            if ((f1 & f2) != 0) {
080                return false;
081            }
082            double dx = (x2 - x1);
083            double dy = (y2 - y1);
084            // update (x1, y1), (x2, y2) and f1 and f2 using intersections
085            // then recheck
086            if (f1 != 0) {
087                // first point is outside, so we update it against one of the
088                // four sides then continue
089                if ((f1 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT
090                        && dx != 0.0) {
091                    y1 = y1 + (minX - x1) * dy / dx;
092                    x1 = minX;
093                }
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                }
099                else if ((f1 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM
100                        && dy != 0.0) {
101                    x1 = x1 + (maxY - y1) * dx / dy;
102                    y1 = maxY;
103                }
104                else if ((f1 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP
105                        && dy != 0.0) {
106                    x1 = x1 + (minY - y1) * dx / dy;
107                    y1 = minY;
108                }
109                f1 = rect.outcode(x1, y1);
110            }
111            else if (f2 != 0) {
112                // second point is outside, so we update it against one of the
113                // four sides then continue
114                if ((f2 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT
115                        && dx != 0.0) {
116                    y2 = y2 + (minX - x2) * dy / dx;
117                    x2 = minX;
118                }
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                }
124                else if ((f2 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM
125                        && dy != 0.0) {
126                    x2 = x2 + (maxY - y2) * dx / dy;
127                    y2 = maxY;
128                }
129                else if ((f2 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP
130                        && dy != 0.0) {
131                    x2 = x2 + (minY - y2) * dx / dy;
132                    y2 = minY;
133                }
134                f2 = rect.outcode(x2, y2);
135            }
136        }
137
138        line.setLine(x1, y1, x2, y2);
139        return true;  // the line is visible - if it wasn't, we'd have
140                      // returned false from within the while loop above
141
142    }
143    
144    /**
145     * Creates a new line by extending an existing line.
146     *
147     * @param line  the line (<code>null</code> not permitted).
148     * @param startPercent  the amount to extend the line at the start point
149     *                      end.
150     * @param endPercent  the amount to extend the line at the end point end.
151     *
152     * @return A new line.
153     * 
154     * @since 1.0.18
155     */
156    public static Line2D extendLine(Line2D line, double startPercent,
157                              double endPercent) {
158        ParamChecks.nullNotPermitted(line, "line");
159        double x1 = line.getX1();
160        double x2 = line.getX2();
161        double deltaX = x2 - x1;
162        double y1 = line.getY1();
163        double y2 = line.getY2();
164        double deltaY = y2 - y1;
165        x1 = x1 - (startPercent * deltaX);
166        y1 = y1 - (startPercent * deltaY);
167        x2 = x2 + (endPercent * deltaX);
168        y2 = y2 + (endPercent * deltaY);
169        return new Line2D.Double(x1, y1, x2, y2);
170    }
171
172}