| 1 |
{ |
| 2 |
Copyright 2006-2022 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Rectangle representation (TRectangle, TFloatRectangle). } |
| 17 |
unit CastleRectangles; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses Generics.Collections, Classes, |
| 24 |
CastleVectors, CastleUtils, CastleClassUtils; |
| 25 |
|
| 26 |
type |
| 27 |
{ Horizontal position of one control/rectangle |
| 28 |
with respect to another. |
| 29 |
|
| 30 |
This is used by @link(TCastleUserInterface.Anchor), |
| 31 |
@link(TRectangle.Align), @link(TFloatRectangle.Align) and other methods |
| 32 |
to specify the alignment of one control/rectangle with respect to another. |
| 33 |
|
| 34 |
Note that @link(TCastleUserInterface.Anchor) has various overloaded |
| 35 |
versions. E.g. you can align the left side of the control to the left side |
| 36 |
of the parent (most common situation), or you can align left side |
| 37 |
of the control to the middle of the parent... |
| 38 |
|
| 39 |
@seealso TVerticalPosition |
| 40 |
} |
| 41 |
THorizontalPosition = ( |
| 42 |
hpLeft, |
| 43 |
hpMiddle, |
| 44 |
hpRight |
| 45 |
); |
| 46 |
|
| 47 |
{ Vertical position of one control/rectangle with respect to another. |
| 48 |
@seealso THorizontalPosition } |
| 49 |
TVerticalPosition = ( |
| 50 |
vpBottom, |
| 51 |
vpMiddle, |
| 52 |
vpTop |
| 53 |
); |
| 54 |
|
| 55 |
{ 2D rectangle with @bold(integer) coordinates. |
| 56 |
Useful for various 2D GUI operations. |
| 57 |
|
| 58 |
The area covered by the rectangle starts in (Left,Bottom) |
| 59 |
pixel and spans (Width,Height) pixels. This means that the right-top pixel |
| 60 |
covered by the rectangle is (Left + Width - 1,Bottom + Height - 1). |
| 61 |
The rectangle is empty (@link(Contains) will always answer @false) |
| 62 |
when either Width or Height are zero. Neither Width nor Height can ever |
| 63 |
be negative. } |
| 64 |
TRectangle = record |
| 65 |
private |
| 66 |
function GetRight: Integer; |
| 67 |
function GetTop: Integer; |
| 68 |
{ |
| 69 |
// The setters for Right and Top are deliberately commented out. |
| 70 |
// They are a little tricky, as they would actually set Width / Height. |
| 71 |
// - So setting "Left := 10; Right := 20;" works as expected, |
| 72 |
// but setting "Right := 20; Left := 10;"... does not (it depends |
| 73 |
// on previous rectangle value). |
| 74 |
// - Also, they would be necessarily inconsistent between int TRectangle |
| 75 |
// and float TFloatRectangle, since int TRectangle does not allow |
| 76 |
// negative Width / Height. |
| 77 |
// So it's probably less error-prone to leave them commented out. |
| 78 |
procedure SetRight(const Value: Integer); |
| 79 |
procedure SetTop(const Value: Integer); |
| 80 |
} |
| 81 |
function GetLeftBottom: TVector2Integer; |
| 82 |
procedure SetLeftBottom(const Value: TVector2Integer); |
| 83 |
public |
| 84 |
Left, Bottom: Integer; |
| 85 |
Width, Height: Cardinal; |
| 86 |
|
| 87 |
class function Empty: TRectangle; static; inline; |
| 88 |
|
| 89 |
function IsEmpty: Boolean; |
| 90 |
|
| 91 |
function Contains(const X, Y: Integer): Boolean; overload; |
| 92 |
function Contains(const Point: TVector2): Boolean; overload; |
| 93 |
function Contains(const Point: TVector2Integer): Boolean; overload; |
| 94 |
|
| 95 |
{ Right and top coordinates of the rectangle. |
| 96 |
@code(Right) is simply the @code(Left + Width), |
| 97 |
@code(Top) is simply the @code(Bottom + Height). |
| 98 |
|
| 99 |
If you use this for drawing, note that the pixel |
| 100 |
with coordinates @code((Right, Top)) is actually *outside* |
| 101 |
of the rectangle (by 1 pixel). That's because the rectangle starts at |
| 102 |
pixel @code((Left, Bottom)) and spans the @code((Width, Height)) pixels. |
| 103 |
@groupBegin } |
| 104 |
property Right: Integer read GetRight; |
| 105 |
property Top: Integer read GetTop; |
| 106 |
{ @groupEnd } |
| 107 |
|
| 108 |
{ Return rectangle with given width and height centered |
| 109 |
in the middle of this rectangle. The given W, H may be smaller or larger |
| 110 |
than this rectangle sizes. } |
| 111 |
function CenterInside(const W, H: Cardinal): TRectangle; |
| 112 |
|
| 113 |
{ Grow (when Delta > 0) or shrink (when Delta < 0) |
| 114 |
the rectangle, returning new value. |
| 115 |
This adds a margin of Delta pixels around all sides of the rectangle, |
| 116 |
so in total width grows by 2 * Delta, and the same for height. |
| 117 |
In case of shrinking, we protect from shrinking too much: |
| 118 |
the resulting width or height is set to zero (which makes a valid |
| 119 |
and empty rectangle) if shrinking too much. } |
| 120 |
function Grow(const Delta: Integer): TRectangle; overload; |
| 121 |
function Grow(const DeltaX, DeltaY: Integer): TRectangle; overload; |
| 122 |
|
| 123 |
{ Returns the rectangle with a number of pixels from given |
| 124 |
side removed. Returns an empty rectangle if you try to remove too much. |
| 125 |
@groupBegin } |
| 126 |
function RemoveLeft(W: Cardinal): TRectangle; |
| 127 |
function RemoveBottom(H: Cardinal): TRectangle; |
| 128 |
function RemoveRight(W: Cardinal): TRectangle; |
| 129 |
function RemoveTop(H: Cardinal): TRectangle; |
| 130 |
{ @groupEnd } |
| 131 |
|
| 132 |
{ Returns the rectangle with a number of pixels on given |
| 133 |
side added. |
| 134 |
@groupBegin } |
| 135 |
function GrowLeft(const W: Cardinal): TRectangle; |
| 136 |
function GrowBottom(const H: Cardinal): TRectangle; |
| 137 |
function GrowRight(const W: Cardinal): TRectangle; |
| 138 |
function GrowTop(const H: Cardinal): TRectangle; |
| 139 |
{ @groupEnd } |
| 140 |
|
| 141 |
{ Returns the given side of the rectangle, cut down to given number of pixels |
| 142 |
from given side. This is similar to RemoveXxx methods, but here you specify |
| 143 |
which side to keep, as opposed to RemoveXxx methods where you specify which |
| 144 |
side you remove. |
| 145 |
|
| 146 |
If the requested size is larger than current size (for example, |
| 147 |
W > Width for LeftPart) then the unmodified rectangle is returned. |
| 148 |
|
| 149 |
@groupBegin } |
| 150 |
function LeftPart(W: Cardinal): TRectangle; |
| 151 |
function BottomPart(H: Cardinal): TRectangle; |
| 152 |
function RightPart(W: Cardinal): TRectangle; |
| 153 |
function TopPart(H: Cardinal): TRectangle; |
| 154 |
{ @groupEnd } |
| 155 |
|
| 156 |
property LeftBottom: TVector2Integer read GetLeftBottom write SetLeftBottom; |
| 157 |
function Middle: TVector2Integer; deprecated 'use Center'; |
| 158 |
function Center: TVector2Integer; |
| 159 |
|
| 160 |
{ Clamp value to be within allowed horizontal range. |
| 161 |
That is, clamp to @code([Left, Right - 1]). } |
| 162 |
function ClampX(const X: Integer): Integer; |
| 163 |
|
| 164 |
{ Clamp value to be within allowed vertical range. |
| 165 |
That is, clamp to @code([Bottom, Top - 1]). } |
| 166 |
function ClampY(const Y: Integer): Integer; |
| 167 |
|
| 168 |
function ScaleToWidth(const NewWidth: Cardinal): TRectangle; |
| 169 |
function ScaleToHeight(const NewHeight: Cardinal): TRectangle; |
| 170 |
|
| 171 |
{ Scale rectangle position and size around it's own @link(Center) point. |
| 172 |
|
| 173 |
Since the scaling is independent in each axis, |
| 174 |
this handles "carefully" a half-empty rectangles |
| 175 |
(when one size is <= 0, but other is > 0). |
| 176 |
It scales correctly the positive dimension |
| 177 |
(not just returns @link(Empty) constant), |
| 178 |
leaving the other dimension (it's position and size) untouched. } |
| 179 |
function ScaleAroundCenter(const Factor: Single): TRectangle; |
| 180 |
function ScaleAroundMiddle(const Factor: Single): TRectangle; |
| 181 |
deprecated 'use ScaleAroundCenter'; |
| 182 |
|
| 183 |
{ Scale rectangle position and size around the (0,0) point. |
| 184 |
|
| 185 |
Since the scaling is independent in each axis, |
| 186 |
this handles "carefully" a half-empty rectangles |
| 187 |
(when one size is <= 0, but other is > 0). |
| 188 |
It scales correctly the positive dimension |
| 189 |
(not just returns @link(Empty) constant), |
| 190 |
leaving the other dimension (it's position and size) untouched. |
| 191 |
|
| 192 |
These details matter, e.g. when you set @link(TCastleUserInterface.Width), but not |
| 193 |
@link(TCastleUserInterface.Height), |
| 194 |
and then you expect the @link(TCastleUserInterface.EffectiveWidth) to work. |
| 195 |
} |
| 196 |
function ScaleAround0(const Factor: Single): TRectangle; |
| 197 |
|
| 198 |
{ Scale @link(Width), in the same manner as ScaleAround0 would do. } |
| 199 |
function ScaleWidthAround0(const Factor: Single): Cardinal; |
| 200 |
{ Scale @link(Height), in the same manner as ScaleAround0 would do. } |
| 201 |
function ScaleHeightAround0(const Factor: Single): Cardinal; |
| 202 |
|
| 203 |
{ Scale and align us to fit inside rectangle R, preserving our aspect ratio. } |
| 204 |
function FitInside(const R: TRectangle; |
| 205 |
const AlignHorizontal: THorizontalPosition = hpMiddle; |
| 206 |
const AlignVertical: TVerticalPosition = vpMiddle): TRectangle; |
| 207 |
|
| 208 |
{ Align this rectangle within other rectangle by calculating new value |
| 209 |
for @link(Left). } |
| 210 |
function AlignCore( |
| 211 |
const ThisPosition: THorizontalPosition; |
| 212 |
const OtherRect: TRectangle; |
| 213 |
const OtherPosition: THorizontalPosition; |
| 214 |
const X: Integer = 0): Integer; overload; |
| 215 |
function Align( |
| 216 |
const ThisPosition: THorizontalPosition; |
| 217 |
const OtherRect: TRectangle; |
| 218 |
const OtherPosition: THorizontalPosition; |
| 219 |
const X: Integer = 0): TRectangle; overload; |
| 220 |
|
| 221 |
{ Align this rectangle within other rectangle by calculating new value |
| 222 |
for @link(Bottom). } |
| 223 |
function AlignCore( |
| 224 |
const ThisPosition: TVerticalPosition; |
| 225 |
const OtherRect: TRectangle; |
| 226 |
const OtherPosition: TVerticalPosition; |
| 227 |
const Y: Integer = 0): Integer; overload; |
| 228 |
function Align( |
| 229 |
const ThisPosition: TVerticalPosition; |
| 230 |
const OtherRect: TRectangle; |
| 231 |
const OtherPosition: TVerticalPosition; |
| 232 |
const Y: Integer = 0): TRectangle; overload; |
| 233 |
|
| 234 |
function ToString: string; |
| 235 |
|
| 236 |
{ Move the rectangle. Empty rectangle after moving is still an empty rectangle. } |
| 237 |
function Translate(const V: TVector2Integer): TRectangle; |
| 238 |
|
| 239 |
{ Does it have any common part with another rectangle. } |
| 240 |
function Collides(const R: TRectangle): Boolean; |
| 241 |
|
| 242 |
{ Sum of the two rectangles is a bounding rectangle - |
| 243 |
a smallest rectangle that contains them both. } |
| 244 |
class operator {$ifdef FPC}+{$else}Add{$endif} (const R1, R2: TRectangle): TRectangle; |
| 245 |
|
| 246 |
{ Common part of the two rectangles. } |
| 247 |
class operator {$ifdef FPC}*{$else}Multiply{$endif} (const R1, R2: TRectangle): TRectangle; |
| 248 |
|
| 249 |
function Equals(const R: TRectangle): Boolean; |
| 250 |
end; |
| 251 |
|
| 252 |
{ 2D rectangle with @bold(float) coordinates. |
| 253 |
Useful for various 2D GUI operations, and for bounding boxes for 2D objects. |
| 254 |
|
| 255 |
The area covered by the rectangle starts at (Left,Bottom) position |
| 256 |
and spans (Width,Height) units. |
| 257 |
The rectangle is empty (@link(Contains) will always answer @false) |
| 258 |
when either Width or Height are less than zero. |
| 259 |
@bold(This is consistent with it's 3D equivalent, @link(TBox3D), |
| 260 |
and different from it's integer counterpart @link(TRectangle).) |
| 261 |
In case of float bounding box (@link(TBox3D)) or float rectangle |
| 262 |
(@name), having a zero size makes sense, and it still is something non-empty |
| 263 |
(a single 2D or 3D point has zero size, but it also still has a position). } |
| 264 |
TFloatRectangle = record |
| 265 |
private |
| 266 |
function GetRight: Single; |
| 267 |
function GetTop: Single; |
| 268 |
{ |
| 269 |
// Commented out -- see TRectangle comments. |
| 270 |
procedure SetRight(const Value: Single); |
| 271 |
procedure SetTop(const Value: Single); |
| 272 |
} |
| 273 |
function GetLeftBottom: TVector2; |
| 274 |
procedure SetLeftBottom(const Value: TVector2); |
| 275 |
function GetSize: TVector2; |
| 276 |
procedure SetSize(const Value: TVector2); |
| 277 |
public |
| 278 |
Left, Bottom: Single; |
| 279 |
Width, Height: Single; |
| 280 |
|
| 281 |
class function Empty: TFloatRectangle; static; inline; |
| 282 |
|
| 283 |
function IsEmpty: Boolean; |
| 284 |
|
| 285 |
function Contains(const X, Y: Single): Boolean; overload; |
| 286 |
function Contains(const Point: TVector2): Boolean; overload; |
| 287 |
function Contains(const R: TFloatRectangle): Boolean; overload; |
| 288 |
|
| 289 |
{ Right and top coordinates of the rectangle. |
| 290 |
@code(Right) is simply the @code(Left + Width), |
| 291 |
@code(Top) is simply the @code(Bottom + Height). |
| 292 |
|
| 293 |
Note: If you use this for drawing, |
| 294 |
and the values of Left, Bottom, Width, Height |
| 295 |
are actually integers (or close to integers), |
| 296 |
then the pixel with @code((Round(Right), Round(Top))) |
| 297 |
coordinates is actually *outside* of the rectangle (by 1 pixel). |
| 298 |
That's because the rectangle starts at the pixel |
| 299 |
@code((Round(Left), Round(Bottom))) and |
| 300 |
spans the @code((Round(Width), Round(Height))) pixels. |
| 301 |
@groupBegin } |
| 302 |
property Right: Single read GetRight; |
| 303 |
property Top: Single read GetTop; |
| 304 |
{ @groupEnd } |
| 305 |
|
| 306 |
property LeftBottom: TVector2 read GetLeftBottom write SetLeftBottom; |
| 307 |
function Middle: TVector2; deprecated 'use Center'; |
| 308 |
function Center: TVector2; |
| 309 |
|
| 310 |
{ Return rectangle with given width and height centered |
| 311 |
in the middle of this rectangle. The given W, H may be smaller or larger |
| 312 |
than this rectangle sizes. } |
| 313 |
function CenterInside(const W, H: Single): TFloatRectangle; |
| 314 |
|
| 315 |
{ Grow (when Delta > 0) or shrink (when Delta < 0) |
| 316 |
the rectangle, returning new value. |
| 317 |
This adds a margin of Delta pixels around all sides of the rectangle, |
| 318 |
so in total width grows by 2 * Delta, and the same for height. |
| 319 |
In case of shrinking, we protect from shrinking too much: |
| 320 |
the resulting width or height is set to zero (which makes a valid |
| 321 |
and empty rectangle) if shrinking too much. } |
| 322 |
function Grow(const Delta: Single): TFloatRectangle; overload; |
| 323 |
function Grow(const DeltaX, DeltaY: Single): TFloatRectangle; overload; |
| 324 |
|
| 325 |
{ Returns the rectangle with a number of pixels from given |
| 326 |
side removed. Returns an empty rectangle if you try to remove too much. |
| 327 |
@groupBegin } |
| 328 |
function RemoveLeft(W: Single): TFloatRectangle; |
| 329 |
function RemoveBottom(H: Single): TFloatRectangle; |
| 330 |
function RemoveRight(W: Single): TFloatRectangle; |
| 331 |
function RemoveTop(H: Single): TFloatRectangle; |
| 332 |
{ @groupEnd } |
| 333 |
|
| 334 |
{ Returns the rectangle with a number of pixels on given side added. |
| 335 |
@groupBegin } |
| 336 |
function GrowLeft(const W: Single): TFloatRectangle; |
| 337 |
function GrowBottom(const H: Single): TFloatRectangle; |
| 338 |
function GrowRight(const W: Single): TFloatRectangle; |
| 339 |
function GrowTop(const H: Single): TFloatRectangle; |
| 340 |
{ @groupEnd } |
| 341 |
|
| 342 |
{ Returns the given side of the rectangle, cut down to given number of pixels |
| 343 |
from given side. This is similar to RemoveXxx methods, but here you specify |
| 344 |
which side to keep, as opposed to RemoveXxx methods where you specify which |
| 345 |
side you remove. |
| 346 |
|
| 347 |
If the requested size is larger than current size (for example, |
| 348 |
W > Width for LeftPart) then the unmodified rectangle is returned. |
| 349 |
|
| 350 |
@groupBegin } |
| 351 |
function LeftPart(W: Single): TFloatRectangle; |
| 352 |
function BottomPart(H: Single): TFloatRectangle; |
| 353 |
function RightPart(W: Single): TFloatRectangle; |
| 354 |
function TopPart(H: Single): TFloatRectangle; |
| 355 |
{ @groupEnd } |
| 356 |
|
| 357 |
{ Align this rectangle within other rectangle by calculating new value |
| 358 |
for @link(Left). } |
| 359 |
function AlignCore( |
| 360 |
const ThisPosition: THorizontalPosition; |
| 361 |
const OtherRect: TFloatRectangle; |
| 362 |
const OtherPosition: THorizontalPosition; |
| 363 |
const X: Single = 0): Single; overload; |
| 364 |
function Align( |
| 365 |
const ThisPosition: THorizontalPosition; |
| 366 |
const OtherRect: TFloatRectangle; |
| 367 |
const OtherPosition: THorizontalPosition; |
| 368 |
const X: Single = 0): TFloatRectangle; overload; |
| 369 |
|
| 370 |
{ Align this rectangle within other rectangle by calculating new value |
| 371 |
for @link(Bottom). } |
| 372 |
function AlignCore( |
| 373 |
const ThisPosition: TVerticalPosition; |
| 374 |
const OtherRect: TFloatRectangle; |
| 375 |
const OtherPosition: TVerticalPosition; |
| 376 |
const Y: Single = 0): Single; overload; |
| 377 |
function Align( |
| 378 |
const ThisPosition: TVerticalPosition; |
| 379 |
const OtherRect: TFloatRectangle; |
| 380 |
const OtherPosition: TVerticalPosition; |
| 381 |
const Y: Single = 0): TFloatRectangle; overload; |
| 382 |
|
| 383 |
function ToString: string; |
| 384 |
|
| 385 |
{ Move the rectangle. Empty rectangle after moving is still an empty rectangle. } |
| 386 |
function Translate(const V: TVector2): TFloatRectangle; |
| 387 |
|
| 388 |
{ Does it have any common part with another rectangle. } |
| 389 |
function Collides(const R: TFloatRectangle): Boolean; |
| 390 |
|
| 391 |
function CollidesDisc(const DiscCenter: TVector2; const Radius: Single): Boolean; |
| 392 |
|
| 393 |
function ScaleToWidth(const NewWidth: Single): TFloatRectangle; |
| 394 |
function ScaleToHeight(const NewHeight: Single): TFloatRectangle; |
| 395 |
|
| 396 |
{ Scale rectangle position and size around it's own @link(Center) point. |
| 397 |
|
| 398 |
Since the scaling is independent in each axis, |
| 399 |
this handles "carefully" a half-empty rectangles |
| 400 |
(when one size is <= 0, but other is > 0). |
| 401 |
It scales correctly the positive dimension |
| 402 |
(not just returns @link(Empty) constant), |
| 403 |
leaving the other dimension (it's position and size) untouched. } |
| 404 |
function ScaleAroundCenter(const Factor: Single): TFloatRectangle; |
| 405 |
|
| 406 |
{ Scale rectangle position and size around the (0,0) point. } |
| 407 |
function ScaleAround0(const Factor: Single): TFloatRectangle; |
| 408 |
|
| 409 |
{ Scale and align us to fit inside rectangle R, preserving our aspect ratio. } |
| 410 |
function FitInside(const R: TFloatRectangle; |
| 411 |
const AlignHorizontal: THorizontalPosition = hpMiddle; |
| 412 |
const AlignVertical: TVerticalPosition = vpMiddle): TFloatRectangle; |
| 413 |
|
| 414 |
{ Return larger rectangle, so that it includes given point. } |
| 415 |
function Include(const P: TVector2): TFloatRectangle; |
| 416 |
|
| 417 |
{ Convert to a 4D vector, like expected by X3D fields |
| 418 |
OrthoViewpoint.fieldOfView or DirectionalLight.projectionRectangle. } |
| 419 |
function ToX3DVector: TVector4; |
| 420 |
|
| 421 |
{ Convert from a 4D vector, like expected by X3D fields |
| 422 |
OrthoViewpoint.fieldOfView or DirectionalLight.projectionRectangle. } |
| 423 |
class function FromX3DVector(const V: TVector4): TFloatRectangle; static; |
| 424 |
|
| 425 |
{ Round rectangle coordinates, converting TFloatRectangle to TRectangle. } |
| 426 |
function Round: TRectangle; |
| 427 |
|
| 428 |
{ Is another rectangle equal to this one. |
| 429 |
Floating-point values are compared with an epsilon tolerance. } |
| 430 |
function Equals(const R: TFloatRectangle): Boolean; overload; |
| 431 |
|
| 432 |
{ Is another rectangle equal to this one. |
| 433 |
Floating-point values are compared with an epsilon tolerance. } |
| 434 |
function Equals(const R: TFloatRectangle; const Epsilon: Single): Boolean; overload; |
| 435 |
|
| 436 |
{ Compare using exact comparison (without any epsilon to tolerate small float differences). } |
| 437 |
function PerfectlyEquals(const R: TFloatRectangle): Boolean; |
| 438 |
|
| 439 |
{ Sum of the two rectangles is a bounding rectangle - |
| 440 |
a smallest rectangle that contains them both. } |
| 441 |
class operator {$ifdef FPC}+{$else}Add{$endif} (const R1, R2: TFloatRectangle): TFloatRectangle; |
| 442 |
|
| 443 |
{ Common part of the two rectangles. } |
| 444 |
class operator {$ifdef FPC}*{$else}Multiply{$endif} (const R1, R2: TFloatRectangle): TFloatRectangle; |
| 445 |
|
| 446 |
{ Alternative way to access @link(Width) and @link(Height). |
| 447 |
Name consistent with TBoxNode.Size, TBox3D.Size. } |
| 448 |
property Size: TVector2 read GetSize write SetSize; |
| 449 |
|
| 450 |
{ Add 4 corners of this rectangle to the list, in CCW order, starting from left-bottom. } |
| 451 |
procedure AddToCoords(const Coords: TVector2List); overload; |
| 452 |
|
| 453 |
{ Add 4 corners of this rectangle to the list, in CCW order, starting from left-bottom. |
| 454 |
Set the Z coordinate of all added (3D) points to the one you specify by the Z parameter. } |
| 455 |
procedure AddToCoords(const Coords: TVector3List; const Z: Single); overload; |
| 456 |
end; |
| 457 |
|
| 458 |
PFloatRectangle = ^TFloatRectangle; |
| 459 |
TFloatRectangleArray = packed array [0..MaxInt div SizeOf(TFloatRectangle) - 1] of TFloatRectangle; |
| 460 |
PFloatRectangleArray = ^TFloatRectangleArray; |
| 461 |
|
| 462 |
TRectangleList = class({$ifdef FPC}specialize{$endif} TStructList<TRectangle>) |
| 463 |
public |
| 464 |
{ Index of the first rectangle that contains point (X, Y). |
| 465 |
Returns -1 if not found. } |
| 466 |
function FindRectangle(const X, Y: Integer): Integer; overload; |
| 467 |
function FindRectangle(const Point: TVector2): Integer; overload; |
| 468 |
end; |
| 469 |
|
| 470 |
TFloatRectangleList = {$ifdef FPC}specialize{$endif} TStructList<TFloatRectangle>; |
| 471 |
|
| 472 |
{$define read_interface} |
| 473 |
|
| 474 |
{$I castlerectangles_persistent.inc} |
| 475 |
|
| 476 |
{$undef read_interface} |
| 477 |
|
| 478 |
function Rectangle(const Left, Bottom: Integer; |
| 479 |
const Width, Height: Cardinal): TRectangle; overload; |
| 480 |
function Rectangle(const LeftBottom: TVector2Integer; |
| 481 |
const Width, Height: Cardinal): TRectangle; overload; |
| 482 |
function FloatRectangle(const Left, Bottom, Width, Height: Single): TFloatRectangle; overload; |
| 483 |
function FloatRectangle(const R: TRectangle): TFloatRectangle; overload; |
| 484 |
function FloatRectangle(const LeftBottom: TVector2; |
| 485 |
const Width, Height: Single): TFloatRectangle; overload; |
| 486 |
|
| 487 |
implementation |
| 488 |
|
| 489 |
uses SysUtils, Math; |
| 490 |
|
| 491 |
{ TRectangle ----------------------------------------------------------------- } |
| 492 |
|
| 493 |
function Rectangle(const Left, Bottom: Integer; |
| 494 |
const Width, Height: Cardinal): TRectangle; |
| 495 |
begin |
| 496 |
Result.Left := Left; |
| 497 |
Result.Bottom := Bottom; |
| 498 |
Result.Width := Width; |
| 499 |
Result.Height := Height; |
| 500 |
end; |
| 501 |
|
| 502 |
function Rectangle(const LeftBottom: TVector2Integer; |
| 503 |
const Width, Height: Cardinal): TRectangle; |
| 504 |
begin |
| 505 |
Result.Left := LeftBottom.X; |
| 506 |
Result.Bottom := LeftBottom.Y; |
| 507 |
Result.Width := Width; |
| 508 |
Result.Height := Height; |
| 509 |
end; |
| 510 |
|
| 511 |
class function TRectangle.Empty: TRectangle; |
| 512 |
begin |
| 513 |
FillChar(Result, SizeOf(Result), 0); |
| 514 |
end; |
| 515 |
|
| 516 |
function TRectangle.IsEmpty: Boolean; |
| 517 |
begin |
| 518 |
Result := (Width <= 0) or (Height <= 0); |
| 519 |
end; |
| 520 |
|
| 521 |
function TRectangle.Contains(const X, Y: Integer): Boolean; |
| 522 |
begin |
| 523 |
Result := (X >= Left ) and (X < Left + Integer(Width)) and |
| 524 |
(Y >= Bottom) and (Y < Bottom + Integer(Height)); |
| 525 |
end; |
| 526 |
|
| 527 |
function TRectangle.Contains(const Point: TVector2): Boolean; |
| 528 |
begin |
| 529 |
Result := (Point.X >= Left ) and (Point.X < Left + Integer(Width)) and |
| 530 |
(Point.Y >= Bottom) and (Point.Y < Bottom + Integer(Height)); |
| 531 |
end; |
| 532 |
|
| 533 |
function TRectangle.Contains(const Point: TVector2Integer): Boolean; |
| 534 |
begin |
| 535 |
Result := (Point.X >= Left ) and (Point.X < Left + Integer(Width)) and |
| 536 |
(Point.Y >= Bottom) and (Point.Y < Bottom + Integer(Height)); |
| 537 |
end; |
| 538 |
|
| 539 |
function TRectangle.CenterInside(const W, H: Cardinal): TRectangle; |
| 540 |
begin |
| 541 |
Result.Left := Left + (Integer(Width ) - Integer(W)) div 2; |
| 542 |
Result.Bottom := Bottom + (Integer(Height) - Integer(H)) div 2; |
| 543 |
Result.Width := W; |
| 544 |
Result.Height := H; |
| 545 |
end; |
| 546 |
|
| 547 |
function TRectangle.Grow(const DeltaX, DeltaY: Integer): TRectangle; |
| 548 |
begin |
| 549 |
if Integer(Width) + 2 * DeltaX < 0 then |
| 550 |
begin |
| 551 |
Result.Left := Left + Width div 2; |
| 552 |
Result.Width := 0; |
| 553 |
end else |
| 554 |
begin |
| 555 |
Result.Left := Left - DeltaX; |
| 556 |
Result.Width := Integer(Width) + 2 * DeltaX; |
| 557 |
end; |
| 558 |
|
| 559 |
if Integer(Height) + 2 * DeltaY < 0 then |
| 560 |
begin |
| 561 |
Result.Bottom := Bottom + Height div 2; |
| 562 |
Result.Height := 0; |
| 563 |
end else |
| 564 |
begin |
| 565 |
Result.Bottom := Bottom - DeltaY; |
| 566 |
Result.Height := Integer(Height) + 2 * DeltaY; |
| 567 |
end; |
| 568 |
end; |
| 569 |
|
| 570 |
function TRectangle.Grow(const Delta: Integer): TRectangle; |
| 571 |
begin |
| 572 |
Result := Grow(Delta, Delta); |
| 573 |
end; |
| 574 |
|
| 575 |
function TRectangle.GetRight: Integer; |
| 576 |
begin |
| 577 |
Result := Left + Width; |
| 578 |
end; |
| 579 |
|
| 580 |
function TRectangle.GetTop: Integer; |
| 581 |
begin |
| 582 |
Result := Bottom + Height; |
| 583 |
end; |
| 584 |
|
| 585 |
{ |
| 586 |
procedure TRectangle.SetRight(const Value: Integer); |
| 587 |
begin |
| 588 |
if Value <= Left then |
| 589 |
Width := 0 |
| 590 |
else |
| 591 |
Width := Value - Left; |
| 592 |
end; |
| 593 |
|
| 594 |
procedure TRectangle.SetTop(const Value: Integer); |
| 595 |
begin |
| 596 |
if Value <= Bottom then |
| 597 |
Height := 0 |
| 598 |
else |
| 599 |
Height := Value - Bottom; |
| 600 |
end; |
| 601 |
} |
| 602 |
|
| 603 |
function TRectangle.RemoveLeft(W: Cardinal): TRectangle; |
| 604 |
begin |
| 605 |
Result := Self; |
| 606 |
MinVar(W, Width); |
| 607 |
Result.Left := Result.Left + W; |
| 608 |
Result.Width := Result.Width - W; |
| 609 |
end; |
| 610 |
|
| 611 |
function TRectangle.RemoveBottom(H: Cardinal): TRectangle; |
| 612 |
begin |
| 613 |
Result := Self; |
| 614 |
MinVar(H, Height); |
| 615 |
Result.Bottom := Result.Bottom + H; |
| 616 |
Result.Height := Result.Height - H; |
| 617 |
end; |
| 618 |
|
| 619 |
function TRectangle.RemoveRight(W: Cardinal): TRectangle; |
| 620 |
begin |
| 621 |
Result := Self; |
| 622 |
MinVar(W, Width); |
| 623 |
Result.Width := Result.Width - W; |
| 624 |
end; |
| 625 |
|
| 626 |
function TRectangle.RemoveTop(H: Cardinal): TRectangle; |
| 627 |
begin |
| 628 |
Result := Self; |
| 629 |
MinVar(H, Height); |
| 630 |
Result.Height := Result.Height - H; |
| 631 |
end; |
| 632 |
|
| 633 |
function TRectangle.GrowLeft(const W: Cardinal): TRectangle; |
| 634 |
begin |
| 635 |
Result := Self; |
| 636 |
Result.Left := Result.Left - Integer(W); |
| 637 |
Result.Width := Result.Width + W; |
| 638 |
end; |
| 639 |
|
| 640 |
function TRectangle.GrowBottom(const H: Cardinal): TRectangle; |
| 641 |
begin |
| 642 |
Result := Self; |
| 643 |
Result.Bottom := Result.Bottom - Integer(H); |
| 644 |
Result.Height := Result.Height + H; |
| 645 |
end; |
| 646 |
|
| 647 |
function TRectangle.GrowRight(const W: Cardinal): TRectangle; |
| 648 |
begin |
| 649 |
Result := Self; |
| 650 |
Result.Width := Result.Width + W; |
| 651 |
end; |
| 652 |
|
| 653 |
function TRectangle.GrowTop(const H: Cardinal): TRectangle; |
| 654 |
begin |
| 655 |
Result := Self; |
| 656 |
Result.Height := Result.Height + H; |
| 657 |
end; |
| 658 |
|
| 659 |
function TRectangle.LeftPart(W: Cardinal): TRectangle; |
| 660 |
begin |
| 661 |
Result := Self; |
| 662 |
MinVar(W, Width); |
| 663 |
Result.Width := W; |
| 664 |
end; |
| 665 |
|
| 666 |
function TRectangle.BottomPart(H: Cardinal): TRectangle; |
| 667 |
begin |
| 668 |
Result := Self; |
| 669 |
MinVar(H, Height); |
| 670 |
Result.Height := H; |
| 671 |
end; |
| 672 |
|
| 673 |
function TRectangle.RightPart(W: Cardinal): TRectangle; |
| 674 |
begin |
| 675 |
Result := Self; |
| 676 |
MinVar(W, Width); |
| 677 |
Result.Left := Result.Left + Width - W; |
| 678 |
Result.Width := W; |
| 679 |
end; |
| 680 |
|
| 681 |
function TRectangle.TopPart(H: Cardinal): TRectangle; |
| 682 |
begin |
| 683 |
Result := Self; |
| 684 |
MinVar(H, Height); |
| 685 |
Result.Bottom := Result.Bottom + Height - H; |
| 686 |
Result.Height := H; |
| 687 |
end; |
| 688 |
|
| 689 |
function TRectangle.GetLeftBottom: TVector2Integer; |
| 690 |
begin |
| 691 |
Result.X := Left; |
| 692 |
Result.Y := Bottom; |
| 693 |
end; |
| 694 |
|
| 695 |
procedure TRectangle.SetLeftBottom(const Value: TVector2Integer); |
| 696 |
begin |
| 697 |
Left := Value.X; |
| 698 |
Bottom := Value.Y; |
| 699 |
end; |
| 700 |
|
| 701 |
function TRectangle.ClampX(const X: Integer): Integer; |
| 702 |
begin |
| 703 |
if X < Left then |
| 704 |
Result := Left else |
| 705 |
if X >= Left + Width then |
| 706 |
Result := Left + Width - 1 else |
| 707 |
Result := X; |
| 708 |
end; |
| 709 |
|
| 710 |
function TRectangle.ClampY(const Y: Integer): Integer; |
| 711 |
begin |
| 712 |
if Y < Bottom then |
| 713 |
Result := Bottom else |
| 714 |
if Y >= Bottom + Height then |
| 715 |
Result := Bottom + Height - 1 else |
| 716 |
Result := Y; |
| 717 |
end; |
| 718 |
|
| 719 |
function TRectangle.ToString: string; |
| 720 |
begin |
| 721 |
if IsEmpty then |
| 722 |
Result := 'TRectangle: Empty' |
| 723 |
else |
| 724 |
Result := Format('TRectangle: %dx%d %dx%d', [Left, Bottom, Width, Height]); |
| 725 |
end; |
| 726 |
|
| 727 |
function TRectangle.Center: TVector2Integer; |
| 728 |
begin |
| 729 |
Result := Vector2Integer(Left + Width div 2, Bottom + Height div 2); |
| 730 |
end; |
| 731 |
|
| 732 |
function TRectangle.Middle: TVector2Integer; |
| 733 |
begin |
| 734 |
Result := Center; |
| 735 |
end; |
| 736 |
|
| 737 |
function TRectangle.ScaleToWidth(const NewWidth: Cardinal): TRectangle; |
| 738 |
begin |
| 739 |
if IsEmpty then Exit(Empty); |
| 740 |
Result.Left := Left; |
| 741 |
Result.Bottom := Bottom; |
| 742 |
Result.Width := NewWidth; |
| 743 |
Result.Height := Height * NewWidth div Width; |
| 744 |
end; |
| 745 |
|
| 746 |
function TRectangle.ScaleToHeight(const NewHeight: Cardinal): TRectangle; |
| 747 |
begin |
| 748 |
if IsEmpty then Exit(Empty); |
| 749 |
Result.Left := Left; |
| 750 |
Result.Bottom := Bottom; |
| 751 |
Result.Width := Width * NewHeight div Height; |
| 752 |
Result.Height := NewHeight; |
| 753 |
end; |
| 754 |
|
| 755 |
function TRectangle.ScaleAroundMiddle(const Factor: Single): TRectangle; |
| 756 |
begin |
| 757 |
Result := ScaleAroundCenter(Factor); |
| 758 |
end; |
| 759 |
|
| 760 |
function TRectangle.ScaleAroundCenter(const Factor: Single): TRectangle; |
| 761 |
begin |
| 762 |
if Width > 0 then |
| 763 |
begin |
| 764 |
Result.Width := Round(Width * Factor); |
| 765 |
if Width > Result.Width then |
| 766 |
Result.Left := Left + (Width - Result.Width ) div 2 |
| 767 |
else |
| 768 |
Result.Left := Left - (Result.Width - Width) div 2 |
| 769 |
end else |
| 770 |
begin |
| 771 |
Result.Width := Width; |
| 772 |
Result.Left := Left; |
| 773 |
end; |
| 774 |
|
| 775 |
if Height > 0 then |
| 776 |
begin |
| 777 |
Result.Height := Round(Height * Factor); |
| 778 |
if Height > Result.Height then |
| 779 |
Result.Bottom := Bottom + (Height - Result.Height) div 2 |
| 780 |
else |
| 781 |
Result.Bottom := Bottom - (Result.Height - Height) div 2; |
| 782 |
end else |
| 783 |
begin |
| 784 |
Result.Height := Height; |
| 785 |
Result.Bottom := Bottom; |
| 786 |
end; |
| 787 |
end; |
| 788 |
|
| 789 |
function TRectangle.ScaleAround0(const Factor: Single): TRectangle; |
| 790 |
var |
| 791 |
ResultRight, ResultTop: Integer; |
| 792 |
begin |
| 793 |
if Width > 0 then |
| 794 |
begin |
| 795 |
Result.Left := Floor(Left * Factor); |
| 796 |
ResultRight := Ceil(Right * Factor); |
| 797 |
Result.Width := ResultRight - Result.Left; |
| 798 |
end else |
| 799 |
begin |
| 800 |
Result.Width := Width; |
| 801 |
Result.Left := Left; |
| 802 |
end; |
| 803 |
|
| 804 |
if Height > 0 then |
| 805 |
begin |
| 806 |
Result.Bottom := Floor(Bottom * Factor); |
| 807 |
ResultTop := Ceil(Top * Factor); |
| 808 |
Result.Height := ResultTop - Result.Bottom; |
| 809 |
end else |
| 810 |
begin |
| 811 |
Result.Height := Height; |
| 812 |
Result.Bottom := Bottom; |
| 813 |
end; |
| 814 |
end; |
| 815 |
|
| 816 |
function TRectangle.ScaleWidthAround0(const Factor: Single): Cardinal; |
| 817 |
begin |
| 818 |
if Width > 0 then |
| 819 |
Result := Ceil(Right * Factor) - Floor(Left * Factor) else |
| 820 |
Result := Width; |
| 821 |
end; |
| 822 |
|
| 823 |
function TRectangle.ScaleHeightAround0(const Factor: Single): Cardinal; |
| 824 |
begin |
| 825 |
if Height > 0 then |
| 826 |
Result := Ceil(Top * Factor) - Floor(Bottom * Factor) else |
| 827 |
Result := Height; |
| 828 |
end; |
| 829 |
|
| 830 |
function TRectangle.FitInside(const R: TRectangle; |
| 831 |
const AlignHorizontal: THorizontalPosition = hpMiddle; |
| 832 |
const AlignVertical: TVerticalPosition = vpMiddle): TRectangle; |
| 833 |
begin |
| 834 |
if R.Width / R.Height > Width / Height then |
| 835 |
begin |
| 836 |
Result.Height := R.Height; |
| 837 |
Result.Width := Width * Result.Height div Height; |
| 838 |
end else |
| 839 |
begin |
| 840 |
Result.Width := R.Width; |
| 841 |
Result.Height := Height * Result.Width div Width; |
| 842 |
end; |
| 843 |
Result.Left := Result.AlignCore(AlignHorizontal, R, AlignHorizontal, 0); |
| 844 |
Result.Bottom := Result.AlignCore(AlignVertical, R, AlignVertical, 0); |
| 845 |
end; |
| 846 |
|
| 847 |
function TRectangle.AlignCore( |
| 848 |
const ThisPosition: THorizontalPosition; |
| 849 |
const OtherRect: TRectangle; |
| 850 |
const OtherPosition: THorizontalPosition; |
| 851 |
const X: Integer = 0): Integer; |
| 852 |
begin |
| 853 |
Result := OtherRect.Left + X; |
| 854 |
case ThisPosition of |
| 855 |
hpLeft : ; |
| 856 |
{ Integer cast to avoid Arithmetic overflow (FPC 3.3.1-9828-g983fbff871 [2022/01/02], Linux/x86_64), |
| 857 |
testcase in TTestRectangles.TestAlign. } |
| 858 |
hpMiddle: Result := Result - Integer(Width) div 2; |
| 859 |
hpRight : Result := Result - Integer(Width); |
| 860 |
end; |
| 861 |
case OtherPosition of |
| 862 |
hpLeft : ; |
| 863 |
{ Integer cast to avoid Range check error (FPC 3.3.1-9828-g983fbff871 [2022/01/02], Linux/x86_64), |
| 864 |
testcase in TTestRectangles.TestAlign. } |
| 865 |
hpMiddle: Result := Result + Integer(OtherRect.Width) div 2; |
| 866 |
hpRight : Result := Result + Integer(OtherRect.Width); |
| 867 |
end; |
| 868 |
end; |
| 869 |
|
| 870 |
function TRectangle.AlignCore( |
| 871 |
const ThisPosition: TVerticalPosition; |
| 872 |
const OtherRect: TRectangle; |
| 873 |
const OtherPosition: TVerticalPosition; |
| 874 |
const Y: Integer = 0): Integer; |
| 875 |
begin |
| 876 |
Result := OtherRect.Bottom + Y; |
| 877 |
case ThisPosition of |
| 878 |
vpBottom: ; |
| 879 |
vpMiddle: Result := Result - Integer(Height) div 2; |
| 880 |
vpTop : Result := Result - Integer(Height); |
| 881 |
end; |
| 882 |
case OtherPosition of |
| 883 |
vpBottom: ; |
| 884 |
vpMiddle: Result := Result + Integer(OtherRect.Height) div 2; |
| 885 |
vpTop : Result := Result + Integer(OtherRect.Height); |
| 886 |
end; |
| 887 |
end; |
| 888 |
|
| 889 |
function TRectangle.Align( |
| 890 |
const ThisPosition: THorizontalPosition; |
| 891 |
const OtherRect: TRectangle; |
| 892 |
const OtherPosition: THorizontalPosition; |
| 893 |
const X: Integer = 0): TRectangle; |
| 894 |
begin |
| 895 |
Result.Left := AlignCore(ThisPosition, OtherRect, OtherPosition, X); |
| 896 |
Result.Bottom := Bottom; |
| 897 |
Result.Width := Width; |
| 898 |
Result.Height := Height; |
| 899 |
end; |
| 900 |
|
| 901 |
function TRectangle.Align( |
| 902 |
const ThisPosition: TVerticalPosition; |
| 903 |
const OtherRect: TRectangle; |
| 904 |
const OtherPosition: TVerticalPosition; |
| 905 |
const Y: Integer = 0): TRectangle; |
| 906 |
begin |
| 907 |
Result.Left := Left; |
| 908 |
Result.Bottom := AlignCore(ThisPosition, OtherRect, OtherPosition, Y); |
| 909 |
Result.Width := Width; |
| 910 |
Result.Height := Height; |
| 911 |
end; |
| 912 |
|
| 913 |
function TRectangle.Translate(const V: TVector2Integer): TRectangle; |
| 914 |
begin |
| 915 |
Result.Left := Left + V.X; |
| 916 |
Result.Bottom := Bottom + V.Y; |
| 917 |
Result.Width := Width; |
| 918 |
Result.Height := Height; |
| 919 |
end; |
| 920 |
|
| 921 |
function TRectangle.Collides(const R: TRectangle): Boolean; |
| 922 |
begin |
| 923 |
Result := |
| 924 |
(not IsEmpty) and |
| 925 |
(not R.IsEmpty) and |
| 926 |
(not (( Right - 1 < R.Left) or |
| 927 |
(R.Right - 1 < Left))) and |
| 928 |
(not (( Top - 1 < R.Bottom) or |
| 929 |
(R.Top - 1 < Bottom))); |
| 930 |
end; |
| 931 |
|
| 932 |
class operator TRectangle.{$ifdef FPC}+{$else}Add{$endif} (const R1, R2: TRectangle): TRectangle; |
| 933 |
var |
| 934 |
NewRight, NewTop: Integer; |
| 935 |
begin |
| 936 |
if R1.IsEmpty then |
| 937 |
Result := R2 else |
| 938 |
if R2.IsEmpty then |
| 939 |
Result := R1 else |
| 940 |
begin |
| 941 |
Result.Left := Min(R1.Left , R2.Left); |
| 942 |
Result.Bottom := Min(R1.Bottom, R2.Bottom); |
| 943 |
NewRight := Max(R1.Right , R2.Right); |
| 944 |
NewTop := Max(R1.Top , R2.Top); |
| 945 |
Result.Width := NewRight - Result.Left; |
| 946 |
Result.Height := NewTop - Result.Bottom; |
| 947 |
end; |
| 948 |
end; |
| 949 |
|
| 950 |
class operator TRectangle.{$ifdef FPC}*{$else}Multiply{$endif} (const R1, R2: TRectangle): TRectangle; |
| 951 |
var |
| 952 |
NewRight, NewTop: Integer; |
| 953 |
begin |
| 954 |
if R1.IsEmpty or R2.IsEmpty then |
| 955 |
Result := TRectangle.Empty else |
| 956 |
begin |
| 957 |
Result.Left := Max(R1.Left , R2.Left); |
| 958 |
Result.Bottom := Max(R1.Bottom, R2.Bottom); |
| 959 |
NewRight := Min(R1.Right , R2.Right); |
| 960 |
NewTop := Min(R1.Top , R2.Top); |
| 961 |
if (NewRight > Result.Left) and (NewTop > Result.Bottom) then |
| 962 |
begin |
| 963 |
Result.Width := NewRight - Result.Left; |
| 964 |
Result.Height := NewTop - Result.Bottom; |
| 965 |
end else |
| 966 |
Result := TRectangle.Empty; |
| 967 |
end; |
| 968 |
end; |
| 969 |
|
| 970 |
function TRectangle.Equals(const R: TRectangle): Boolean; |
| 971 |
begin |
| 972 |
if IsEmpty then |
| 973 |
Result := R.IsEmpty |
| 974 |
else |
| 975 |
Result := |
| 976 |
(not R.IsEmpty) and |
| 977 |
(Left = R.Left) and |
| 978 |
(Bottom = R.Bottom) and |
| 979 |
(Width = R.Width) and |
| 980 |
(Height = R.Height); |
| 981 |
end; |
| 982 |
|
| 983 |
{ TFloatRectangle ----------------------------------------------------------------- } |
| 984 |
|
| 985 |
function FloatRectangle(const Left, Bottom, Width, Height: Single): TFloatRectangle; |
| 986 |
begin |
| 987 |
Result.Left := Left; |
| 988 |
Result.Bottom := Bottom; |
| 989 |
Result.Width := Width; |
| 990 |
Result.Height := Height; |
| 991 |
end; |
| 992 |
|
| 993 |
function FloatRectangle(const R: TRectangle): TFloatRectangle; |
| 994 |
begin |
| 995 |
if R.IsEmpty then |
| 996 |
Exit(TFloatRectangle.Empty); |
| 997 |
Result.Left := R.Left; |
| 998 |
Result.Bottom := R.Bottom; |
| 999 |
Result.Width := R.Width; |
| 1000 |
Result.Height := R.Height; |
| 1001 |
end; |
| 1002 |
|
| 1003 |
function FloatRectangle(const LeftBottom: TVector2; |
| 1004 |
const Width, Height: Single): TFloatRectangle; |
| 1005 |
begin |
| 1006 |
Result.Left := LeftBottom.X; |
| 1007 |
Result.Bottom := LeftBottom.Y; |
| 1008 |
Result.Width := Width; |
| 1009 |
Result.Height := Height; |
| 1010 |
end; |
| 1011 |
|
| 1012 |
class function TFloatRectangle.Empty: TFloatRectangle; |
| 1013 |
begin |
| 1014 |
FillChar(Result, SizeOf(Result), 0); |
| 1015 |
Result.Width := -1; |
| 1016 |
Result.Height := -1; |
| 1017 |
end; |
| 1018 |
|
| 1019 |
function TFloatRectangle.IsEmpty: Boolean; |
| 1020 |
begin |
| 1021 |
Result := (Width < 0) or (Height < 0); |
| 1022 |
end; |
| 1023 |
|
| 1024 |
function TFloatRectangle.Contains(const X, Y: Single): Boolean; |
| 1025 |
begin |
| 1026 |
Result := (X >= Left ) and (X <= Left + Width) and |
| 1027 |
(Y >= Bottom) and (Y <= Bottom + Height); |
| 1028 |
end; |
| 1029 |
|
| 1030 |
function TFloatRectangle.Contains(const Point: TVector2): Boolean; |
| 1031 |
begin |
| 1032 |
Result := (Point.X >= Left ) and (Point.X <= Left + Width) and |
| 1033 |
(Point.Y >= Bottom) and (Point.Y <= Bottom + Height); |
| 1034 |
end; |
| 1035 |
|
| 1036 |
function TFloatRectangle.Contains(const R: TFloatRectangle): Boolean; |
| 1037 |
begin |
| 1038 |
if R.IsEmpty then |
| 1039 |
Result := true |
| 1040 |
else |
| 1041 |
Result := |
| 1042 |
(not IsEmpty) and |
| 1043 |
(R.Left >= Left) and |
| 1044 |
(R.Bottom >= Bottom) and |
| 1045 |
(R.Right <= Right) and |
| 1046 |
(R.Top <= Top); |
| 1047 |
end; |
| 1048 |
|
| 1049 |
function TFloatRectangle.GetLeftBottom: TVector2; |
| 1050 |
begin |
| 1051 |
Result.X := Left; |
| 1052 |
Result.Y := Bottom; |
| 1053 |
end; |
| 1054 |
|
| 1055 |
procedure TFloatRectangle.SetLeftBottom(const Value: TVector2); |
| 1056 |
begin |
| 1057 |
Left := Value.X; |
| 1058 |
Bottom := Value.Y; |
| 1059 |
end; |
| 1060 |
|
| 1061 |
function TFloatRectangle.GetSize: TVector2; |
| 1062 |
begin |
| 1063 |
Result.X := Width; |
| 1064 |
Result.Y := Height; |
| 1065 |
end; |
| 1066 |
|
| 1067 |
procedure TFloatRectangle.SetSize(const Value: TVector2); |
| 1068 |
begin |
| 1069 |
Width := Value.X; |
| 1070 |
Height := Value.Y; |
| 1071 |
end; |
| 1072 |
|
| 1073 |
function TFloatRectangle.Center: TVector2; |
| 1074 |
begin |
| 1075 |
Result := Vector2(Left + Width / 2, Bottom + Height / 2); |
| 1076 |
end; |
| 1077 |
|
| 1078 |
function TFloatRectangle.Middle: TVector2; |
| 1079 |
begin |
| 1080 |
Result := Center; |
| 1081 |
end; |
| 1082 |
|
| 1083 |
function TFloatRectangle.CenterInside(const W, H: Single): TFloatRectangle; |
| 1084 |
begin |
| 1085 |
Result.Left := Left + (Width - W) / 2; |
| 1086 |
Result.Bottom := Bottom + (Height - H) / 2; |
| 1087 |
Result.Width := W; |
| 1088 |
Result.Height := H; |
| 1089 |
end; |
| 1090 |
|
| 1091 |
function TFloatRectangle.Grow(const DeltaX, DeltaY: Single): TFloatRectangle; |
| 1092 |
begin |
| 1093 |
if Width + 2 * DeltaX < 0 then |
| 1094 |
begin |
| 1095 |
Result.Left := Left + Width / 2; |
| 1096 |
Result.Width := 0; |
| 1097 |
end else |
| 1098 |
begin |
| 1099 |
Result.Left := Left - DeltaX; |
| 1100 |
Result.Width := Width + 2 * DeltaX; |
| 1101 |
end; |
| 1102 |
|
| 1103 |
if Height + 2 * DeltaY < 0 then |
| 1104 |
begin |
| 1105 |
Result.Bottom := Bottom + Height / 2; |
| 1106 |
Result.Height := 0; |
| 1107 |
end else |
| 1108 |
begin |
| 1109 |
Result.Bottom := Bottom - DeltaY; |
| 1110 |
Result.Height := Height + 2 * DeltaY; |
| 1111 |
end; |
| 1112 |
end; |
| 1113 |
|
| 1114 |
function TFloatRectangle.Grow(const Delta: Single): TFloatRectangle; |
| 1115 |
begin |
| 1116 |
Result := Grow(Delta, Delta); |
| 1117 |
end; |
| 1118 |
|
| 1119 |
function TFloatRectangle.RemoveLeft(W: Single): TFloatRectangle; |
| 1120 |
begin |
| 1121 |
Result := Self; |
| 1122 |
MinVar(W, Width); |
| 1123 |
Result.Left := Result.Left + W; |
| 1124 |
Result.Width := Result.Width - W; |
| 1125 |
end; |
| 1126 |
|
| 1127 |
function TFloatRectangle.RemoveBottom(H: Single): TFloatRectangle; |
| 1128 |
begin |
| 1129 |
Result := Self; |
| 1130 |
MinVar(H, Height); |
| 1131 |
Result.Bottom := Result.Bottom + H; |
| 1132 |
Result.Height := Result.Height - H; |
| 1133 |
end; |
| 1134 |
|
| 1135 |
function TFloatRectangle.RemoveRight(W: Single): TFloatRectangle; |
| 1136 |
begin |
| 1137 |
Result := Self; |
| 1138 |
MinVar(W, Width); |
| 1139 |
Result.Width := Result.Width - W; |
| 1140 |
end; |
| 1141 |
|
| 1142 |
function TFloatRectangle.RemoveTop(H: Single): TFloatRectangle; |
| 1143 |
begin |
| 1144 |
Result := Self; |
| 1145 |
MinVar(H, Height); |
| 1146 |
Result.Height := Result.Height - H; |
| 1147 |
end; |
| 1148 |
|
| 1149 |
function TFloatRectangle.GrowLeft(const W: Single): TFloatRectangle; |
| 1150 |
begin |
| 1151 |
Result := Self; |
| 1152 |
Result.Left := Result.Left - W; |
| 1153 |
Result.Width := Result.Width + W; |
| 1154 |
end; |
| 1155 |
|
| 1156 |
function TFloatRectangle.GrowBottom(const H: Single): TFloatRectangle; |
| 1157 |
begin |
| 1158 |
Result := Self; |
| 1159 |
Result.Bottom := Result.Bottom - H; |
| 1160 |
Result.Height := Result.Height + H; |
| 1161 |
end; |
| 1162 |
|
| 1163 |
function TFloatRectangle.GrowRight(const W: Single): TFloatRectangle; |
| 1164 |
begin |
| 1165 |
Result := Self; |
| 1166 |
Result.Width := Result.Width + W; |
| 1167 |
end; |
| 1168 |
|
| 1169 |
function TFloatRectangle.GrowTop(const H: Single): TFloatRectangle; |
| 1170 |
begin |
| 1171 |
Result := Self; |
| 1172 |
Result.Height := Result.Height + H; |
| 1173 |
end; |
| 1174 |
|
| 1175 |
function TFloatRectangle.LeftPart(W: Single): TFloatRectangle; |
| 1176 |
begin |
| 1177 |
Result := Self; |
| 1178 |
MinVar(W, Width); |
| 1179 |
Result.Width := W; |
| 1180 |
end; |
| 1181 |
|
| 1182 |
function TFloatRectangle.BottomPart(H: Single): TFloatRectangle; |
| 1183 |
begin |
| 1184 |
Result := Self; |
| 1185 |
MinVar(H, Height); |
| 1186 |
Result.Height := H; |
| 1187 |
end; |
| 1188 |
|
| 1189 |
function TFloatRectangle.RightPart(W: Single): TFloatRectangle; |
| 1190 |
begin |
| 1191 |
Result := Self; |
| 1192 |
MinVar(W, Width); |
| 1193 |
Result.Left := Result.Left + Width - W; |
| 1194 |
Result.Width := W; |
| 1195 |
end; |
| 1196 |
|
| 1197 |
function TFloatRectangle.TopPart(H: Single): TFloatRectangle; |
| 1198 |
begin |
| 1199 |
Result := Self; |
| 1200 |
MinVar(H, Height); |
| 1201 |
Result.Bottom := Result.Bottom + Height - H; |
| 1202 |
Result.Height := H; |
| 1203 |
end; |
| 1204 |
|
| 1205 |
function TFloatRectangle.GetRight: Single; |
| 1206 |
begin |
| 1207 |
Result := Left + Width; |
| 1208 |
end; |
| 1209 |
|
| 1210 |
function TFloatRectangle.GetTop: Single; |
| 1211 |
begin |
| 1212 |
Result := Bottom + Height; |
| 1213 |
end; |
| 1214 |
|
| 1215 |
{ |
| 1216 |
procedure TFloatRectangle.SetRight(const Value: Single); |
| 1217 |
begin |
| 1218 |
Width := Value - Left; |
| 1219 |
end; |
| 1220 |
|
| 1221 |
procedure TFloatRectangle.SetTop(const Value: Single); |
| 1222 |
begin |
| 1223 |
Height := Value - Bottom; |
| 1224 |
end; |
| 1225 |
} |
| 1226 |
|
| 1227 |
function TFloatRectangle.AlignCore( |
| 1228 |
const ThisPosition: THorizontalPosition; |
| 1229 |
const OtherRect: TFloatRectangle; |
| 1230 |
const OtherPosition: THorizontalPosition; |
| 1231 |
const X: Single = 0): Single; |
| 1232 |
begin |
| 1233 |
Result := OtherRect.Left + X; |
| 1234 |
case ThisPosition of |
| 1235 |
hpLeft : ; |
| 1236 |
hpMiddle: Result := Result - Width / 2; |
| 1237 |
hpRight : Result := Result - Width; |
| 1238 |
end; |
| 1239 |
case OtherPosition of |
| 1240 |
hpLeft : ; |
| 1241 |
hpMiddle: Result := Result + OtherRect.Width / 2; |
| 1242 |
hpRight : Result := Result + OtherRect.Width; |
| 1243 |
end; |
| 1244 |
end; |
| 1245 |
|
| 1246 |
function TFloatRectangle.AlignCore( |
| 1247 |
const ThisPosition: TVerticalPosition; |
| 1248 |
const OtherRect: TFloatRectangle; |
| 1249 |
const OtherPosition: TVerticalPosition; |
| 1250 |
const Y: Single = 0): Single; |
| 1251 |
begin |
| 1252 |
Result := OtherRect.Bottom + Y; |
| 1253 |
case ThisPosition of |
| 1254 |
vpBottom: ; |
| 1255 |
vpMiddle: Result := Result - Height / 2; |
| 1256 |
vpTop : Result := Result - Height; |
| 1257 |
end; |
| 1258 |
case OtherPosition of |
| 1259 |
vpBottom: ; |
| 1260 |
vpMiddle: Result := Result + OtherRect.Height / 2; |
| 1261 |
vpTop : Result := Result + OtherRect.Height; |
| 1262 |
end; |
| 1263 |
end; |
| 1264 |
|
| 1265 |
function TFloatRectangle.Align( |
| 1266 |
const ThisPosition: THorizontalPosition; |
| 1267 |
const OtherRect: TFloatRectangle; |
| 1268 |
const OtherPosition: THorizontalPosition; |
| 1269 |
const X: Single = 0): TFloatRectangle; |
| 1270 |
begin |
| 1271 |
Result.Left := AlignCore(ThisPosition, OtherRect, OtherPosition, X); |
| 1272 |
Result.Bottom := Bottom; |
| 1273 |
Result.Width := Width; |
| 1274 |
Result.Height := Height; |
| 1275 |
end; |
| 1276 |
|
| 1277 |
function TFloatRectangle.Align( |
| 1278 |
const ThisPosition: TVerticalPosition; |
| 1279 |
const OtherRect: TFloatRectangle; |
| 1280 |
const OtherPosition: TVerticalPosition; |
| 1281 |
const Y: Single = 0): TFloatRectangle; |
| 1282 |
begin |
| 1283 |
Result.Left := Left; |
| 1284 |
Result.Bottom := AlignCore(ThisPosition, OtherRect, OtherPosition, Y); |
| 1285 |
Result.Width := Width; |
| 1286 |
Result.Height := Height; |
| 1287 |
end; |
| 1288 |
|
| 1289 |
function TFloatRectangle.ToString: string; |
| 1290 |
begin |
| 1291 |
if IsEmpty then |
| 1292 |
Result := 'TFloatRectangle: Empty' |
| 1293 |
else |
| 1294 |
Result := Format('TFloatRectangle: %fx%f %fx%f', [Left, Bottom, Width, Height]); |
| 1295 |
end; |
| 1296 |
|
| 1297 |
function TFloatRectangle.Translate(const V: TVector2): TFloatRectangle; |
| 1298 |
begin |
| 1299 |
Result.Left := Left + V.X; |
| 1300 |
Result.Bottom := Bottom + V.Y; |
| 1301 |
Result.Width := Width; |
| 1302 |
Result.Height := Height; |
| 1303 |
end; |
| 1304 |
|
| 1305 |
function TFloatRectangle.Collides(const R: TFloatRectangle): Boolean; |
| 1306 |
begin |
| 1307 |
Result := |
| 1308 |
(not IsEmpty) and |
| 1309 |
(not R.IsEmpty) and |
| 1310 |
(not (( Right < R.Left) or |
| 1311 |
(R.Right < Left))) and |
| 1312 |
(not (( Top < R.Bottom) or |
| 1313 |
(R.Top < Bottom))); |
| 1314 |
end; |
| 1315 |
|
| 1316 |
function TFloatRectangle.CollidesDisc(const DiscCenter: TVector2; |
| 1317 |
const Radius: Single): Boolean; |
| 1318 |
var |
| 1319 |
ARight, ATop, ClosestCornerX, ClosestCornerY: Single; |
| 1320 |
InsideX, InsideY: Boolean; |
| 1321 |
begin |
| 1322 |
if IsEmpty then |
| 1323 |
Exit(false); |
| 1324 |
|
| 1325 |
ARight := Left + Width; |
| 1326 |
ATop := Bottom + Height; |
| 1327 |
|
| 1328 |
if DiscCenter.X < Left then |
| 1329 |
begin |
| 1330 |
InsideX := false; |
| 1331 |
ClosestCornerX := Left; |
| 1332 |
if Left - DiscCenter.X > Radius then Exit(false); |
| 1333 |
end else |
| 1334 |
if DiscCenter.X > ARight then |
| 1335 |
begin |
| 1336 |
InsideX := false; |
| 1337 |
ClosestCornerX := ARight; |
| 1338 |
if DiscCenter.X - ARight > Radius then Exit(false); |
| 1339 |
end else |
| 1340 |
begin |
| 1341 |
InsideX := true; |
| 1342 |
if DiscCenter.X < (Left + ARight) / 2 then |
| 1343 |
ClosestCornerX := Left |
| 1344 |
else |
| 1345 |
ClosestCornerX := ARight; |
| 1346 |
end; |
| 1347 |
|
| 1348 |
if DiscCenter.Y < Bottom then |
| 1349 |
begin |
| 1350 |
InsideY := false; |
| 1351 |
ClosestCornerY := Bottom; |
| 1352 |
if Bottom - DiscCenter.Y > Radius then Exit(false); |
| 1353 |
end else |
| 1354 |
if DiscCenter.Y > ATop then |
| 1355 |
begin |
| 1356 |
InsideY := false; |
| 1357 |
ClosestCornerY := ATop; |
| 1358 |
if DiscCenter.Y - ATop > Radius then Exit(false); |
| 1359 |
end else |
| 1360 |
begin |
| 1361 |
InsideY := true; |
| 1362 |
if DiscCenter.Y < (Bottom + ATop) / 2 then |
| 1363 |
ClosestCornerY := Bottom |
| 1364 |
else |
| 1365 |
ClosestCornerY := ATop; |
| 1366 |
end; |
| 1367 |
|
| 1368 |
{ If we get here, then DiscCenter is within a Radius margin from the rectangle. |
| 1369 |
In other words, the bounding rect of the cirle for sure collides with |
| 1370 |
this rectangle. |
| 1371 |
The only way for the circle not to collide, is to be at the very corner |
| 1372 |
of such "rectangle enlarged by Radius". } |
| 1373 |
|
| 1374 |
if InsideX or InsideY then |
| 1375 |
Exit(true); |
| 1376 |
|
| 1377 |
Result := |
| 1378 |
Sqr(DiscCenter.X - ClosestCornerX) + |
| 1379 |
Sqr(DiscCenter.Y - ClosestCornerY) <= |
| 1380 |
Sqr(Radius); |
| 1381 |
end; |
| 1382 |
|
| 1383 |
function TFloatRectangle.ScaleToWidth(const NewWidth: Single): TFloatRectangle; |
| 1384 |
begin |
| 1385 |
if IsEmpty then Exit(Empty); |
| 1386 |
Result.Left := Left; |
| 1387 |
Result.Bottom := Bottom; |
| 1388 |
Result.Width := NewWidth; |
| 1389 |
Result.Height := Height * NewWidth / Width; |
| 1390 |
end; |
| 1391 |
|
| 1392 |
function TFloatRectangle.ScaleToHeight(const NewHeight: Single): TFloatRectangle; |
| 1393 |
begin |
| 1394 |
if IsEmpty then Exit(Empty); |
| 1395 |
Result.Left := Left; |
| 1396 |
Result.Bottom := Bottom; |
| 1397 |
Result.Width := Width * NewHeight / Height; |
| 1398 |
Result.Height := NewHeight; |
| 1399 |
end; |
| 1400 |
|
| 1401 |
function TFloatRectangle.ScaleAroundCenter(const Factor: Single): TFloatRectangle; |
| 1402 |
begin |
| 1403 |
if Width >= 0 then |
| 1404 |
begin |
| 1405 |
Result.Width := Width * Factor; |
| 1406 |
Result.Left := Left + (Width - Result.Width ) / 2; |
| 1407 |
end else |
| 1408 |
begin |
| 1409 |
Result.Width := Width; |
| 1410 |
Result.Left := Left; |
| 1411 |
end; |
| 1412 |
|
| 1413 |
if Height >= 0 then |
| 1414 |
begin |
| 1415 |
Result.Height := Height * Factor; |
| 1416 |
Result.Bottom := Bottom + (Height - Result.Height) / 2; |
| 1417 |
end else |
| 1418 |
begin |
| 1419 |
Result.Height := Height; |
| 1420 |
Result.Bottom := Bottom; |
| 1421 |
end; |
| 1422 |
end; |
| 1423 |
|
| 1424 |
function TFloatRectangle.ScaleAround0(const Factor: Single): TFloatRectangle; |
| 1425 |
var |
| 1426 |
ResultRight, ResultTop: Single; |
| 1427 |
begin |
| 1428 |
if Width >= 0 then |
| 1429 |
begin |
| 1430 |
Result.Left := Left * Factor; |
| 1431 |
ResultRight := Right * Factor; |
| 1432 |
Result.Width := ResultRight - Result.Left; |
| 1433 |
end else |
| 1434 |
begin |
| 1435 |
Result.Width := Width; |
| 1436 |
Result.Left := Left; |
| 1437 |
end; |
| 1438 |
|
| 1439 |
if Height >= 0 then |
| 1440 |
begin |
| 1441 |
Result.Bottom := Bottom * Factor; |
| 1442 |
ResultTop := Top * Factor; |
| 1443 |
Result.Height := ResultTop - Result.Bottom; |
| 1444 |
end else |
| 1445 |
begin |
| 1446 |
Result.Height := Height; |
| 1447 |
Result.Bottom := Bottom; |
| 1448 |
end; |
| 1449 |
end; |
| 1450 |
|
| 1451 |
function TFloatRectangle.FitInside(const R: TFloatRectangle; |
| 1452 |
const AlignHorizontal: THorizontalPosition = hpMiddle; |
| 1453 |
const AlignVertical: TVerticalPosition = vpMiddle): TFloatRectangle; |
| 1454 |
begin |
| 1455 |
if R.Width / R.Height > Width / Height then |
| 1456 |
begin |
| 1457 |
Result.Height := R.Height; |
| 1458 |
Result.Width := Width * Result.Height / Height; |
| 1459 |
end else |
| 1460 |
begin |
| 1461 |
Result.Width := R.Width; |
| 1462 |
Result.Height := Height * Result.Width / Width; |
| 1463 |
end; |
| 1464 |
Result.Left := Result.AlignCore(AlignHorizontal, R, AlignHorizontal, 0); |
| 1465 |
Result.Bottom := Result.AlignCore(AlignVertical, R, AlignVertical, 0); |
| 1466 |
end; |
| 1467 |
|
| 1468 |
function TFloatRectangle.Include(const P: TVector2): TFloatRectangle; |
| 1469 |
begin |
| 1470 |
if IsEmpty then |
| 1471 |
begin |
| 1472 |
Result.Left := P.X; |
| 1473 |
Result.Bottom := P.Y; |
| 1474 |
Result.Width := 0; |
| 1475 |
Result.Height := 0; |
| 1476 |
end else |
| 1477 |
begin |
| 1478 |
if P.X < Left then |
| 1479 |
begin |
| 1480 |
Result.Left := P.X; |
| 1481 |
Result.Width := (Left - P.X) + Width; |
| 1482 |
end else |
| 1483 |
begin |
| 1484 |
Result.Left := Left; |
| 1485 |
if P.X > Right then |
| 1486 |
Result.Width := Width + P.X - Right |
| 1487 |
else |
| 1488 |
Result.Width := Width; |
| 1489 |
end; |
| 1490 |
|
| 1491 |
if P.Y < Bottom then |
| 1492 |
begin |
| 1493 |
Result.Bottom := P.Y; |
| 1494 |
Result.Height := (Bottom - P.Y) + Height; |
| 1495 |
end else |
| 1496 |
begin |
| 1497 |
Result.Bottom := Bottom; |
| 1498 |
if P.Y > Top then |
| 1499 |
Result.Height := Height + P.Y - Top |
| 1500 |
else |
| 1501 |
Result.Height := Height; |
| 1502 |
end; |
| 1503 |
end; |
| 1504 |
end; |
| 1505 |
|
| 1506 |
function TFloatRectangle.ToX3DVector: TVector4; |
| 1507 |
begin |
| 1508 |
Result := Vector4( |
| 1509 |
Left, |
| 1510 |
Bottom, |
| 1511 |
Right, |
| 1512 |
Top); |
| 1513 |
end; |
| 1514 |
|
| 1515 |
class function TFloatRectangle.FromX3DVector(const V: TVector4): TFloatRectangle; |
| 1516 |
begin |
| 1517 |
Result.Left := V.X; |
| 1518 |
Result.Bottom := V.Y; |
| 1519 |
Result.Width := V.Z - V.X; |
| 1520 |
Result.Height := V.W - V.Y; |
| 1521 |
end; |
| 1522 |
|
| 1523 |
function TFloatRectangle.Round: TRectangle; |
| 1524 |
begin |
| 1525 |
if IsEmpty then |
| 1526 |
Result := TRectangle.Empty |
| 1527 |
else |
| 1528 |
Result := Rectangle( |
| 1529 |
System.Round(Left), |
| 1530 |
System.Round(Bottom), |
| 1531 |
System.Round(Width), |
| 1532 |
System.Round(Height)); |
| 1533 |
end; |
| 1534 |
|
| 1535 |
function TFloatRectangle.Equals(const R: TFloatRectangle): Boolean; |
| 1536 |
begin |
| 1537 |
if IsEmpty then |
| 1538 |
Result := R.IsEmpty |
| 1539 |
else |
| 1540 |
Result := |
| 1541 |
(not R.IsEmpty) and |
| 1542 |
(SameValue(Left , R.Left)) and |
| 1543 |
(SameValue(Bottom, R.Bottom)) and |
| 1544 |
(SameValue(Width , R.Width)) and |
| 1545 |
(SameValue(Height, R.Height)); |
| 1546 |
end; |
| 1547 |
|
| 1548 |
function TFloatRectangle.Equals(const R: TFloatRectangle; const Epsilon: Single): Boolean; |
| 1549 |
begin |
| 1550 |
if IsEmpty then |
| 1551 |
Result := R.IsEmpty |
| 1552 |
else |
| 1553 |
Result := |
| 1554 |
(not R.IsEmpty) and |
| 1555 |
(SameValue(Left , R.Left , Epsilon)) and |
| 1556 |
(SameValue(Bottom, R.Bottom, Epsilon)) and |
| 1557 |
(SameValue(Width , R.Width , Epsilon)) and |
| 1558 |
(SameValue(Height, R.Height, Epsilon)); |
| 1559 |
end; |
| 1560 |
|
| 1561 |
function TFloatRectangle.PerfectlyEquals(const R: TFloatRectangle): Boolean; |
| 1562 |
begin |
| 1563 |
if IsEmpty then |
| 1564 |
Result := R.IsEmpty |
| 1565 |
else |
| 1566 |
Result := |
| 1567 |
(not R.IsEmpty) and |
| 1568 |
(Left = R.Left ) and |
| 1569 |
(Bottom = R.Bottom) and |
| 1570 |
(Width = R.Width ) and |
| 1571 |
(Height = R.Height); |
| 1572 |
end; |
| 1573 |
|
| 1574 |
class operator TFloatRectangle.{$ifdef FPC}+{$else}Add{$endif} (const R1, R2: TFloatRectangle): TFloatRectangle; |
| 1575 |
var |
| 1576 |
NewRight, NewTop: Single; |
| 1577 |
begin |
| 1578 |
if R1.IsEmpty then |
| 1579 |
Result := R2 else |
| 1580 |
if R2.IsEmpty then |
| 1581 |
Result := R1 else |
| 1582 |
begin |
| 1583 |
Result.Left := Min(R1.Left , R2.Left); |
| 1584 |
Result.Bottom := Min(R1.Bottom, R2.Bottom); |
| 1585 |
NewRight := Max(R1.Right , R2.Right); |
| 1586 |
NewTop := Max(R1.Top , R2.Top); |
| 1587 |
Result.Width := NewRight - Result.Left; |
| 1588 |
Result.Height := NewTop - Result.Bottom; |
| 1589 |
end; |
| 1590 |
end; |
| 1591 |
|
| 1592 |
class operator TFloatRectangle.{$ifdef FPC}*{$else}Multiply{$endif} (const R1, R2: TFloatRectangle): TFloatRectangle; |
| 1593 |
var |
| 1594 |
NewRight, NewTop: Single; |
| 1595 |
begin |
| 1596 |
if R1.IsEmpty or R2.IsEmpty then |
| 1597 |
Result := TFloatRectangle.Empty else |
| 1598 |
begin |
| 1599 |
Result.Left := Max(R1.Left , R2.Left); |
| 1600 |
Result.Bottom := Max(R1.Bottom, R2.Bottom); |
| 1601 |
NewRight := Min(R1.Right , R2.Right); |
| 1602 |
NewTop := Min(R1.Top , R2.Top); |
| 1603 |
{ ">=" unline the int version that checks ">". |
| 1604 |
For TFloatRectangle, having zero size makes sense. } |
| 1605 |
if (NewRight >= Result.Left) and (NewTop >= Result.Bottom) then |
| 1606 |
begin |
| 1607 |
{ use Max(0, ..) to secure from floating point errors in case equations above |
| 1608 |
are true but subtraction yields < 0 due to floating point inaccuracy. |
| 1609 |
Not sure is this possible (A >= B and still A - B < 0), probably not, |
| 1610 |
but better stay safe when dealing with floating point numbers. } |
| 1611 |
Result.Width := Max(0, NewRight - Result.Left); |
| 1612 |
Result.Height := Max(0, NewTop - Result.Bottom); |
| 1613 |
end else |
| 1614 |
Result := TFloatRectangle.Empty; |
| 1615 |
end; |
| 1616 |
end; |
| 1617 |
|
| 1618 |
procedure TFloatRectangle.AddToCoords(const Coords: TVector2List); |
| 1619 |
begin |
| 1620 |
Coords.Add(Vector2(Left , Bottom)); |
| 1621 |
Coords.Add(Vector2(Right, Bottom)); |
| 1622 |
Coords.Add(Vector2(Right, Top)); |
| 1623 |
Coords.Add(Vector2(Left , Top)); |
| 1624 |
end; |
| 1625 |
|
| 1626 |
procedure TFloatRectangle.AddToCoords(const Coords: TVector3List; const Z: Single); |
| 1627 |
begin |
| 1628 |
Coords.Add(Vector3(Left , Bottom, Z)); |
| 1629 |
Coords.Add(Vector3(Right, Bottom, Z)); |
| 1630 |
Coords.Add(Vector3(Right, Top , Z)); |
| 1631 |
Coords.Add(Vector3(Left , Top , Z)); |
| 1632 |
end; |
| 1633 |
|
| 1634 |
{ TRectangleList -------------------------------------------------------------- } |
| 1635 |
|
| 1636 |
function TRectangleList.FindRectangle(const X, Y: Integer): Integer; |
| 1637 |
begin |
| 1638 |
for Result := 0 to Count - 1 do |
| 1639 |
if L[Result].Contains(X, Y) then |
| 1640 |
Exit; |
| 1641 |
Result := -1; |
| 1642 |
end; |
| 1643 |
|
| 1644 |
function TRectangleList.FindRectangle(const Point: TVector2): Integer; |
| 1645 |
begin |
| 1646 |
for Result := 0 to Count - 1 do |
| 1647 |
if L[Result].Contains(Point) then |
| 1648 |
Exit; |
| 1649 |
Result := -1; |
| 1650 |
end; |
| 1651 |
|
| 1652 |
{$define read_implementation} |
| 1653 |
|
| 1654 |
{$I castlerectangles_persistent.inc} |
| 1655 |
|
| 1656 |
end. |