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