forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxRectangle.java
More file actions
312 lines (274 loc) · 6.61 KB
/
mxRectangle.java
File metadata and controls
312 lines (274 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
* Copyright (c) 2007-2010, Gaudenz Alder, David Benson
*/
package com.mxgraph.util;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
/**
* Implements a 2-dimensional rectangle with double precision coordinates.
*/
public class mxRectangle extends mxPoint
{
/**
*
*/
private static final long serialVersionUID = -3793966043543578946L;
/**
* Holds the width and the height. Default is 0.
*/
protected double width, height;
/**
* Constructs a new rectangle at (0, 0) with the width and height set to 0.
*/
public mxRectangle()
{
this(0, 0, 0, 0);
}
/**
* Constructs a copy of the given rectangle.
*
* @param rect Rectangle to construct a copy of.
*/
public mxRectangle(Rectangle2D rect)
{
this(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
}
/**
* Constructs a copy of the given rectangle.
*
* @param rect Rectangle to construct a copy of.
*/
public mxRectangle(mxRectangle rect)
{
this(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
}
/**
* Constructs a rectangle using the given parameters.
*
* @param x X-coordinate of the new rectangle.
* @param y Y-coordinate of the new rectangle.
* @param width Width of the new rectangle.
* @param height Height of the new rectangle.
*/
public mxRectangle(double x, double y, double width, double height)
{
super(x, y);
setWidth(width);
setHeight(height);
}
/**
* Returns the width of the rectangle.
*
* @return Returns the width.
*/
public double getWidth()
{
return width;
}
/**
* Sets the width of the rectangle.
*
* @param value Double that specifies the new width.
*/
public void setWidth(double value)
{
width = value;
}
/**
* Returns the height of the rectangle.
*
* @return Returns the height.
*/
public double getHeight()
{
return height;
}
/**
* Sets the height of the rectangle.
*
* @param value Double that specifies the new height.
*/
public void setHeight(double value)
{
height = value;
}
/**
* Sets this rectangle to the specified values
*
* @param x the new x-axis position
* @param y the new y-axis position
* @param w the new width of the rectangle
* @param h the new height of the rectangle
*/
public void setRect(double x, double y, double w, double h)
{
this.x = x;
this.y = y;
this.width = w;
this.height = h;
}
/**
* Adds the given rectangle to this rectangle.
*/
public void add(mxRectangle rect)
{
if (rect != null)
{
double minX = Math.min(x, rect.x);
double minY = Math.min(y, rect.y);
double maxX = Math.max(x + width, rect.x + rect.width);
double maxY = Math.max(y + height, rect.y + rect.height);
x = minX;
y = minY;
width = maxX - minX;
height = maxY - minY;
}
}
/**
* Returns the x-coordinate of the center.
*
* @return Returns the x-coordinate of the center.
*/
public double getCenterX()
{
return getX() + getWidth() / 2;
}
/**
* Returns the y-coordinate of the center.
*
* @return Returns the y-coordinate of the center.
*/
public double getCenterY()
{
return getY() + getHeight() / 2;
}
/**
* Grows the rectangle by the given amount, that is, this method subtracts
* the given amount from the x- and y-coordinates and adds twice the amount
* to the width and height.
*
* @param amount Amount by which the rectangle should be grown.
*/
public void grow(double amount)
{
x -= amount;
y -= amount;
width += 2 * amount;
height += 2 * amount;
}
/**
* Returns true if the given point is contained in the rectangle.
*
* @param x X-coordinate of the point.
* @param y Y-coordinate of the point.
* @return Returns true if the point is contained in the rectangle.
*/
public boolean contains(double x, double y)
{
return (this.x <= x && this.x + width >= x && this.y <= y && this.y
+ height >= y);
}
/**
* Returns the point at which the specified point intersects the perimeter
* of this rectangle or null if there is no intersection.
*
* @param x0 the x co-ordinate of the first point of the line
* @param y0 the y co-ordinate of the first point of the line
* @param x1 the x co-ordinate of the second point of the line
* @param y1 the y co-ordinate of the second point of the line
* @return the point at which the line intersects this rectangle, or null
* if there is no intersection
*/
public mxPoint intersectLine(double x0, double y0, double x1, double y1)
{
mxPoint result = null;
result = mxUtils.intersection(x, y, x + width, y, x0, y0, x1, y1);
if (result == null)
{
result = mxUtils.intersection(x + width, y, x + width, y + height,
x0, y0, x1, y1);
}
if (result == null)
{
result = mxUtils.intersection(x + width, y + height, x, y + height,
x0, y0, x1, y1);
}
if (result == null)
{
result = mxUtils.intersection(x, y, x, y + height, x0, y0, x1, y1);
}
return result;
}
/**
* Returns the bounds as a new rectangle.
*
* @return Returns a new rectangle for the bounds.
*/
public Rectangle getRectangle()
{
int ix = (int) Math.round(x);
int iy = (int) Math.round(y);
int iw = (int) Math.round(width - ix + x);
int ih = (int) Math.round(height - iy + y);
return new Rectangle(ix, iy, iw, ih);
}
/**
* Rotates this rectangle by 90 degree around its center point.
*/
public void rotate90()
{
double t = (this.width - this.height) / 2;
this.x += t;
this.y -= t;
double tmp = this.width;
this.width = this.height;
this.height = tmp;
};
/**
* Returns true if the given object equals this rectangle.
*/
public boolean equals(Object obj)
{
if (obj instanceof mxRectangle)
{
mxRectangle rect = (mxRectangle) obj;
return rect.getX() == getX() && rect.getY() == getY()
&& rect.getWidth() == getWidth()
&& rect.getHeight() == getHeight();
}
return false;
}
/**
* Returns a new instance of the same rectangle.
*/
public Object clone()
{
mxRectangle clone = (mxRectangle) super.clone();
clone.setWidth(getWidth());
clone.setHeight(getHeight());
return clone;
}
/**
* Returns the <code>String</code> representation of this
* <code>mxRectangle</code>.
* @return a <code>String</code> representing this
* <code>mxRectangle</code>.
*/
@Override
public String toString()
{
StringBuilder builder = new StringBuilder(32);
builder.append(getClass().getSimpleName());
builder.append(" [");
builder.append("x=");
builder.append(x);
builder.append(", y=");
builder.append(y);
builder.append(", width=");
builder.append(width);
builder.append(", height=");
builder.append(height);
builder.append("]");
return builder.toString();
}
}