Package org.eclipse.jface.util
Class Geometry
java.lang.Object
org.eclipse.jface.util.Geometry
Contains static methods for performing simple geometric operations
on the SWT geometry classes.
- Since:
- 3.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic Point
Adds two points as 2d vectors.static Rectangle
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
Returns a copy of the given pointstatic Rectangle
Returns a copy of the given rectanglestatic 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
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
Moves each edge of the given rectangle outward by the given amount.static void
Moves each edge of the given rectangle outward by the given amount.static void
Swaps the X and Y coordinates of the given point.static void
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
Returns the size of the rectangle, as a Pointstatic 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
Returns the magnitude of the given 2d vector (represented as a Point)static int
Returns the square of the magnitude of the given 2-space vector (represented using a point)static Point
Returns a new point whose coordinates are the maximum of the coordinates of the given pointsstatic Point
Returns a new point whose coordinates are the minimum of the coordinates of the given pointsstatic 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
Normalizes the given rectangle.static void
Sets result equal to toCopystatic void
Sets result equal to toCopystatic 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
Sets the size of the given rectangle to the given sizestatic Point
Performs vector subtraction on two points.static Rectangle
Returns a new difference Rectangle whose x, y, width, and height are equal to the difference of the corresponding attributes from the given rectanglesstatic Rectangle
Converts the given rectangle from display coordinates to the local coordinate system of the given object into display coordinates.static Rectangle
Converts the given rectangle from the local coordinate system of the given object into display coordinates.
-
Method Details
-
distanceSquared
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 endpointp2
- second endpoint- Returns:
- the square of the distance between the two points
- Since:
- 3.0
-
magnitude
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
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
Returns the dot product of the given vectors (expressed as Points)- Parameters:
p1
- the first vectorp2
- the second vector- Returns:
- the dot product of the two vectors
- Since:
- 3.0
-
min
Returns a new point whose coordinates are the minimum of the coordinates of the given points- Parameters:
p1
- a Pointp2
- a Point- Returns:
- a new point whose coordinates are the minimum of the coordinates of the given points
- Since:
- 3.0
-
max
Returns a new point whose coordinates are the maximum of the coordinates of the given points- Parameters:
p1
- a Pointp2
- a Point- Returns:
- point a new point whose coordinates are the maximum of the coordinates
- Since:
- 3.0
-
getDirectionVector
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 vectordirection
- 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
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
Returns a copy of the given point- Parameters:
toCopy
- point to copy- Returns:
- a copy of the given point
-
set
Sets result equal to toCopy- Parameters:
result
- object that will be modifiedtoCopy
- object that will be copied- Since:
- 3.1
-
set
Sets result equal to toCopy- Parameters:
result
- object that will be modifiedtoCopy
- object that will be copied- Since:
- 3.1
-
subtract
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 rectanglerect2
- rectangle to subtract- Returns:
- the difference between the two rectangles (computed as rect1 - rect2)
- Since:
- 3.3
-
add
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 addrect2
- 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
-
add
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
Divides both coordinates of the given point by the given scalar.- Parameters:
toDivide
- point to dividescalar
- denominator- Returns:
- a new Point whose coordinates are equal to the original point divided by the scalar
- Since:
- 3.1
-
subtract
Performs vector subtraction on two points. Returns a new point equal to (point1 - point2).- Parameters:
point1
- initial pointpoint2
- vector to subtract- Returns:
- the difference (point1 - point2)
- Since:
- 3.0
-
flipXY
Swaps the X and Y coordinates of the given point.- Parameters:
toFlip
- modifies this point- Since:
- 3.1
-
flipXY
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
Returns the height or width of the given rectangle.- Parameters:
toMeasure
- rectangle to measurewidth
- returns the width if true, and the height if false- Returns:
- the width or height of the given rectangle
- Since:
- 3.0
-
getCoordinate
Returns the x or y coordinates of the given point.- Parameters:
toMeasure
- point being measuredwidth
- if true, returns x. Otherwise, returns y.- Returns:
- the x or y coordinate
- Since:
- 3.1
-
getCoordinate
Returns the x or y coordinates of the given rectangle.- Parameters:
toMeasure
- rectangle being measuredwidth
- if true, returns x. Otherwise, returns y.- Returns:
- the x or y coordinate
- Since:
- 3.1
-
setDimension
Sets one dimension of the given rectangle. Modifies the given rectangle.- Parameters:
toSet
- rectangle to modifywidth
- if true, the width is modified. If false, the height is modified.newCoordinate
- new value of the width or height- Since:
- 3.1
-
setCoordinate
Sets one coordinate of the given rectangle. Modifies the given rectangle.- Parameters:
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 coordinates- Since:
- 3.1
-
setCoordinate
Sets one coordinate of the given point. Modifies the given point.- Parameters:
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 coordinates- Since:
- 3.1
-
getDistanceFromEdge
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 rectangletestPoint
- a point to testedgeOfInterest
- 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
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
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
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 modifydifferenceRect
- difference rectangle to be added to rect- Since:
- 3.3
-
createDiffRectangle
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));
- 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
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 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)- Since:
- 3.1
-
normalize
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
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 totoConvert
- rectangle to convert- Returns:
- a rectangle in control coordinates
- Since:
- 3.0
-
toDisplay
Converts the given rectangle from the local coordinate system of the given object into display coordinates.- Parameters:
coordinateSystem
- local coordinate system being converted fromtoConvert
- rectangle to convert- Returns:
- a rectangle in display coordinates
- Since:
- 3.0
-
getRelativePosition
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 rectangletoTest
- 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
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 testtoTest
- 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
Returns the edge of the given rectangle is closest to the given point.- Parameters:
boundary
- rectangle to testtoTest
- point to compare- Returns:
- one of SWT.LEFT, SWT.RIGHT, SWT.TOP, or SWT.BOTTOM
- Since:
- 3.0
-
copy
Returns a copy of the given rectangle- Parameters:
toCopy
- rectangle to copy- Returns:
- a copy of the given rectangle
- Since:
- 3.0
-
getSize
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
Sets the size of the given rectangle to the given size- Parameters:
rectangle
- rectangle to modifynewSize
- new size of the rectangle- Since:
- 3.0
-
setLocation
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 modifynewLocation
- new location of the rectangle- Since:
- 3.0
-
getLocation
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
Returns a new rectangle with the given position and dimensions, expressed as points.- Parameters:
position
- the (x,y) position of the rectanglesize
- 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
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
-