]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/profile/LineUtilities.java
Sync git svn branch with SVN repository r33189.
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / profile / LineUtilities.java
1 package org.simantics.diagram.profile;\r
2 /* \r
3  * JFreeChart : a free chart library for the Java(tm) platform\r
4  * \r
5  *\r
6  * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors.\r
7  *\r
8  * Project Info:  http://www.jfree.org/jfreechart/index.html\r
9  *\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
14  *\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
19  *\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
23  * USA.\r
24  *\r
25  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.\r
26  * in the United States and other countries.]\r
27  *\r
28  * ------------------\r
29  * LineUtilities.java\r
30  * ------------------\r
31  * (C) Copyright 2008, by Object Refinery Limited and Contributors.\r
32  *\r
33  * Original Author:  David Gilbert (for Object Refinery Limited);\r
34  * Contributor(s):   -;\r
35  *\r
36  * Changes\r
37  * -------\r
38  * 05-Nov-2008 : Version 1 (DG);\r
39  *\r
40  */\r
41 \r
42 import java.awt.geom.Line2D;\r
43 import java.awt.geom.Rectangle2D;\r
44 \r
45 /**\r
46  * Some utility methods for {@link Line2D} objects.\r
47  *\r
48  * @since 1.0.12\r
49  */\r
50 public class LineUtilities {\r
51 \r
52     /**\r
53      * Clips the specified line to the given rectangle.\r
54      *\r
55      * @param line  the line (<code>null</code> not permitted).\r
56      * @param rect  the clipping rectangle (<code>null</code> not permitted).\r
57      *\r
58      * @return <code>true</code> if the clipped line is visible, and\r
59      *     <code>false</code> otherwise.\r
60      */\r
61     public static boolean clipLine(Line2D line, Rectangle2D rect) {\r
62 \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
67 \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
72 \r
73         int f1 = rect.outcode(x1, y1);\r
74         int f2 = rect.outcode(x2, y2);\r
75 \r
76         while ((f1 | f2) != 0) {\r
77             if ((f1 & f2) != 0) {\r
78                 return false;\r
79             }\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
83             // then recheck\r
84             if (f1 != 0) {\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
88                         && dx != 0.0) {\r
89                     y1 = y1 + (minX - x1) * dy / dx;\r
90                     x1 = minX;\r
91                 }\r
92                 else if ((f1 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT\r
93                         && dx != 0.0) {\r
94                     y1 = y1 + (maxX - x1) * dy / dx;\r
95                     x1 = maxX;\r
96                 }\r
97                 else if ((f1 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM\r
98                         && dy != 0.0) {\r
99                     x1 = x1 + (maxY - y1) * dx / dy;\r
100                     y1 = maxY;\r
101                 }\r
102                 else if ((f1 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP\r
103                         && dy != 0.0) {\r
104                     x1 = x1 + (minY - y1) * dx / dy;\r
105                     y1 = minY;\r
106                 }\r
107                 f1 = rect.outcode(x1, y1);\r
108             }\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
113                         && dx != 0.0) {\r
114                     y2 = y2 + (minX - x2) * dy / dx;\r
115                     x2 = minX;\r
116                 }\r
117                 else if ((f2 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT\r
118                         && dx != 0.0) {\r
119                     y2 = y2 + (maxX - x2) * dy / dx;\r
120                     x2 = maxX;\r
121                 }\r
122                 else if ((f2 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM\r
123                         && dy != 0.0) {\r
124                     x2 = x2 + (maxY - y2) * dx / dy;\r
125                     y2 = maxY;\r
126                 }\r
127                 else if ((f2 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP\r
128                         && dy != 0.0) {\r
129                     x2 = x2 + (minY - y2) * dx / dy;\r
130                     y2 = minY;\r
131                 }\r
132                 f2 = rect.outcode(x2, y2);\r
133             }\r
134         }\r
135 \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
139 \r
140     }\r
141 \r
142 }