public class Geometry extends Object
Modifier and Type | Method and 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
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();
|
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.
|
public static int distanceSquared(Point p1, Point p2)
This is preferred over the real distance when searching for the closest point, since it avoids square roots.
p1
- first endpointp2
- second endpointpublic static double magnitude(Point p)
p
- point representing the 2d vector whose magnitude is being computedpublic static int magnitudeSquared(Point p)
p
- the point whose magnitude is being computedpublic static int dotProduct(Point p1, Point p2)
p1
- the first vectorp2
- the second vectorpublic static Point min(Point p1, Point p2)
p1
- a Pointp2
- a Pointpublic static Point max(Point p1, Point p2)
p1
- a Pointp2
- a Pointpublic static Point getDirectionVector(int distance, int direction)
distance
- magnitude of the vectordirection
- one of SWT.TOP, SWT.BOTTOM, SWT.LEFT, or SWT.RIGHTpublic static Point centerPoint(Rectangle rect)
rect
- rectangle being computedpublic static Point copy(Point toCopy)
toCopy
- point to copypublic static void set(Point result, Point toCopy)
result
- object that will be modifiedtoCopy
- object that will be copiedpublic static void set(Rectangle result, Rectangle toCopy)
result
- object that will be modifiedtoCopy
- object that will be copiedpublic 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();
rect1
- first rectanglerect2
- rectangle to subtractpublic 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.
rect1
- first rectangle to addrect2
- second rectangle to addpublic static Point add(Point point1, Point point2)
point1
- the first point (not null)point2
- the second point (not null)public static Point divide(Point toDivide, int scalar)
toDivide
- point to dividescalar
- denominatorpublic static Point subtract(Point point1, Point point2)
point1
- initial pointpoint2
- vector to subtractpublic static void flipXY(Point toFlip)
toFlip
- modifies this pointpublic static void flipXY(Rectangle toFlip)
toFlip
- modifies this rectanglepublic static int getDimension(Rectangle toMeasure, boolean width)
toMeasure
- rectangle to measurewidth
- returns the width if true, and the height if falsepublic static int getCoordinate(Point toMeasure, boolean width)
toMeasure
- point being measuredwidth
- if true, returns x. Otherwise, returns y.public static int getCoordinate(Rectangle toMeasure, boolean width)
toMeasure
- rectangle being measuredwidth
- if true, returns x. Otherwise, returns y.public static void setDimension(Rectangle toSet, boolean width, int newCoordinate)
toSet
- rectangle to modifywidth
- if true, the width is modified. If false, the height is modified.newCoordinate
- new value of the width or heightpublic static void setCoordinate(Rectangle toSet, boolean width, int newCoordinate)
toSet
- rectangle to modifywidth
- if true, the x coordinate is modified. If false, the y coordinate is modified.newCoordinate
- new value of the x or y coordinatespublic static void setCoordinate(Point toSet, boolean width, int newCoordinate)
toSet
- point to modifywidth
- if true, the x coordinate is modified. If false, the y coordinate is modified.newCoordinate
- new value of the x or y coordinatespublic static int getDistanceFromEdge(Rectangle rectangle, Point testPoint, int edgeOfInterest)
rectangle
- a bounding rectangletestPoint
- a point to testedgeOfInterest
- side of the rectangle to test againstpublic static Rectangle getExtrudedEdge(Rectangle toExtrude, int size, int orientation)
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.public static int getOppositeSide(int swtDirectionConstant)
swtDirectionConstant
- one of SWT.LEFT, SWT.RIGHT, SWT.TOP, or SWT.BOTTOMpublic static int getSwtHorizontalOrVerticalConstant(boolean horizontal)
horizontal
- if true, returns SWT.HORIZONTAL. If false, returns SWT.VERTICALpublic static boolean isHorizontal(int swtSideConstant)
swtSideConstant
- one of SWT.TOP, SWT.BOTTOM, SWT.LEFT, or SWT.RIGHTpublic static void moveRectangle(Rectangle rect, Point delta)
rect
- rectangle to move (will be modified)delta
- direction vector to move the rectangle bypublic static void expand(Rectangle rect, Rectangle differenceRect)
rect
- normalized rectangle to modifydifferenceRect
- difference rectangle to be added to rectpublic 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();
someControl.setBounds(Geometry.add(bounds, margins));
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)public static void expand(Rectangle rect, int left, int right, int top, int bottom)
rect
- normalized rectangle to modifyleft
- 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)public static void normalize(Rectangle rect)
rect
- rectangle to modifypublic static Rectangle toControl(Control coordinateSystem, Rectangle toConvert)
coordinateSystem
- local coordinate system being converted totoConvert
- rectangle to convertpublic static Rectangle toDisplay(Control coordinateSystem, Rectangle toConvert)
coordinateSystem
- local coordinate system being converted fromtoConvert
- rectangle to convertpublic static int getRelativePosition(Rectangle boundary, Point toTest)
boundary
- normalized boundary rectangletoTest
- point whose relative position to the rectangle is being computedpublic static int getDistanceFrom(Rectangle boundary, Point toTest)
boundary
- rectangle to testtoTest
- point to testpublic static int getClosestSide(Rectangle boundary, Point toTest)
boundary
- rectangle to testtoTest
- point to comparepublic static Rectangle copy(Rectangle toCopy)
toCopy
- rectangle to copypublic static Point getSize(Rectangle rectangle)
rectangle
- rectangle whose size is being computedpublic static void setSize(Rectangle rectangle, Point newSize)
rectangle
- rectangle to modifynewSize
- new size of the rectanglepublic static void setLocation(Rectangle rectangle, Point newLocation)
rectangle
- rectangle to modifynewLocation
- new location of the rectanglepublic static Point getLocation(Rectangle toQuery)
toQuery
- rectangle to querypublic static Rectangle createRectangle(Point position, Point size)
position
- the (x,y) position of the rectanglesize
- the size of the new rectangle, where (x,y) -> (width, height)public static void moveInside(Rectangle inner, Rectangle outer)
inner
- The 'inner' rectangle to be repositioned (should be smaller than the 'outer' rectangle)outer
- The 'outer' rectangle
Copyright (c) 2000, 2015 Eclipse Contributors and others. All rights reserved.Guidelines for using Eclipse APIs.