--- /dev/null
+package org.simantics.diagram.profile;\r
+/* \r
+ * JFreeChart : a free chart library for the Java(tm) platform\r
+ * \r
+ *\r
+ * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors.\r
+ *\r
+ * Project Info: http://www.jfree.org/jfreechart/index.html\r
+ *\r
+ * This library is free software; you can redistribute it and/or modify it\r
+ * under the terms of the GNU Lesser General Public License as published by\r
+ * the Free Software Foundation; either version 2.1 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This library is distributed in the hope that it will be useful, but\r
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public\r
+ * License for more details.\r
+ *\r
+ * You should have received a copy of the GNU Lesser General Public\r
+ * License along with this library; if not, write to the Free Software\r
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,\r
+ * USA.\r
+ *\r
+ * [Java is a trademark or registered trademark of Sun Microsystems, Inc.\r
+ * in the United States and other countries.]\r
+ *\r
+ * ------------------\r
+ * LineUtilities.java\r
+ * ------------------\r
+ * (C) Copyright 2008, by Object Refinery Limited and Contributors.\r
+ *\r
+ * Original Author: David Gilbert (for Object Refinery Limited);\r
+ * Contributor(s): -;\r
+ *\r
+ * Changes\r
+ * -------\r
+ * 05-Nov-2008 : Version 1 (DG);\r
+ *\r
+ */\r
+\r
+import java.awt.geom.Line2D;\r
+import java.awt.geom.Rectangle2D;\r
+\r
+/**\r
+ * Some utility methods for {@link Line2D} objects.\r
+ *\r
+ * @since 1.0.12\r
+ */\r
+public class LineUtilities {\r
+\r
+ /**\r
+ * Clips the specified line to the given rectangle.\r
+ *\r
+ * @param line the line (<code>null</code> not permitted).\r
+ * @param rect the clipping rectangle (<code>null</code> not permitted).\r
+ *\r
+ * @return <code>true</code> if the clipped line is visible, and\r
+ * <code>false</code> otherwise.\r
+ */\r
+ public static boolean clipLine(Line2D line, Rectangle2D rect) {\r
+\r
+ double x1 = line.getX1();\r
+ double y1 = line.getY1();\r
+ double x2 = line.getX2();\r
+ double y2 = line.getY2();\r
+\r
+ double minX = rect.getMinX();\r
+ double maxX = rect.getMaxX();\r
+ double minY = rect.getMinY();\r
+ double maxY = rect.getMaxY();\r
+\r
+ int f1 = rect.outcode(x1, y1);\r
+ int f2 = rect.outcode(x2, y2);\r
+\r
+ while ((f1 | f2) != 0) {\r
+ if ((f1 & f2) != 0) {\r
+ return false;\r
+ }\r
+ double dx = (x2 - x1);\r
+ double dy = (y2 - y1);\r
+ // update (x1, y1), (x2, y2) and f1 and f2 using intersections\r
+ // then recheck\r
+ if (f1 != 0) {\r
+ // first point is outside, so we update it against one of the\r
+ // four sides then continue\r
+ if ((f1 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT\r
+ && dx != 0.0) {\r
+ y1 = y1 + (minX - x1) * dy / dx;\r
+ x1 = minX;\r
+ }\r
+ else if ((f1 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT\r
+ && dx != 0.0) {\r
+ y1 = y1 + (maxX - x1) * dy / dx;\r
+ x1 = maxX;\r
+ }\r
+ else if ((f1 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM\r
+ && dy != 0.0) {\r
+ x1 = x1 + (maxY - y1) * dx / dy;\r
+ y1 = maxY;\r
+ }\r
+ else if ((f1 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP\r
+ && dy != 0.0) {\r
+ x1 = x1 + (minY - y1) * dx / dy;\r
+ y1 = minY;\r
+ }\r
+ f1 = rect.outcode(x1, y1);\r
+ }\r
+ else if (f2 != 0) {\r
+ // second point is outside, so we update it against one of the\r
+ // four sides then continue\r
+ if ((f2 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT\r
+ && dx != 0.0) {\r
+ y2 = y2 + (minX - x2) * dy / dx;\r
+ x2 = minX;\r
+ }\r
+ else if ((f2 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT\r
+ && dx != 0.0) {\r
+ y2 = y2 + (maxX - x2) * dy / dx;\r
+ x2 = maxX;\r
+ }\r
+ else if ((f2 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM\r
+ && dy != 0.0) {\r
+ x2 = x2 + (maxY - y2) * dx / dy;\r
+ y2 = maxY;\r
+ }\r
+ else if ((f2 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP\r
+ && dy != 0.0) {\r
+ x2 = x2 + (minY - y2) * dx / dy;\r
+ y2 = minY;\r
+ }\r
+ f2 = rect.outcode(x2, y2);\r
+ }\r
+ }\r
+\r
+ line.setLine(x1, y1, x2, y2);\r
+ return true; // the line is visible - if it wasn't, we'd have\r
+ // returned false from within the while loop above\r
+\r
+ }\r
+\r
+}
\ No newline at end of file