## Class Geometry

• ```public class Geometry
extends Object```
Contains static methods for performing simple geometric operations on the SWT geometry classes.
Since:
3.0
• ### Method Summary

All Methods
Modifier and Type Method Description
`static Point` ```add​(Point point1, Point point2)```
Adds two points as 2d vectors.
`static Rectangle` ```add​(Rectangle rect1, Rectangle rect2)```
Returns a new Rectangle whose x, y, width, and height is the sum of the x, y, width, and height values of both rectangles respectively.
`static Point` `centerPoint​(Rectangle rect)`
Returns the point in the center of the given rectangle.
`static Point` `copy​(Point toCopy)`
Returns a copy of the given point
`static Rectangle` `copy​(Rectangle toCopy)`
Returns a copy of the given rectangle
`static Rectangle` ```createDiffRectangle​(int left, int right, int top, int bottom)```
Returns a rectangle which, when added to another rectangle, will expand each side by the given number of units.
`static Rectangle` ```createRectangle​(Point position, Point size)```
Returns a new rectangle with the given position and dimensions, expressed as points.
`static int` ```distanceSquared​(Point p1, Point p2)```
Returns the square of the distance between two points.
`static Point` ```divide​(Point toDivide, int scalar)```
Divides both coordinates of the given point by the given scalar.
`static int` ```dotProduct​(Point p1, Point p2)```
Returns the dot product of the given vectors (expressed as Points)
`static void` ```expand​(Rectangle rect, int left, int right, int top, int bottom)```
Moves each edge of the given rectangle outward by the given amount.
`static void` ```expand​(Rectangle rect, Rectangle differenceRect)```
Moves each edge of the given rectangle outward by the given amount.
`static void` `flipXY​(Point toFlip)`
Swaps the X and Y coordinates of the given point.
`static void` `flipXY​(Rectangle toFlip)`
Swaps the X and Y coordinates of the given rectangle, along with the height and width.
`static int` ```getClosestSide​(Rectangle boundary, Point toTest)```
Returns the edge of the given rectangle is closest to the given point.
`static int` ```getCoordinate​(Point toMeasure, boolean width)```
Returns the x or y coordinates of the given point.
`static int` ```getCoordinate​(Rectangle toMeasure, boolean width)```
Returns the x or y coordinates of the given rectangle.
`static int` ```getDimension​(Rectangle toMeasure, boolean width)```
Returns the height or width of the given rectangle.
`static Point` ```getDirectionVector​(int distance, int direction)```
Returns a vector in the given direction with the given magnitude.
`static int` ```getDistanceFrom​(Rectangle boundary, Point toTest)```
Returns the distance from the point to the nearest edge of the given rectangle.
`static int` ```getDistanceFromEdge​(Rectangle rectangle, Point testPoint, int edgeOfInterest)```
Returns the distance of the given point from a particular side of the given rectangle.
`static Rectangle` ```getExtrudedEdge​(Rectangle toExtrude, int size, int orientation)```
Extrudes the given edge inward by the given distance.
`static Point` `getLocation​(Rectangle toQuery)`
Returns the x,y position of the given rectangle.
`static int` `getOppositeSide​(int swtDirectionConstant)`
Returns the opposite of the given direction.
`static int` ```getRelativePosition​(Rectangle boundary, Point toTest)```
Determines where the given point lies with respect to the given rectangle.
`static Point` `getSize​(Rectangle rectangle)`
Returns the size of the rectangle, as a Point
`static int` `getSwtHorizontalOrVerticalConstant​(boolean horizontal)`
Converts the given boolean into an SWT orientation constant.
`static boolean` `isHorizontal​(int swtSideConstant)`
Returns true iff the given SWT side constant corresponds to a horizontal side of a rectangle.
`static double` `magnitude​(Point p)`
Returns the magnitude of the given 2d vector (represented as a Point)
`static int` `magnitudeSquared​(Point p)`
Returns the square of the magnitude of the given 2-space vector (represented using a point)
`static Point` ```max​(Point p1, Point p2)```
Returns a new point whose coordinates are the maximum of the coordinates of the given points
`static Point` ```min​(Point p1, Point p2)```
Returns a new point whose coordinates are the minimum of the coordinates of the given points
`static void` ```moveInside​(Rectangle inner, Rectangle outer)```
Repositions the 'inner' rectangle to lie completely within the bounds of the 'outer' rectangle if possible.
`static void` ```moveRectangle​(Rectangle rect, Point delta)```
Moves the given rectangle by the given delta.
`static void` `normalize​(Rectangle rect)`
Normalizes the given rectangle.
`static void` ```set​(Point result, Point toCopy)```
Sets result equal to toCopy
`static void` ```set​(Rectangle result, Rectangle toCopy)```
Sets result equal to toCopy
`static void` ```setCoordinate​(Point toSet, boolean width, int newCoordinate)```
Sets one coordinate of the given point.
`static void` ```setCoordinate​(Rectangle toSet, boolean width, int newCoordinate)```
Sets one coordinate of the given rectangle.
`static void` ```setDimension​(Rectangle toSet, boolean width, int newCoordinate)```
Sets one dimension of the given rectangle.
`static void` ```setLocation​(Rectangle rectangle, Point newLocation)```
Sets the x,y position of the given rectangle.
`static void` ```setSize​(Rectangle rectangle, Point newSize)```
Sets the size of the given rectangle to the given size
`static Point` ```subtract​(Point point1, Point point2)```
Performs vector subtraction on two points.
`static Rectangle` ```subtract​(Rectangle rect1, Rectangle rect2)```
Returns a new difference Rectangle whose x, y, width, and height are equal to the difference of the corresponding attributes from the given rectangles
`static Rectangle` ```toControl​(Control coordinateSystem, Rectangle toConvert)```
Converts the given rectangle from display coordinates to the local coordinate system of the given object into display coordinates.
`static Rectangle` ```toDisplay​(Control coordinateSystem, Rectangle toConvert)```
Converts the given rectangle from the local coordinate system of the given object into display coordinates.
• ### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
• ### Method Detail

• #### distanceSquared

```public static int distanceSquared​(Point p1,
Point p2)```
Returns the square of the distance between two points.

This is preferred over the real distance when searching for the closest point, since it avoids square roots.

Parameters:
`p1` - first endpoint
`p2` - second endpoint
Returns:
the square of the distance between the two points
Since:
3.0
• #### magnitude

`public static double magnitude​(Point p)`
Returns the magnitude of the given 2d vector (represented as a Point)
Parameters:
`p` - point representing the 2d vector whose magnitude is being computed
Returns:
the magnitude of the given 2d vector
Since:
3.0
• #### magnitudeSquared

`public static int magnitudeSquared​(Point p)`
Returns the square of the magnitude of the given 2-space vector (represented using a point)
Parameters:
`p` - the point whose magnitude is being computed
Returns:
the square of the magnitude of the given vector
Since:
3.0
• #### dotProduct

```public static int dotProduct​(Point p1,
Point p2)```
Returns the dot product of the given vectors (expressed as Points)
Parameters:
`p1` - the first vector
`p2` - the second vector
Returns:
the dot product of the two vectors
Since:
3.0
• #### min

```public static Point min​(Point p1,
Point p2)```
Returns a new point whose coordinates are the minimum of the coordinates of the given points
Parameters:
`p1` - a Point
`p2` - a Point
Returns:
a new point whose coordinates are the minimum of the coordinates of the given points
Since:
3.0
• #### max

```public static Point max​(Point p1,
Point p2)```
Returns a new point whose coordinates are the maximum of the coordinates of the given points
Parameters:
`p1` - a Point
`p2` - a Point
Returns:
point a new point whose coordinates are the maximum of the coordinates
Since:
3.0
• #### getDirectionVector

```public static Point getDirectionVector​(int distance,
int direction)```
Returns a vector in the given direction with the given magnitude. Directions are given using SWT direction constants, and the resulting vector is in the screen's coordinate system. That is, the vector (0, 1) is down and the vector (1, 0) is right.
Parameters:
`distance` - magnitude of the vector
`direction` - one of SWT.TOP, SWT.BOTTOM, SWT.LEFT, or SWT.RIGHT
Returns:
a point representing a vector in the given direction with the given magnitude
Since:
3.0
• #### centerPoint

`public static Point centerPoint​(Rectangle rect)`
Returns the point in the center of the given rectangle.
Parameters:
`rect` - rectangle being computed
Returns:
a Point at the center of the given rectangle.
Since:
3.0
• #### copy

`public static Point copy​(Point toCopy)`
Returns a copy of the given point
Parameters:
`toCopy` - point to copy
Returns:
a copy of the given point
• #### set

```public static void set​(Point result,
Point toCopy)```
Sets result equal to toCopy
Parameters:
`result` - object that will be modified
`toCopy` - object that will be copied
Since:
3.1
• #### set

```public static void set​(Rectangle result,
Rectangle toCopy)```
Sets result equal to toCopy
Parameters:
`result` - object that will be modified
`toCopy` - object that will be copied
Since:
3.1
• #### subtract

```public static Rectangle subtract​(Rectangle rect1,
Rectangle rect2)```

Returns a new difference Rectangle whose x, y, width, and height are equal to the difference of the corresponding attributes from the given rectangles

Example: Compute the margins for a given Composite, and apply those same margins to a new GridLayout
``` ```
// Compute the client area, in the coordinate system of the input composite's parent
Rectangle clientArea = Display.getCurrent().map(inputComposite,
inputComposite.getParent(), inputComposite.getClientArea());

// Compute the margins for a given Composite by subtracting the client area from the composite's bounds
Rectangle margins = Geometry.subtract(inputComposite.getBounds(), clientArea);

// Now apply these margins to a new GridLayout
GridLayout layout = GridLayoutFactory.fillDefaults().margins(margins).create();
```
```
Parameters:
`rect1` - first rectangle
`rect2` - rectangle to subtract
Returns:
the difference between the two rectangles (computed as rect1 - rect2)
Since:
3.3

```public static Rectangle add​(Rectangle rect1,
Rectangle rect2)```

Returns a new Rectangle whose x, y, width, and height is the sum of the x, y, width, and height values of both rectangles respectively.

Parameters:
`rect1` - first rectangle to add
`rect2` - second rectangle to add
Returns:
a new rectangle whose x, y, height, and width attributes are the sum of the corresponding attributes from the arguments.
Since:
3.3

```public static Point add​(Point point1,
Point point2)```
Adds two points as 2d vectors. Returns a new point whose coordinates are the sum of the original two points.
Parameters:
`point1` - the first point (not null)
`point2` - the second point (not null)
Returns:
a new point whose coordinates are the sum of the given points
Since:
3.0
• #### divide

```public static Point divide​(Point toDivide,
int scalar)```
Divides both coordinates of the given point by the given scalar.
Parameters:
`toDivide` - point to divide
`scalar` - denominator
Returns:
a new Point whose coordinates are equal to the original point divided by the scalar
Since:
3.1
• #### subtract

```public static Point subtract​(Point point1,
Point point2)```
Performs vector subtraction on two points. Returns a new point equal to (point1 - point2).
Parameters:
`point1` - initial point
`point2` - vector to subtract
Returns:
the difference (point1 - point2)
Since:
3.0
• #### flipXY

`public static void flipXY​(Point toFlip)`
Swaps the X and Y coordinates of the given point.
Parameters:
`toFlip` - modifies this point
Since:
3.1
• #### flipXY

`public static void flipXY​(Rectangle toFlip)`
Swaps the X and Y coordinates of the given rectangle, along with the height and width.
Parameters:
`toFlip` - modifies this rectangle
Since:
3.1
• #### getDimension

```public static int getDimension​(Rectangle toMeasure,
boolean width)```
Returns the height or width of the given rectangle.
Parameters:
`toMeasure` - rectangle to measure
`width` - returns the width if true, and the height if false
Returns:
the width or height of the given rectangle
Since:
3.0
• #### getCoordinate

```public static int getCoordinate​(Point toMeasure,
boolean width)```
Returns the x or y coordinates of the given point.
Parameters:
`toMeasure` - point being measured
`width` - if true, returns x. Otherwise, returns y.
Returns:
the x or y coordinate
Since:
3.1
• #### getCoordinate

```public static int getCoordinate​(Rectangle toMeasure,
boolean width)```
Returns the x or y coordinates of the given rectangle.
Parameters:
`toMeasure` - rectangle being measured
`width` - if true, returns x. Otherwise, returns y.
Returns:
the x or y coordinate
Since:
3.1
• #### setDimension

```public static void setDimension​(Rectangle toSet,
boolean width,
int newCoordinate)```
Sets one dimension of the given rectangle. Modifies the given rectangle.
Parameters:
`toSet` - rectangle to modify
`width` - if true, the width is modified. If false, the height is modified.
`newCoordinate` - new value of the width or height
Since:
3.1
• #### setCoordinate

```public static void setCoordinate​(Rectangle toSet,
boolean width,
int newCoordinate)```
Sets one coordinate of the given rectangle. Modifies the given rectangle.
Parameters:
`toSet` - rectangle to modify
`width` - if true, the x coordinate is modified. If false, the y coordinate is modified.
`newCoordinate` - new value of the x or y coordinates
Since:
3.1
• #### setCoordinate

```public static void setCoordinate​(Point toSet,
boolean width,
int newCoordinate)```
Sets one coordinate of the given point. Modifies the given point.
Parameters:
`toSet` - point to modify
`width` - if true, the x coordinate is modified. If false, the y coordinate is modified.
`newCoordinate` - new value of the x or y coordinates
Since:
3.1
• #### getDistanceFromEdge

```public static int getDistanceFromEdge​(Rectangle rectangle,
Point testPoint,
int edgeOfInterest)```
Returns the distance of the given point from a particular side of the given rectangle. Returns negative values for points outside the rectangle.
Parameters:
`rectangle` - a bounding rectangle
`testPoint` - a point to test
`edgeOfInterest` - side of the rectangle to test against
Returns:
the distance of the given point from the given edge of the rectangle
Since:
3.0
• #### getExtrudedEdge

```public static Rectangle getExtrudedEdge​(Rectangle toExtrude,
int size,
int orientation)```
Extrudes the given edge inward by the given distance. That is, if one side of the rectangle was sliced off with a given thickness, this returns the rectangle that forms the slice. Note that the returned rectangle will be inside the given rectangle if size > 0.
Parameters:
`toExtrude` - the rectangle to extrude. The resulting rectangle will share three sides with this rectangle.
`size` - distance to extrude. A negative size will extrude outwards (that is, the resulting rectangle will overlap the original iff this is positive).
`orientation` - the side to extrude. One of SWT.LEFT, SWT.RIGHT, SWT.TOP, or SWT.BOTTOM. The resulting rectangle will always share this side with the original rectangle.
Returns:
a rectangle formed by extruding the given side of the rectangle by the given distance.
Since:
3.0
• #### getOppositeSide

`public static int getOppositeSide​(int swtDirectionConstant)`
Returns the opposite of the given direction. That is, returns SWT.LEFT if given SWT.RIGHT and visa-versa.
Parameters:
`swtDirectionConstant` - one of SWT.LEFT, SWT.RIGHT, SWT.TOP, or SWT.BOTTOM
Returns:
one of SWT.LEFT, SWT.RIGHT, SWT.TOP, or SWT.BOTTOM
Since:
3.0
• #### getSwtHorizontalOrVerticalConstant

`public static int getSwtHorizontalOrVerticalConstant​(boolean horizontal)`
Converts the given boolean into an SWT orientation constant.
Parameters:
`horizontal` - if true, returns SWT.HORIZONTAL. If false, returns SWT.VERTICAL
Returns:
SWT.HORIZONTAL or SWT.VERTICAL.
Since:
3.0
• #### isHorizontal

`public static boolean isHorizontal​(int swtSideConstant)`
Returns true iff the given SWT side constant corresponds to a horizontal side of a rectangle. That is, returns true for the top and bottom but false for the left and right.
Parameters:
`swtSideConstant` - one of SWT.TOP, SWT.BOTTOM, SWT.LEFT, or SWT.RIGHT
Returns:
true iff the given side is horizontal.
Since:
3.0
• #### moveRectangle

```public static void moveRectangle​(Rectangle rect,
Point delta)```
Moves the given rectangle by the given delta.
Parameters:
`rect` - rectangle to move (will be modified)
`delta` - direction vector to move the rectangle by
Since:
3.0
• #### expand

```public static void expand​(Rectangle rect,
Rectangle differenceRect)```
Moves each edge of the given rectangle outward by the given amount. Negative values cause the rectangle to contract. Does not allow the rectangle's width or height to be reduced below zero.
Parameters:
`rect` - normalized rectangle to modify
`differenceRect` - difference rectangle to be added to rect
Since:
3.3
• #### createDiffRectangle

```public static Rectangle createDiffRectangle​(int left,
int right,
int top,
int bottom)```

Returns a rectangle which, when added to another rectangle, will expand each side by the given number of units.

This is commonly used to store margin sizes. For example:

``` ```
// Expands the left, right, top, and bottom
// of the given control by 10, 5, 1, and 15 units respectively

Rectangle margins = Geometry.createDifferenceRect(10,5,1,15);
Rectangle bounds = someControl.getBounds();
```
```
Parameters:
`left` - distance to expand the left side (negative values move the edge inward)
`right` - distance to expand the right side (negative values move the edge inward)
`top` - distance to expand the top (negative values move the edge inward)
`bottom` - distance to expand the bottom (negative values move the edge inward)
Returns:
a difference rectangle that, when added to another rectangle, will cause each side to expand by the given number of units
Since:
3.3
• #### expand

```public static void expand​(Rectangle rect,
int left,
int right,
int top,
int bottom)```
Moves each edge of the given rectangle outward by the given amount. Negative values cause the rectangle to contract. Does not allow the rectangle's width or height to be reduced below zero.
Parameters:
`rect` - normalized rectangle to modify
`left` - distance to move the left edge outward (negative values move the edge inward)
`right` - distance to move the right edge outward (negative values move the edge inward)
`top` - distance to move the top edge outward (negative values move the edge inward)
`bottom` - distance to move the bottom edge outward (negative values move the edge inward)
Since:
3.1
• #### normalize

`public static void normalize​(Rectangle rect)`
Normalizes the given rectangle. That is, any rectangle with negative width or height becomes a rectangle with positive width or height that extends to the upper-left of the original rectangle.
Parameters:
`rect` - rectangle to modify
Since:
3.0
• #### toControl

```public static Rectangle toControl​(Control coordinateSystem,
Rectangle toConvert)```
Converts the given rectangle from display coordinates to the local coordinate system of the given object into display coordinates.
Parameters:
`coordinateSystem` - local coordinate system being converted to
`toConvert` - rectangle to convert
Returns:
a rectangle in control coordinates
Since:
3.0
• #### toDisplay

```public static Rectangle toDisplay​(Control coordinateSystem,
Rectangle toConvert)```
Converts the given rectangle from the local coordinate system of the given object into display coordinates.
Parameters:
`coordinateSystem` - local coordinate system being converted from
`toConvert` - rectangle to convert
Returns:
a rectangle in display coordinates
Since:
3.0
• #### getRelativePosition

```public static int getRelativePosition​(Rectangle boundary,
Point toTest)```
Determines where the given point lies with respect to the given rectangle. Returns a combination of SWT.LEFT, SWT.RIGHT, SWT.TOP, and SWT.BOTTOM, combined with bitwise or (for example, returns SWT.TOP | SWT.LEFT if the point is to the upper-left of the rectangle). Returns 0 if the point lies within the rectangle. Positions are in screen coordinates (ie: a point is to the upper-left of the rectangle if its x and y coordinates are smaller than any point in the rectangle)
Parameters:
`boundary` - normalized boundary rectangle
`toTest` - point whose relative position to the rectangle is being computed
Returns:
one of SWT.LEFT | SWT.TOP, SWT.TOP, SWT.RIGHT | SWT.TOP, SWT.LEFT, 0, SWT.RIGHT, SWT.LEFT | SWT.BOTTOM, SWT.BOTTOM, SWT.RIGHT | SWT.BOTTOM
Since:
3.0
• #### getDistanceFrom

```public static int getDistanceFrom​(Rectangle boundary,
Point toTest)```
Returns the distance from the point to the nearest edge of the given rectangle. Returns negative values if the point lies outside the rectangle.
Parameters:
`boundary` - rectangle to test
`toTest` - point to test
Returns:
the distance between the given point and the nearest edge of the rectangle. Returns positive values for points inside the rectangle and negative values for points outside the rectangle.
Since:
3.1
• #### getClosestSide

```public static int getClosestSide​(Rectangle boundary,
Point toTest)```
Returns the edge of the given rectangle is closest to the given point.
Parameters:
`boundary` - rectangle to test
`toTest` - point to compare
Returns:
one of SWT.LEFT, SWT.RIGHT, SWT.TOP, or SWT.BOTTOM
Since:
3.0
• #### copy

`public static Rectangle copy​(Rectangle toCopy)`
Returns a copy of the given rectangle
Parameters:
`toCopy` - rectangle to copy
Returns:
a copy of the given rectangle
Since:
3.0
• #### getSize

`public static Point getSize​(Rectangle rectangle)`
Returns the size of the rectangle, as a Point
Parameters:
`rectangle` - rectangle whose size is being computed
Returns:
the size of the given rectangle
Since:
3.0
• #### setSize

```public static void setSize​(Rectangle rectangle,
Point newSize)```
Sets the size of the given rectangle to the given size
Parameters:
`rectangle` - rectangle to modify
`newSize` - new size of the rectangle
Since:
3.0
• #### setLocation

```public static void setLocation​(Rectangle rectangle,
Point newLocation)```
Sets the x,y position of the given rectangle. For a normalized rectangle (a rectangle with positive width and height), this will be the upper-left corner of the rectangle.
Parameters:
`rectangle` - rectangle to modify
`newLocation` - new location of the rectangle
Since:
3.0
• #### getLocation

`public static Point getLocation​(Rectangle toQuery)`
Returns the x,y position of the given rectangle. For normalized rectangles (rectangles with positive width and height), this is the upper-left corner of the rectangle.
Parameters:
`toQuery` - rectangle to query
Returns:
a Point containing the x,y position of the rectangle
Since:
3.0
• #### createRectangle

```public static Rectangle createRectangle​(Point position,
Point size)```
Returns a new rectangle with the given position and dimensions, expressed as points.
Parameters:
`position` - the (x,y) position of the rectangle
`size` - the size of the new rectangle, where (x,y) -> (width, height)
Returns:
a new Rectangle with the given position and size
Since:
3.0
• #### moveInside

```public static void moveInside​(Rectangle inner,
Rectangle outer)```
Repositions the 'inner' rectangle to lie completely within the bounds of the 'outer' rectangle if possible. One use for this is to ensure that, when setting a control's bounds, that they will always lie within its parent's client area (to avoid clipping).
Parameters:
`inner` - The 'inner' rectangle to be repositioned (should be smaller than the 'outer' rectangle)
`outer` - The 'outer' rectangle