1 11 package org.eclipse.jface.util; 12 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.graphics.Point; 15 import org.eclipse.swt.graphics.Rectangle; 16 import org.eclipse.swt.widgets.Control; 17 18 24 public class Geometry { 25 26 31 private Geometry() { 32 } 34 35 46 public static int distanceSquared(Point p1, Point p2) { 47 int term1 = p1.x - p2.x; 48 int term2 = p1.y - p2.y; 49 return term1 * term1 + term2 * term2; 50 } 51 52 59 public static double magnitude(Point p) { 60 return Math.sqrt(magnitudeSquared(p)); 61 } 62 63 71 public static int magnitudeSquared(Point p) { 72 return p.x * p.x + p.y * p.y; 73 } 74 75 83 public static int dotProduct(Point p1, Point p2) { 84 return p1.x * p2.x + p1.y * p2.y; 85 } 86 87 97 public static Point min(Point p1, Point p2) { 98 return new Point(Math.min(p1.x, p2.x), Math.min(p1.y, p2.y)); 99 } 100 101 109 public static Point max(Point p1, Point p2) { 110 return new Point(Math.max(p1.x, p2.x), Math.max(p1.y, p2.y)); 111 } 112 113 124 public static Point getDirectionVector(int distance, int direction) { 125 switch (direction) { 126 case SWT.TOP: 127 return new Point(0, -distance); 128 case SWT.BOTTOM: 129 return new Point(0, distance); 130 case SWT.LEFT: 131 return new Point(-distance, 0); 132 case SWT.RIGHT: 133 return new Point(distance, 0); 134 } 135 136 return new Point(0, 0); 137 } 138 139 146 public static Point centerPoint(Rectangle rect) { 147 return new Point(rect.x + rect.width / 2, rect.y + rect.height / 2); 148 } 149 150 156 public static Point copy(Point toCopy) { 157 return new Point(toCopy.x, toCopy.y); 158 } 159 160 167 public static void set(Point result, Point toCopy) { 168 result.x = toCopy.x; 169 result.y = toCopy.y; 170 } 171 172 179 public static void set(Rectangle result, Rectangle toCopy) { 180 result.x = toCopy.x; 181 result.y = toCopy.y; 182 result.width = toCopy.width; 183 result.height = toCopy.height; 184 } 185 186 210 public static Rectangle subtract(Rectangle rect1, Rectangle rect2) { 211 return new Rectangle(rect1.x - rect2.x, rect1.y - rect2.y, rect1.width - rect2.width, rect1.height - rect2.height); 212 } 213 214 224 public static Rectangle add(Rectangle rect1, Rectangle rect2) { 225 return new Rectangle(rect1.x + rect2.x, rect1.y + rect2.y, 226 rect1.width + rect2.width, rect1.height + rect2.height); 227 } 228 229 238 public static Point add(Point point1, Point point2) { 239 return new Point(point1.x + point2.x, point1.y + point2.y); 240 } 241 242 251 public static Point divide(Point toDivide, int scalar) { 252 return new Point(toDivide.x / scalar, toDivide.y / scalar); 253 } 254 255 256 265 public static Point subtract(Point point1, Point point2) { 266 return new Point(point1.x - point2.x, point1.y - point2.y); 267 } 268 269 275 public static void flipXY(Point toFlip) { 276 int temp = toFlip.x; 277 toFlip.x = toFlip.y; 278 toFlip.y = temp; 279 } 280 281 287 public static void flipXY(Rectangle toFlip) { 288 int temp = toFlip.x; 289 toFlip.x = toFlip.y; 290 toFlip.y = temp; 291 292 temp = toFlip.width; 293 toFlip.width = toFlip.height; 294 toFlip.height = temp; 295 } 296 297 305 public static int getDimension(Rectangle toMeasure, boolean width) { 306 if (width) { 307 return toMeasure.width; 308 } 309 return toMeasure.height; 310 } 311 312 320 public static int getCoordinate(Point toMeasure, boolean width) { 321 return width ? toMeasure.x : toMeasure.y; 322 } 323 324 332 public static int getCoordinate(Rectangle toMeasure, boolean width) { 333 return width ? toMeasure.x : toMeasure.y; 334 } 335 336 344 public static void setDimension(Rectangle toSet, boolean width, int newCoordinate) { 345 if (width) { 346 toSet.width = newCoordinate; 347 } else { 348 toSet.height = newCoordinate; 349 } 350 } 351 352 360 public static void setCoordinate(Rectangle toSet, boolean width, int newCoordinate) { 361 if (width) { 362 toSet.x = newCoordinate; 363 } else { 364 toSet.y = newCoordinate; 365 } 366 } 367 368 376 public static void setCoordinate(Point toSet, boolean width, int newCoordinate) { 377 if (width) { 378 toSet.x = newCoordinate; 379 } else { 380 toSet.y = newCoordinate; 381 } 382 } 383 384 394 public static int getDistanceFromEdge(Rectangle rectangle, Point testPoint, 395 int edgeOfInterest) { 396 switch (edgeOfInterest) { 397 case SWT.TOP: 398 return testPoint.y - rectangle.y; 399 case SWT.BOTTOM: 400 return rectangle.y + rectangle.height - testPoint.y; 401 case SWT.LEFT: 402 return testPoint.x - rectangle.x; 403 case SWT.RIGHT: 404 return rectangle.x + rectangle.width - testPoint.x; 405 } 406 407 return 0; 408 } 409 410 424 public static Rectangle getExtrudedEdge(Rectangle toExtrude, int size, 425 int orientation) { 426 Rectangle bounds = new Rectangle(toExtrude.x, toExtrude.y, 427 toExtrude.width, toExtrude.height); 428 429 if (!isHorizontal(orientation)) { 430 bounds.width = size; 431 } else { 432 bounds.height = size; 433 } 434 435 switch (orientation) { 436 case SWT.RIGHT: 437 bounds.x = toExtrude.x + toExtrude.width - bounds.width; 438 break; 439 case SWT.BOTTOM: 440 bounds.y = toExtrude.y + toExtrude.height - bounds.height; 441 break; 442 } 443 444 normalize(bounds); 445 446 return bounds; 447 } 448 449 457 public static int getOppositeSide(int swtDirectionConstant) { 458 switch (swtDirectionConstant) { 459 case SWT.TOP: 460 return SWT.BOTTOM; 461 case SWT.BOTTOM: 462 return SWT.TOP; 463 case SWT.LEFT: 464 return SWT.RIGHT; 465 case SWT.RIGHT: 466 return SWT.LEFT; 467 } 468 469 return swtDirectionConstant; 470 } 471 472 479 public static int getSwtHorizontalOrVerticalConstant(boolean horizontal) { 480 if (horizontal) { 481 return SWT.HORIZONTAL; 482 } 483 return SWT.VERTICAL; 484 } 485 486 495 public static boolean isHorizontal(int swtSideConstant) { 496 return !(swtSideConstant == SWT.LEFT || swtSideConstant == SWT.RIGHT); 497 } 498 499 506 public static void moveRectangle(Rectangle rect, Point delta) { 507 rect.x += delta.x; 508 rect.y += delta.y; 509 } 510 511 520 public static void expand(Rectangle rect, Rectangle differenceRect) { 521 rect.x += differenceRect.x; 522 rect.y += differenceRect.y; 523 rect.height = Math.max(0, rect.height + differenceRect.height); 524 rect.width = Math.max(0, rect.width + differenceRect.width); 525 } 526 527 551 public static Rectangle createDiffRectangle(int left, int right, int top, int bottom) { 552 return new Rectangle(-left, -top, left + right, top + bottom); 553 } 554 555 567 public static void expand(Rectangle rect, int left, int right, int top, int bottom) { 568 rect.x -= left; 569 rect.width = Math.max(0, rect.width + left + right); 570 rect.y -= top; 571 rect.height = Math.max(0, rect.height + top + bottom); 572 } 573 574 583 public static void normalize(Rectangle rect) { 584 if (rect.width < 0) { 585 rect.width = -rect.width; 586 rect.x -= rect.width; 587 } 588 589 if (rect.height < 0) { 590 rect.height = -rect.height; 591 rect.y -= rect.height; 592 } 593 } 594 595 604 public static Rectangle toControl(Control coordinateSystem, 605 Rectangle toConvert) { 606 return(coordinateSystem.getDisplay().map 607 (null,coordinateSystem,toConvert)); 608 } 609 610 619 public static Rectangle toDisplay(Control coordinateSystem, 620 Rectangle toConvert) { 621 return(coordinateSystem.getDisplay().map 622 (coordinateSystem,null,toConvert)); 623 624 } 625 626 640 public static int getRelativePosition(Rectangle boundary, Point toTest) { 641 int result = 0; 642 643 if (toTest.x < boundary.x) { 644 result |= SWT.LEFT; 645 } else if (toTest.x >= boundary.x + boundary.width) { 646 result |= SWT.RIGHT; 647 } 648 649 if (toTest.y < boundary.y) { 650 result |= SWT.TOP; 651 } else if (toTest.y >= boundary.y + boundary.height) { 652 result |= SWT.BOTTOM; 653 } 654 655 return result; 656 } 657 658 669 public static int getDistanceFrom(Rectangle boundary, Point toTest) { 670 int side = getClosestSide(boundary, toTest); 671 return getDistanceFromEdge(boundary, toTest, side); 672 } 673 674 684 public static int getClosestSide(Rectangle boundary, Point toTest) { 685 int[] sides = new int[] { SWT.LEFT, SWT.RIGHT, SWT.TOP, SWT.BOTTOM }; 686 687 int closestSide = SWT.LEFT; 688 int closestDistance = Integer.MAX_VALUE; 689 690 for (int idx = 0; idx < sides.length; idx++) { 691 int side = sides[idx]; 692 693 int distance = getDistanceFromEdge(boundary, toTest, side); 694 695 if (distance < closestDistance) { 696 closestDistance = distance; 697 closestSide = side; 698 } 699 } 700 701 return closestSide; 702 } 703 704 711 public static Rectangle copy(Rectangle toCopy) { 712 return new Rectangle(toCopy.x, toCopy.y, toCopy.width, toCopy.height); 713 } 714 715 722 public static Point getSize(Rectangle rectangle) { 723 return new Point(rectangle.width, rectangle.height); 724 } 725 726 733 public static void setSize(Rectangle rectangle, Point newSize) { 734 rectangle.width = newSize.x; 735 rectangle.height = newSize.y; 736 } 737 738 748 public static void setLocation(Rectangle rectangle, Point newSize) { 749 rectangle.width = newSize.x; 750 rectangle.height = newSize.y; 751 } 752 753 763 public static Point getLocation(Rectangle toQuery) { 764 return new Point(toQuery.x, toQuery.y); 765 } 766 767 777 public static Rectangle createRectangle(Point position, Point size) { 778 return new Rectangle(position.x, position.y, size.x, size.y); 779 } 780 781 789 public static void moveInside(Rectangle inner, Rectangle outer) { 790 if (inner.x < outer.x) { 792 inner.x = outer.x; 793 } 794 if ((inner.x + inner.width) > (outer.x + outer.width)) { 795 inner.x -= (inner.x + inner.width) - (outer.x + outer.width); 796 } 797 798 if (inner.y < outer.y) { 800 inner.y = outer.y; 801 } 802 if ((inner.y + inner.height) > (outer.y + outer.height)) { 803 inner.y -= (inner.y + inner.height) - (outer.y + outer.height); 804 } 805 } 806 807 } 808 | Popular Tags |