📄 geometry.java
字号:
/******************************************************************************* * Copyright (c) 2004, 2006 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/package org.eclipse.jface.util;import org.eclipse.swt.SWT;import org.eclipse.swt.graphics.Point;import org.eclipse.swt.graphics.Rectangle;import org.eclipse.swt.widgets.Control;/** * Contains static methods for performing simple geometric operations * on the SWT geometry classes. * * @since 3.0 */public class Geometry { /** * Prevent this class from being instantiated. * * @since 3.0 */ private Geometry() { //This is not instantiated } /** * Returns the square of the distance between two points. * <p>This is preferred over the real distance when searching * for the closest point, since it avoids square roots.</p> * * @param p1 first endpoint * @param p2 second endpoint * @return the square of the distance between the two points * * @since 3.0 */ public static int distanceSquared(Point p1, Point p2) { int term1 = p1.x - p2.x; int term2 = p1.y - p2.y; return term1 * term1 + term2 * term2; } /** * Returns the magnitude of the given 2d vector (represented as a Point) * * @param p point representing the 2d vector whose magnitude is being computed * @return the magnitude of the given 2d vector * @since 3.0 */ public static double magnitude(Point p) { return Math.sqrt(magnitudeSquared(p)); } /** * Returns the square of the magnitude of the given 2-space vector (represented * using a point) * * @param p the point whose magnitude is being computed * @return the square of the magnitude of the given vector * @since 3.0 */ public static int magnitudeSquared(Point p) { return p.x * p.x + p.y * p.y; } /** * Returns the dot product of the given vectors (expressed as Points) * * @param p1 the first vector * @param p2 the second vector * @return the dot product of the two vectors * @since 3.0 */ public static int dotProduct(Point p1, Point p2) { return p1.x * p2.x + p1.y * p2.y; } /** * Returns a new point whose coordinates are the minimum of the coordinates of the * given points * * @param p1 a Point * @param p2 a Point * @return a new point whose coordinates are the minimum of the coordinates of the * given points * @since 3.0 */ public static Point min(Point p1, Point p2) { return new Point(Math.min(p1.x, p2.x), Math.min(p1.y, p2.y)); } /** * Returns a new point whose coordinates are the maximum of the coordinates * of the given points * @param p1 a Point * @param p2 a Point * @return point a new point whose coordinates are the maximum of the coordinates * @since 3.0 */ public static Point max(Point p1, Point p2) { return new Point(Math.max(p1.x, p2.x), Math.max(p1.y, p2.y)); } /** * 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. * * @param distance magnitude of the vector * @param direction one of SWT.TOP, SWT.BOTTOM, SWT.LEFT, or SWT.RIGHT * @return a point representing a vector in the given direction with the given magnitude * @since 3.0 */ public static Point getDirectionVector(int distance, int direction) { switch (direction) { case SWT.TOP: return new Point(0, -distance); case SWT.BOTTOM: return new Point(0, distance); case SWT.LEFT: return new Point(-distance, 0); case SWT.RIGHT: return new Point(distance, 0); } return new Point(0, 0); } /** * Returns the point in the center of the given rectangle. * * @param rect rectangle being computed * @return a Point at the center of the given rectangle. * @since 3.0 */ public static Point centerPoint(Rectangle rect) { return new Point(rect.x + rect.width / 2, rect.y + rect.height / 2); } /** * Returns a copy of the given point * * @param toCopy point to copy * @return a copy of the given point */ public static Point copy(Point toCopy) { return new Point(toCopy.x, toCopy.y); } /** * Sets result equal to toCopy * * @param result object that will be modified * @param toCopy object that will be copied * @since 3.1 */ public static void set(Point result, Point toCopy) { result.x = toCopy.x; result.y = toCopy.y; } /** * Sets result equal to toCopy * * @param result object that will be modified * @param toCopy object that will be copied * @since 3.1 */ public static void set(Rectangle result, Rectangle toCopy) { result.x = toCopy.x; result.y = toCopy.y; result.width = toCopy.width; result.height = toCopy.height; } /** * Adds two points as 2d vectors. Returns a new point whose coordinates are * the sum of the original two points. * * @param point1 the first point (not null) * @param point2 the second point (not null) * @return a new point whose coordinates are the sum of the given points * @since 3.0 */ public static Point add(Point point1, Point point2) { return new Point(point1.x + point2.x, point1.y + point2.y); } /** * Divides both coordinates of the given point by the given scalar. * * @since 3.1 * * @param toDivide point to divide * @param scalar denominator * @return a new Point whose coordinates are equal to the original point divided by the scalar */ public static Point divide(Point toDivide, int scalar) { return new Point(toDivide.x / scalar, toDivide.y / scalar); } /** * Performs vector subtraction on two points. Returns a new point equal to * (point1 - point2). * * @param point1 initial point * @param point2 vector to subtract * @return the difference (point1 - point2) * @since 3.0 */ public static Point subtract(Point point1, Point point2) { return new Point(point1.x - point2.x, point1.y - point2.y); } /** * Swaps the X and Y coordinates of the given point. * * @param toFlip modifies this point * @since 3.1 */ public static void flipXY(Point toFlip) { int temp = toFlip.x; toFlip.x = toFlip.y; toFlip.y = temp; } /** * Swaps the X and Y coordinates of the given rectangle, along with the height and width. * * @param toFlip modifies this rectangle * @since 3.1 */ public static void flipXY(Rectangle toFlip) { int temp = toFlip.x; toFlip.x = toFlip.y; toFlip.y = temp; temp = toFlip.width; toFlip.width = toFlip.height; toFlip.height = temp; } /** * Returns the height or width of the given rectangle. * * @param toMeasure rectangle to measure * @param width returns the width if true, and the height if false * @return the width or height of the given rectangle * @since 3.0 */ public static int getDimension(Rectangle toMeasure, boolean width) { if (width) { return toMeasure.width; } return toMeasure.height; } /** * Returns the x or y coordinates of the given point. * * @param toMeasure point being measured * @param width if true, returns x. Otherwise, returns y. * @return the x or y coordinate * @since 3.1 */ public static int getCoordinate(Point toMeasure, boolean width) { return width ? toMeasure.x : toMeasure.y; } /** * Returns the x or y coordinates of the given rectangle. * * @param toMeasure rectangle being measured * @param width if true, returns x. Otherwise, returns y. * @return the x or y coordinate * @since 3.1 */ public static int getCoordinate(Rectangle toMeasure, boolean width) { return width ? toMeasure.x : toMeasure.y; } /** * Sets one dimension of the given rectangle. Modifies the given rectangle. * * @param toSet rectangle to modify * @param width if true, the width is modified. If false, the height is modified. * @param newCoordinate new value of the width or height * @since 3.1 */ public static void setDimension(Rectangle toSet, boolean width, int newCoordinate) { if (width) { toSet.width = newCoordinate; } else { toSet.height = newCoordinate; } } /** * Sets one coordinate of the given rectangle. Modifies the given rectangle. * * @param toSet rectangle to modify * @param width if true, the x coordinate is modified. If false, the y coordinate is modified. * @param newCoordinate new value of the x or y coordinates * @since 3.1 */ public static void setCoordinate(Rectangle toSet, boolean width, int newCoordinate) { if (width) { toSet.x = newCoordinate; } else { toSet.y = newCoordinate; } } /** * Sets one coordinate of the given point. Modifies the given point. * * @param toSet point to modify * @param width if true, the x coordinate is modified. If false, the y coordinate is modified. * @param newCoordinate new value of the x or y coordinates * @since 3.1 */ public static void setCoordinate(Point toSet, boolean width, int newCoordinate) { if (width) { toSet.x = newCoordinate; } else { toSet.y = newCoordinate; } } /** * Returns the distance of the given point from a particular side of the given rectangle. * Returns negative values for points outside the rectangle. * * @param rectangle a bounding rectangle * @param testPoint a point to test * @param edgeOfInterest side of the rectangle to test against * @return the distance of the given point from the given edge of the rectangle * @since 3.0 */ public static int getDistanceFromEdge(Rectangle rectangle, Point testPoint, int edgeOfInterest) { switch (edgeOfInterest) { case SWT.TOP: return testPoint.y - rectangle.y; case SWT.BOTTOM: return rectangle.y + rectangle.height - testPoint.y; case SWT.LEFT: return testPoint.x - rectangle.x; case SWT.RIGHT: return rectangle.x + rectangle.width - testPoint.x;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -