/usr/share/gnu-smalltalk/kernel/Rectangle.st is in gnu-smalltalk-common 3.2.5-1build2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 | "========================================================================
|
| Rectangle Class
|
|
========================================================================"
"======================================================================
|
| Copyright 1988,92,94,95,99,2000,2001,2002,2006,2008
| Free Software Foundation, Inc.
| Written by Doug McCallum.
|
| This file is part of the GNU Smalltalk class library.
|
| The GNU Smalltalk class library is free software; you can redistribute it
| and/or modify it under the terms of the GNU Lesser General Public License
| as published by the Free Software Foundation; either version 2.1, or (at
| your option) any later version.
|
| The GNU Smalltalk class library 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 Lesser
| General Public License for more details.
|
| You should have received a copy of the GNU Lesser General Public License
| along with the GNU Smalltalk class library; see the file COPYING.LIB.
| If not, write to the Free Software Foundation, 59 Temple Place - Suite
| 330, Boston, MA 02110-1301, USA.
|
======================================================================"
Object subclass: Rectangle [
| origin corner |
<category: 'Language-Data types'>
<comment: 'Beginning of the Rectangle class for simple display manipulation.
Rectangles require the Point class to be available. An extension
to the Point class is made here that since it requires Rectangles
to be defined (see converting)'>
Rectangle class >> new [
"Answer the (0 @ 0 corner: 0 @ 0) rectangle"
<category: 'instance creation'>
^self origin: 0 @ 0 corner: 0 @ 0
]
Rectangle class >> origin: originPoint corner: cornerPoint [
"Answer a rectangle with the given corners"
<category: 'instance creation'>
^self basicNew origin: originPoint corner: cornerPoint
]
Rectangle class >> origin: originPoint extent: extentPoint [
"Answer a rectangle with the given origin and size"
<category: 'instance creation'>
^self basicNew origin: originPoint corner: originPoint + extentPoint
]
Rectangle class >> left: leftNumber right: rightNumber top: topNumber bottom: bottomNumber [
"Answer a rectangle with the given coordinates"
<category: 'instance creation'>
^self basicNew origin: (Point x: leftNumber y: topNumber)
corner: (Point x: rightNumber y: bottomNumber)
]
Rectangle class >> left: leftNumber top: topNumber right: rightNumber bottom: bottomNumber [
"Answer a rectangle with the given coordinates"
<category: 'instance creation'>
^self basicNew origin: (Point x: leftNumber y: topNumber)
corner: (Point x: rightNumber y: bottomNumber)
]
bottom [
"Answer the corner's y of the receiver"
<category: 'accessing'>
^corner y
]
bottom: aNumber [
"Set the corner's y of the receiver"
<category: 'accessing'>
corner y: aNumber
]
bottomCenter [
"Answer the center of the receiver's bottom side"
<category: 'accessing'>
^self xCenter @ corner y
]
bottomLeft [
"Answer the bottom-left corner of the receiver"
<category: 'accessing'>
^origin x @ corner y
]
bottomLeft: aPoint [
"Answer the receiver with the bottom-left changed to aPoint"
<category: 'accessing'>
origin x: aPoint x.
corner y: aPoint y
]
bottomRight [
"Answer the bottom-right corner of the receiver"
<category: 'accessing'>
^corner copy
]
bottomRight: aPoint [
"Change the bottom-right corner of the receiver"
<category: 'accessing'>
corner := aPoint copy
]
center [
"Answer the center of the receiver"
<category: 'accessing'>
^self xCenter @ self yCenter
]
corner [
"Answer the corner of the receiver"
<category: 'accessing'>
^corner
]
corner: aPoint [
"Set the corner of the receiver"
<category: 'accessing'>
corner := aPoint
]
extent [
"Answer the extent of the receiver"
<category: 'accessing'>
^corner - origin
]
extent: aPoint [
"Change the size of the receiver, keeping the origin the same"
<category: 'accessing'>
corner := origin + aPoint
]
height [
"Answer the height of the receiver"
<category: 'accessing'>
^self bottom - self top
]
height: aNumber [
"Set the height of the receiver"
<category: 'accessing'>
corner y: self origin y + aNumber
]
left [
"Answer the x of the left edge of the receiver"
<category: 'accessing'>
^origin x
]
left: aValue [
"Set the x of the left edge of the receiver"
<category: 'accessing'>
origin x: aValue
]
left: l top: t right: r bottom: b [
"Change all four the coordinates of the receiver's corners"
<category: 'accessing'>
origin := l @ t.
corner := r @ b
]
leftCenter [
"Answer the center of the receiver's left side"
<category: 'accessing'>
^origin x @ self yCenter
]
origin [
"Answer the top-left corner of the receiver"
<category: 'accessing'>
^origin
]
origin: aPoint [
"Change the top-left corner of the receiver to aPoint"
<category: 'accessing'>
origin := aPoint copy
]
origin: pnt1 corner: pnt2 [
"Change both the origin (top-left corner) and the corner (bottom-right
corner) of the receiver"
<category: 'accessing'>
origin := pnt1 copy.
corner := pnt2 copy
]
origin: pnt1 extent: pnt2 [
"Change the top-left corner and the size of the receiver"
<category: 'accessing'>
origin := pnt1 copy.
corner := pnt1 + pnt2
]
right [
"Answer the x of the bottom-right corner of the receiver"
<category: 'accessing'>
^corner x
]
right: aNumber [
"Change the x of the bottom-right corner of the receiver"
<category: 'accessing'>
corner x: aNumber
]
rightCenter [
"Answer the center of the receiver's right side"
<category: 'accessing'>
^corner x @ self yCenter
]
top [
"Answer the y of the receiver's top-left corner"
<category: 'accessing'>
^origin y
]
top: aValue [
"Change the y of the receiver's top-left corner"
<category: 'accessing'>
origin y: aValue
]
topCenter [
"Answer the center of the receiver's top side"
<category: 'accessing'>
^self xCenter @ origin y
]
topLeft [
"Answer the receiver's top-left corner"
<category: 'accessing'>
^origin copy
]
topLeft: aPoint [
"Change the receiver's top-left corner's coordinates to aPoint"
<category: 'accessing'>
origin := aPoint copy
]
topRight [
"Answer the receiver's top-right corner"
<category: 'accessing'>
^corner x @ origin y
]
topRight: aPoint [
"Change the receiver's top-right corner to aPoint"
<category: 'accessing'>
corner x: aPoint x.
origin y: aPoint y
]
width [
"Answer the receiver's width"
<category: 'accessing'>
^self right - self left
]
width: aNumber [
"Change the receiver's width to aNumber"
<category: 'accessing'>
corner x: origin x + aNumber
]
containsPoint: aPoint [
"Answer true if aPoint is equal to, or below and to the right of, the
receiver's origin; and aPoint is above and to the left of the receiver's
corner"
<category: 'testing'>
^aPoint >= origin and: [aPoint < corner]
]
contains: aRectangle [
"Answer true if the receiver contains (see containsPoint:) both
aRectangle's origin and aRectangle's corner"
<category: 'testing'>
^(self containsPoint: aRectangle origin)
and: [self containsPoint: aRectangle corner]
]
intersects: aRectangle [
"Answer true if the receiver intersect aRectangle, i.e. if it contains
(see containsPoint:) any of aRectangle corners or if aRectangle contains
the receiver"
<category: 'testing'>
| selfNorm rectNorm left top right bottom |
selfNorm := self normalized.
rectNorm := aRectangle normalized.
right := selfNorm right min: rectNorm right.
left := selfNorm left max: rectNorm left.
right <= left ifTrue: [^false].
bottom := selfNorm bottom min: rectNorm bottom.
top := selfNorm top max: rectNorm top.
^bottom > top
]
= aRectangle [
"Answer whether the receiver is equal to aRectangle"
<category: 'testing'>
^self class == aRectangle class
and: [origin = aRectangle origin and: [corner = aRectangle corner]]
]
hash [
"Answer an hash value for the receiver"
<category: 'testing'>
^origin hash bitXor: corner hash
]
amountToTranslateWithin: aRectangle [
"Answer a Point so that if aRectangle is translated by that point,
its origin lies within the receiver's."
<category: 'rectangle functions'>
(aRectangle contains: self) ifTrue: [^0 @ 0].
^aRectangle origin - origin
]
translatedToBeWithin: aRectangle [
"Answer a copy of the receiver that does not extend beyond aRectangle."
<category: 'rectangle functions'>
^self translateBy: (self amountToTranslateWithin: aRectangle)
]
area [
"Answer the receiver's area. The area is the width times the height,
so it is possible for it to be negative if the rectangle is not
normalized."
<category: 'rectangle functions'>
^self width * self height
]
areasOutside: aRectangle [
"Answer a collection of rectangles containing the parts of the receiver
outside of aRectangle. For all points in the receiver, but outside
aRectangle, exactly one rectangle in the collection will contain that
point."
"The basic algorithm is to first determine that there is an
intersection by finding the overlapping rectangle. From the
overlapping rectangle, determine which edges it runs along.
For each edge, if it doesn't run along that edge, add a new
rectangle to the collection.
Most times 2 or 3 rectangles get formed, some times 0, 1 or 4."
<category: 'rectangle functions'>
| ansSet l t r b xsect |
xsect := self intersect: aRectangle.
xsect area = 0 ifTrue: [^{self copy}].
ansSet := Set new: 8.
l := self left min: self right.
r := self left max: self right.
t := self top min: self bottom.
b := self top max: self bottom.
l = xsect left ifFalse: [ansSet add: (l @ t corner: xsect left @ b)].
t = xsect top
ifFalse: [ansSet add: (xsect left @ t corner: xsect topRight)].
b = xsect bottom
ifFalse: [ansSet add: (xsect bottomLeft corner: xsect right @ b)].
r = xsect right ifFalse: [ansSet add: (xsect right @ t corner: r @ b)].
^ansSet asArray
]
expandBy: delta [
"Answer a new rectangle that is the receiver expanded by aValue:
if aValue is a rectangle, calculate origin=origin-aValue origin,
corner=corner+aValue corner; else calculate origin=origin-aValue,
corner=corner+aValue."
<category: 'rectangle functions'>
(delta isMemberOf: Point)
ifTrue: [^Rectangle origin: origin - delta corner: corner + delta].
(delta isMemberOf: Rectangle)
ifTrue:
[^Rectangle origin: origin - delta origin corner: corner + delta corner].
(delta isKindOf: Number)
ifTrue:
[^Rectangle
left: origin x - delta
right: corner x + delta
top: origin y - delta
bottom: corner y + delta].
SystemExceptions.WrongClass signalOn: delta
mustBe: #(#{Point} #{Rectangle} #{Number})
]
insetBy: delta [
"Answer a new rectangle that is the receiver inset by aValue:
if aValue is a rectangle, calculate origin=origin+aValue origin,
corner=corner-aValue corner; else calculate origin=origin+aValue,
corner=corner-aValue."
<category: 'rectangle functions'>
(delta isMemberOf: Point)
ifTrue: [^Rectangle origin: origin + delta corner: corner - delta].
(delta isMemberOf: Rectangle)
ifTrue:
[^Rectangle origin: origin + delta origin corner: corner - delta corner].
(delta isKindOf: Number)
ifTrue:
[^Rectangle
left: origin x + delta
right: corner x - delta
top: origin y + delta
bottom: corner y - delta].
SystemExceptions.WrongClass signalOn: delta
mustBe: #(#{Point} #{Rectangle} #{Number})
]
insetOriginBy: originDelta corner: cornerDelta [
"Answer a new rectangle that is the receiver inset so that
origin=origin+originDelta, corner=corner-cornerDelta.
The deltas can be points or numbers"
<category: 'rectangle functions'>
^Rectangle origin: origin + originDelta corner: corner - cornerDelta
]
merge: aRectangle [
"Answer a new rectangle which is the smallest rectangle containing
both the receiver and aRectangle."
<category: 'rectangle functions'>
^Rectangle origin: (origin min: aRectangle origin)
corner: (corner max: aRectangle corner)
]
intersect: aRectangle [
"Answers the rectangle (if any) created by the overlap of
rectangles A and B. Answers nil if the rectangles do not
overlap"
<category: 'rectangle functions'>
| selfNorm rectNorm left top right bottom |
selfNorm := self normalized.
rectNorm := aRectangle normalized.
right := selfNorm right min: rectNorm right.
left := selfNorm left max: rectNorm left.
right <= left ifTrue: [^nil].
bottom := selfNorm bottom min: rectNorm bottom.
top := selfNorm top max: rectNorm top.
bottom <= top ifTrue: [^nil].
^Rectangle origin: left @ top corner: right @ bottom
]
copy [
"Return a deep copy of the receiver for safety."
<category: 'copying'>
^self deepCopy
]
printOn: aStream [
"Print a representation of the receiver on aStream"
<category: 'printing'>
aStream
print: origin;
nextPutAll: ' corner: ';
print: corner
]
storeOn: aStream [
"Store Smalltalk code compiling to the receiver on aStream"
<category: 'printing'>
aStream
nextPutAll: '(Rectangle origin: ';
store: origin;
nextPutAll: ' corner: ';
store: corner;
nextPut: $)
]
normalize [
<category: 'private'>
"Normalize the receiver (make origin < corner)"
| temp |
self right > self left
ifTrue:
[temp := self left.
origin x: corner x.
corner x: temp].
self bottom > self top
ifTrue:
[temp := self top.
origin y: corner y.
corner y: temp]
]
normalized [
"Answer the receiver if it is normalized, otherwise create and
return a copy which is normalized (has origin < corner)"
<category: 'private'>
(self right > self left and: [self bottom > self top]) ifTrue: [^self].
^Rectangle origin: (origin min: corner) corner: (origin max: corner)
]
xCenter [
"Answer the x of the receiver's center"
<category: 'private'>
^(origin x + corner x) / 2
]
yCenter [
"Answer the y of the receiver's center"
<category: 'private'>
^(origin y + corner y) / 2
]
rounded [
"Answer a copy of the receiver with the coordinates rounded to the nearest
integers"
<category: 'truncation and round off'>
^Rectangle origin: origin rounded corner: corner rounded
]
moveBy: aPoint [
"Change the receiver so that the origin and corner are shifted by aPoint"
<category: 'transforming'>
origin := origin + aPoint.
corner := corner + aPoint
]
moveTo: aPoint [
"Change the receiver so that the origin moves to aPoint and the size
remains unchanged"
<category: 'transforming'>
| diff |
diff := aPoint - origin.
origin := aPoint copy.
corner := corner + diff
]
scaleBy: scale [
"Answer a copy of the receiver in which the origin and corner are
multiplied by scale"
<category: 'transforming'>
^Rectangle origin: origin * scale corner: corner * scale
]
translateBy: factor [
"Answer a copy of the receiver in which the origin and corner are shifted
by aPoint"
<category: 'transforming'>
^Rectangle origin: origin + factor corner: corner + factor
]
]
Number extend [
asRectangle [
"Answer an empty rectangle whose origin is (self asPoint)"
<category: 'converting'>
^Rectangle
left: self
top: self
right: self
bottom: self
]
]
Point extend [
asRectangle [
"Answer an empty rectangle whose origin is self"
<category: 'converting'>
^Rectangle origin: self corner: self copy
]
corner: aPoint [
"Answer a Rectangle whose origin is the receiver and whose corner
is aPoint"
<category: 'converting'>
^Rectangle origin: self corner: aPoint
]
extent: aPoint [
"Answer a Rectangle whose origin is the receiver and whose extent
is aPoint"
<category: 'converting'>
^Rectangle origin: self extent: aPoint
]
]
|