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