-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathRect.java
More file actions
164 lines (138 loc) · 4.46 KB
/
Rect.java
File metadata and controls
164 lines (138 loc) · 4.46 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
/*
* This file is part of NodeBox.
*
* Copyright (C) 2008 Frederik De Bleser (frederik@pandora.be)
*
* NodeBox is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NodeBox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NodeBox. If not, see <http://www.gnu.org/licenses/>.
*/
package nodebox.graphics;
import java.awt.geom.Rectangle2D;
import java.util.*;
public class Rect implements Iterable {
private float x, y, width, height;
public Rect() {
this(0, 0, 0, 0);
}
public Rect(float x, float y, float width, float height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public Rect(Rect r) {
this.x = r.x;
this.y = r.y;
this.width = r.width;
this.height = r.height;
}
public Rect(java.awt.geom.Rectangle2D r) {
this.x = (float) r.getX();
this.y = (float) r.getY();
this.width = (float) r.getWidth();
this.height = (float) r.getHeight();
}
public static Rect centeredRect(float cx, float cy, float width, float height) {
return new Rect(cx - width / 2, cy - height / 2, width, height);
}
public static Rect centeredRect(Rect r) {
return centeredRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
}
public static Rect corneredRect(float cx, float cy, float width, float height) {
return new Rect(cx + width / 2, cy + height / 2, width, height);
}
public static Rect corneredRect(Rect r) {
return corneredRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
}
public float getHeight() {
return height;
}
public float getWidth() {
return width;
}
public float getX() {
return x;
}
public float getY() {
return y;
}
public boolean isEmpty() {
Rect n = normalized();
return n.width <= 0 || n.height <= 0;
}
public Rect normalized() {
Rect r = new Rect(this);
if (r.width < 0) {
r.x += r.width;
r.width = -r.width;
}
if (r.height < 0) {
r.y += r.height;
r.height = -r.height;
}
return r;
}
public Rect united(Rect r) {
Rect r1 = normalized();
Rect r2 = r.normalized();
Rect u = new Rect();
u.x = Math.min(r1.x, r2.x);
u.y = Math.min(r1.y, r2.y);
u.width = Math.max(r1.x + r1.width, r2.x + r2.width) - u.x;
u.height = Math.max(r1.y + r1.height, r2.y + r2.height) - u.y;
return u;
}
public boolean intersects(Rect r) {
Rect r1 = normalized();
Rect r2 = r.normalized();
return Math.max(r1.x, r1.y) < Math.min(r1.x + r1.width, r2.width) &&
Math.max(r1.y, r2.y) < Math.min(r1.y + r1.height, r2.y + r2.height);
}
public boolean contains(Point p) {
Rect r = normalized();
return p.getX() >= r.x && p.getX() <= r.x + r.width &&
p.getY() >= r.y && p.getY() <= r.y + r.height;
}
public boolean contains(Rect r) {
Rect r1 = normalized();
Rect r2 = r.normalized();
return r2.x >= r1.x && r2.x + r2.width <= r1.x + r1.width &&
r2.y >= r1.y && r2.y + r2.height <= r1.y + r1.height;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Rect)) return false;
Rect r = (Rect) o;
return x == r.x && y == r.y && width == r.width && height == r.height;
}
@Override
public String toString() {
return "Rect(" + x + ", " + y + ", " + width + ", " + height + ")";
}
public Rectangle2D getRectangle2D() {
return new Rectangle2D.Float(x, y, width, height);
}
public Iterator<Float> iterator() {
List<Float> list = new ArrayList<Float>();
list.add(x);
list.add(y);
list.add(width);
list.add(height);
return list.iterator();
}
@Override
public Rect clone() {
return new Rect(this);
}
}