source: Include/Classes/System/Drawing/Rectangle.ab@ 212

Last change on this file since 212 was 212, checked in by イグトランス (egtra), 17 years ago

Object.ReferenceEqualsを追加

File size: 5.6 KB
Line 
1' Classes/System/Drawing/Rectangle.ab
2
3#ifndef __SYSTEM_DRAWING_RECTANGLE_AB__
4#define __SYSTEM_DRAWING_RECTANGLE_AB__
5
6#include <Classes/System/Math.ab>
7#include <Classes/System/Drawing/RectangleF.ab>
8#include <Classes/System/Drawing/Point.ab>
9#include <Classes/System/Drawing/Size.ab>
10
11Class Rectangle
12Public
13 Sub Rectangle()
14 x = 0
15 y = 0
16 width = 0
17 height = 0
18 End Sub
19
20 Sub Rectangle(x As Long, y As Long, width As Long, height As Long)
21 x = x
22 y = y
23 width = width
24 height = height
25 End Sub
26
27 Sub Rectangle(l As Point, s As Size)
28 x = l.X
29 y = l.Y
30 width = s.Height
31 height = s.Height
32 End Sub
33
34 Sub Rectangle(ByRef r As Rectangle)
35 x = r.x
36 y = r.y
37 width = r.width
38 height = r.height
39 End Sub
40
41 Sub Rectangle(ByRef r As RECT)
42 This = FromLTRB(r.left, r.top, r.right, r.bottom)
43 End Sub
44
45 Function Location() As Point
46 Dim pt As Point(x, y)
47 Return pt
48 End Function
49
50 Sub Location(point As Point)
51 x = point.X
52 y = point.Y
53 End Sub
54
55 Function Size() As Size
56 Dim size As Size(width, height)
57 End Function
58
59 Sub Size(size As Size)
60 width = size.Width
61 height = size.Height
62 End Sub
63
64 Function X() As Long
65 Return x
66 End Function
67
68 Sub X(value As Long)
69 x = value
70 End Sub
71
72 Function Y() As Long
73 Return y
74 End Function
75
76 Sub Y(value As Long)
77 y = value
78 End Sub
79
80 Function Width() As Long
81 Return width
82 End Function
83
84 Sub Width(value As Long)
85 width = value
86 End Sub
87
88 Function Height() As Long
89 Return height
90 End Function
91
92 Sub Height(value As Long)
93 height = value
94 End Sub
95
96 Function Left() As Long
97 Return X
98 End Function
99
100 Function Top() As Long
101 Return Y
102 End Function
103
104 Function Right() As Long
105 Return X + Width
106 End Function
107
108 Function Bottom() As Long
109 Return Y + Height
110 End Function
111
112 Function IsEmpty() As Boolean
113 If Width <= 0 Or Height <= 0 Then
114 IsEmpty = _System_TRUE
115 Else
116 IsEmpty = _System_FALSE
117 End If
118 End Function
119/*
120 Function Operator = (rc As Rectangle)
121 With rc
122 x = .x
123 y = .y
124 width = .width
125 height = .height
126 End With
127 End Function
128*/
129 Function Operator == (rc As Rectangle)
130 Return Equals(rc)
131 End Function
132
133 Function Operator <> (rc As Rectangle)
134 Return Not Equals(rc)
135 End Function
136
137 Function Operator () As RectangleF
138 Return New RectangleF(x, y, width, height)
139 End Function
140
141 Function Equals(rc As Rectangle) As Boolean
142 If X = rc.X And Y = rc.Y And Width = rc.Width And Height = rc.Height Then
143 Return True
144 Else
145 Return False
146 End If
147 End Function
148
149 Override Function GetHashCode() As Long
150 Return x Xor _System_BSwap(y) Xor width Xor _System_BSwap(height)
151 End Function
152
153 Static Function FromLTRB(l As Long, t As Long, r As Long, b As Long) As Rectangle
154 return New Rectangle(l, t, r - l, r - b)
155 End Function
156
157 Function Contains(x As Long, y As Long) As Boolean
158 If x >= X And x < X + Width And y >= Y And y < Y + Height Then
159 Return True
160 Else
161 Return False
162 End If
163 End Function
164
165 Function Contains(pt As Point) As Boolean
166 Return Contains(pt.X, pt.Y)
167 End Function
168
169 Function Contains(rc As Rectangle) As Boolean
170 If X <= rc.X And rc.Right <= Right And Y <= rc.Y And rc.Bottom <= Bottom Then
171 Return True
172 Else
173 Return False
174 End If
175 End Function
176
177 Sub Inflate(dx As Long, dy As Long)
178 X -= dx
179 Y -= dy
180 Width += dx + dx
181 Height += dy + dy
182 End Sub
183
184 Sub Inflate(sz As Size)
185 Inflate(sz.Width, sz.Height)
186 End Sub
187
188 Static Function Inflate(rc As Rectangle, x As Long, y As Long) As Rectangle
189 Inflate = New Rectangle(rc)
190 Inflate.Inflate(x, y)
191 End Function
192
193 Sub Intersect(rect As Rectangle)
194 This = Rectangle.Intersect(This, rect)
195 End Sub
196
197 Static Function Intersect(a As Rectangle, ByRef b As Rectangle) As Rectangle
198 Dim right As Long, bottom As Long, left As Long, top As Long
199 right = Math.Min(a.Right, b.Right)
200 bottom = Math.Min(a.Bottom, b.Bottom)
201 left = Math.Min(a.Left, b.Left)
202 top = Math.Min(a.Top, b.Top)
203 Return Rectangle.FromLTRB(left, top, right, bottom)
204 End Function
205
206 Function IntersectsWith(rc As Rectangle) As Boolean
207 If Left < rc.Right And _
208 Top < rc.Bottom And _
209 Right > rc.Left And _
210 Bottom > rc.Top Then
211 Return True
212 Else
213 Return False
214 End If
215 End Function
216
217 Static Function Union(a As Rectangle, b As Rectangle) As Rectangle
218 Dim right As Long, bottom As Long, left As Long, top As Long
219 right = Math.Max(a.Right(), b.Right())
220 bottom = Math.Max(a.Bottom(), b.Bottom())
221 left = Math.Max(a.Left(), b.Left())
222 top = Math.Max(a.Top(), b.Top())
223 Return FromLTRB(left, top, right, bottom)
224 End Function
225
226 Sub Offset(pt As Point)
227 Offset(pt.X, pt.Y)
228 End Sub
229
230 Sub Offset(dx As Long, dy As Long)
231 X += dx
232 Y += dy
233 End Sub
234
235 Static Function Ceiling(rcf As RectangleF) As Rectangle
236 Dim r As Rectangle(
237 Math.Ceiling(rcf.X),
238 Math.Ceiling(rcf.Y),
239 Math.Ceiling(rcf.Width),
240 Math.Ceiling(rcf.Height))
241 Return r
242 End Function
243
244 Static Function Round(rcf As RectangleF) As Rectangle
245 Dim r As Rectangle(
246 Math.Round(rcf.X),
247 Math.Round(rcf.Y),
248 Math.Round(rcf.Width),
249 Math.Round(rcf.Height))
250 Return r
251 End Function
252
253 Static Function Truncate(rcf As RectangleF) As Rectangle
254 Dim r As Rectangle(
255 Math.Truncate(rcf.X),
256 Math.Truncate(rcf.Y),
257 Math.Truncate(rcf.Width),
258 Math.Truncate(rcf.Height))
259 Return r
260 End Function
261
262 Function ToRECT() As RECT
263 With ToRECT
264 .left = x
265 .top = y
266 .right = x + width
267 .bottom = y + height
268 End With
269 End Function
270
271Public
272 x As Long
273 y As Long
274 width As Long
275 height As Long
276End Class
277
278#endif '__SYSTEM_DRAWING_RECTFANGLE_AB__
Note: See TracBrowser for help on using the repository browser.