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