1 package org.simantics.diagram.profile;
3 * JFreeChart : a free chart library for the Java(tm) platform
6 * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors.
8 * Project Info: http://www.jfree.org/jfreechart/index.html
10 * This library is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
25 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
26 * in the United States and other countries.]
31 * (C) Copyright 2008, by Object Refinery Limited and Contributors.
33 * Original Author: David Gilbert (for Object Refinery Limited);
38 * 05-Nov-2008 : Version 1 (DG);
42 import java.awt.geom.Line2D;
43 import java.awt.geom.Rectangle2D;
46 * Some utility methods for {@link Line2D} objects.
50 public class LineUtilities {
53 * Clips the specified line to the given rectangle.
55 * @param line the line (<code>null</code> not permitted).
56 * @param rect the clipping rectangle (<code>null</code> not permitted).
58 * @return <code>true</code> if the clipped line is visible, and
59 * <code>false</code> otherwise.
61 public static boolean clipLine(Line2D line, Rectangle2D rect) {
63 double x1 = line.getX1();
64 double y1 = line.getY1();
65 double x2 = line.getX2();
66 double y2 = line.getY2();
68 double minX = rect.getMinX();
69 double maxX = rect.getMaxX();
70 double minY = rect.getMinY();
71 double maxY = rect.getMaxY();
73 int f1 = rect.outcode(x1, y1);
74 int f2 = rect.outcode(x2, y2);
76 while ((f1 | f2) != 0) {
80 double dx = (x2 - x1);
81 double dy = (y2 - y1);
82 // update (x1, y1), (x2, y2) and f1 and f2 using intersections
85 // first point is outside, so we update it against one of the
86 // four sides then continue
87 if ((f1 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT
89 y1 = y1 + (minX - x1) * dy / dx;
92 else if ((f1 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT
94 y1 = y1 + (maxX - x1) * dy / dx;
97 else if ((f1 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM
99 x1 = x1 + (maxY - y1) * dx / dy;
102 else if ((f1 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP
104 x1 = x1 + (minY - y1) * dx / dy;
107 f1 = rect.outcode(x1, y1);
110 // second point is outside, so we update it against one of the
111 // four sides then continue
112 if ((f2 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT
114 y2 = y2 + (minX - x2) * dy / dx;
117 else if ((f2 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT
119 y2 = y2 + (maxX - x2) * dy / dx;
122 else if ((f2 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM
124 x2 = x2 + (maxY - y2) * dx / dy;
127 else if ((f2 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP
129 x2 = x2 + (minY - y2) * dx / dy;
132 f2 = rect.outcode(x2, y2);
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