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