math.geom2d
Class Point2D
- java.lang.Object
-
- math.geom2d.Point2D
-
- All Implemented Interfaces:
- java.lang.Cloneable, java.lang.Iterable<Point2D>, CirculinearShape2D, GeometricObject2D, PointShape2D, Shape2D
public class Point2D extends java.lang.Object implements GeometricObject2D, PointShape2D, java.lang.Cloneable, CirculinearShape2D
A point in the plane defined by its 2 Cartesian coordinates x and y. The class provides static methods to compute distance between two points.
-
-
Constructor Summary
Constructors Constructor and Description Point2D()Constructs a new Point2D at position (0,0).Point2D(double x, double y)Constructs a new Point2D at the given given position.Point2D(java.awt.geom.Point2D point)Constructs a new Point2D by copying coordinates of given java point.Point2D(Point2D point)Deprecated.immutable objects do not need copy constructor (0.11.2)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method and Description booleanalmostEquals(GeometricObject2D obj, double eps)Test whether this object is the same as another point, with respect to a given threshold along each coordinate.Box2DboundingBox()Returns a bounding box with zero width and zero height, whose coordinates limits are point coordinates.CirculinearDomain2Dbuffer(double dist)Computes the buffer of the shape, formed by the set of points located at a distance from the shape that is lower or equal to d.static intccw(Point2D p0, Point2D p1, Point2D p2)Computes the orientation of the 3 points: returns +1 is the path P0->P1->P2 turns Counter-Clockwise, -1 if the path turns Clockwise, and 0 if the point P2 is located on the line segment [P0 P1].static Point2Dcentroid(java.util.Collection<? extends Point2D> points)Computes the centroid, or center of mass, of a collection of points.static Point2Dcentroid(Point2D[] points)Computes the centroid, or center of mass, of an array of points.static Point2Dcentroid(Point2D[] points, double[] weights)Computes the weighted centroid, or center of mass, of an array of points.static Point2Dcentroid(Point2D pt1, Point2D pt2, Point2D pt3)Computes the centroid of three points.PointSet2Dclip(Box2D box)Returns a PointSet2D, containing 0 or 1 point, depending on whether the point lies inside the specified box.Point2Dclone()Deprecated.not necessary to clone immutable objects (0.11.2)booleancontains(double x, double y)Returns true if the two points are equal.booleancontains(Point2D p)Returns true if the two points are equal.static Point2Dcreate(double x, double y)Deprecated.since 0.11.1static Point2Dcreate(java.awt.geom.Point2D point)Deprecated.since 0.11.1static Point2Dcreate(Point2D point)Static factory for creating a new point from an existing instance of javageom point.static Point2DcreatePolar(double rho, double theta)Creates a new point from polar coordinatesrhoandtheta.static Point2DcreatePolar(double x0, double y0, double rho, double theta)Creates a new point from polar coordinatesrhoandtheta, from the position (x0,y0).static Point2DcreatePolar(Point2D point, double rho, double theta)Creates a new point from polar coordinatesrhoandtheta, from the given point.doubledistance(double x, double y)Computes the distance between current point and point with coordinate(x,y).static doubledistance(double x1, double y1, double x2, double y2)Computes the Euclidean distance between two points, given by their coordinates.doubledistance(Point2D point)Computes the distance between this and the pointpoint.static doubledistance(Point2D p1, Point2D p2)Computes the Euclidean distance between two points.voiddraw(java.awt.Graphics2D g2)Draws the point on the specified Graphics2D, using default radius equal to 1.voiddraw(java.awt.Graphics2D g2, double r)Draws the point on the specified Graphics2D, by filling a disc with a given radius.booleanequals(java.lang.Object obj)Two points are considered equal if their Euclidean distance is less than Shape2D.ACCURACY.java.awt.geom.Point2D.DoublegetAsDouble()Converts point to a double version.java.awt.geom.Point2D.FloatgetAsFloat()Converts point to a float version.java.awt.PointgetAsInt()Converts point to an integer version.doublegetX()doublegetY()Returns the y-coordinate of this point.booleanisBounded()Returns true if the point is bounded.static booleanisColinear(Point2D p1, Point2D p2, Point2D p3)Tests if the three points are colinear.booleanisEmpty()Returns false, as a point can not be empty.java.util.Iterator<Point2D>iterator()static Point2DmidPoint(Point2D p1, Point2D p2)Point2Dminus(Point2D p)Removes the coordinates of the given point from the coordinates of this point.Point2Dminus(Vector2D v)Removes the coordinates of the given vector from the coordinates of this point.Point2Dplus(Point2D p)Adds the coordinates of the given point to the coordinates of this point.Point2Dplus(Vector2D v)Adds the coordinates of the given vector to the coordinates of this point.java.util.Collection<Point2D>points()Returns the points in the shape as a collection.Point2Drotate(double theta)Rotates the point by a given angle around the origin.Point2Drotate(Point2D center, double theta)Rotates the point by a given angle around an arbitrary center.Point2Dscale(double k)Returns the new point scaled by the same amount in each direction.Point2Dscale(double kx, double ky)Returns the new point scaled by amount given in each direction.intsize()Returns the number of points in the set.java.lang.StringtoString()Point2Dtransform(AffineTransform2D trans)Returns the transformed point.Point2Dtransform(CircleInversion2D inv)Transforms the shape by a circle inversion.Point2Dtranslate(double tx, double ty)Returns the new point translated by amount given in each direction.doublex()Returns the x-coordinate of this point.doubley()Returns the y-coordinate of this point.
-
-
-
Constructor Detail
-
Point2D
public Point2D()
Constructs a new Point2D at position (0,0).
-
Point2D
public Point2D(double x, double y)Constructs a new Point2D at the given given position.
-
Point2D
public Point2D(java.awt.geom.Point2D point)
Constructs a new Point2D by copying coordinates of given java point.
-
Point2D
@Deprecated public Point2D(Point2D point)
Deprecated. immutable objects do not need copy constructor (0.11.2)Constructs a new Point2D by copying coordinates of given point.
-
-
Method Detail
-
create
@Deprecated public static Point2D create(double x, double y)
Deprecated. since 0.11.1Static factory for creating a new point in cartesian coordinates.
-
create
@Deprecated public static Point2D create(java.awt.geom.Point2D point)
Deprecated. since 0.11.1Static factory for creating a new point from an existing instance of java point.
-
create
public static Point2D create(Point2D point)
Static factory for creating a new point from an existing instance of javageom point.- Since:
- 0.10.0
-
createPolar
public static Point2D createPolar(double rho, double theta)
Creates a new point from polar coordinatesrhoandtheta.
-
createPolar
public static Point2D createPolar(Point2D point, double rho, double theta)
Creates a new point from polar coordinatesrhoandtheta, from the given point.
-
createPolar
public static Point2D createPolar(double x0, double y0, double rho, double theta)
Creates a new point from polar coordinatesrhoandtheta, from the position (x0,y0).
-
distance
public static double distance(double x1, double y1, double x2, double y2)Computes the Euclidean distance between two points, given by their coordinates. Uses robust computation (via Math.hypot() method).- Returns:
- the Euclidean distance between p1 and p2.
-
distance
public static double distance(Point2D p1, Point2D p2)
Computes the Euclidean distance between two points. Uses robust computation (via Math.hypot() method).- Parameters:
p1- the first pointp2- the second point- Returns:
- the Euclidean distance between p1 and p2.
-
isColinear
public static boolean isColinear(Point2D p1, Point2D p2, Point2D p3)
Tests if the three points are colinear.- Returns:
- true if three points lie on the same line.
-
ccw
public static int ccw(Point2D p0, Point2D p1, Point2D p2)
Computes the orientation of the 3 points: returns +1 is the path P0->P1->P2 turns Counter-Clockwise, -1 if the path turns Clockwise, and 0 if the point P2 is located on the line segment [P0 P1]. Algorithm taken from Sedgewick.- Parameters:
p0- the initial pointp1- the middle pointp2- the last point- Returns:
- +1, 0 or -1, depending on the relative position of the points
-
centroid
public static Point2D centroid(Point2D[] points)
Computes the centroid, or center of mass, of an array of points.- Parameters:
points- an array of points- Returns:
- the centroid of the points
-
centroid
public static Point2D centroid(Point2D[] points, double[] weights)
Computes the weighted centroid, or center of mass, of an array of points.- Parameters:
points- an array of pointsweights- an array of weights the same size as points- Returns:
- the centroid of the points
-
centroid
public static Point2D centroid(java.util.Collection<? extends Point2D> points)
Computes the centroid, or center of mass, of a collection of points.- Parameters:
points- a collection of points- Returns:
- the centroid of the points
-
centroid
public static Point2D centroid(Point2D pt1, Point2D pt2, Point2D pt3)
Computes the centroid of three points.- Parameters:
pt1- the first pointpt2- the second pointpt3- the third point- Returns:
- the centroid of the 3 points
-
plus
public Point2D plus(Point2D p)
Adds the coordinates of the given point to the coordinates of this point.
-
plus
public Point2D plus(Vector2D v)
Adds the coordinates of the given vector to the coordinates of this point.
-
minus
public Point2D minus(Point2D p)
Removes the coordinates of the given point from the coordinates of this point.
-
minus
public Point2D minus(Vector2D v)
Removes the coordinates of the given vector from the coordinates of this point.
-
translate
public Point2D translate(double tx, double ty)
Returns the new point translated by amount given in each direction.- Parameters:
tx- the translation in x directionty- the translation in y direction- Returns:
- the translated point
-
scale
public Point2D scale(double kx, double ky)
Returns the new point scaled by amount given in each direction.- Parameters:
kx- the scale factor in x directionky- the scale factor in y direction- Returns:
- the scaled point
-
scale
public Point2D scale(double k)
Returns the new point scaled by the same amount in each direction.- Parameters:
k- the scale factor- Returns:
- the scaled point
-
rotate
public Point2D rotate(double theta)
Rotates the point by a given angle around the origin.- Parameters:
theta- the angle of rotation, in radians- Returns:
- the rotated point.
-
rotate
public Point2D rotate(Point2D center, double theta)
Rotates the point by a given angle around an arbitrary center.- Parameters:
center- the center of the rotationtheta- the angle of rotation, in radians- Returns:
- the rotated point.
-
getAsInt
public java.awt.Point getAsInt()
Converts point to an integer version. Coordinates are rounded to the nearest integer.- Returns:
- an instance of java.awt.Point
-
getAsDouble
public java.awt.geom.Point2D.Double getAsDouble()
Converts point to a double version.
-
getAsFloat
public java.awt.geom.Point2D.Float getAsFloat()
Converts point to a float version. Coordinates are rounded to the nearest float.
-
x
public double x()
Returns the x-coordinate of this point.
-
getX
public double getX()
-
y
public double y()
Returns the y-coordinate of this point.
-
getY
public double getY()
Returns the y-coordinate of this point.
-
buffer
public CirculinearDomain2D buffer(double dist)
Description copied from interface:CirculinearShape2DComputes the buffer of the shape, formed by the set of points located at a distance from the shape that is lower or equal to d.- Specified by:
bufferin interfaceCirculinearShape2D- Parameters:
dist- the maximal distance between a point of the buffer and the shape- Returns:
- the buffer of the shape
-
transform
public Point2D transform(CircleInversion2D inv)
Description copied from interface:CirculinearShape2DTransforms the shape by a circle inversion. The result is still an instance a CirculinearShape2D.- Specified by:
transformin interfaceCirculinearShape2D- Parameters:
inv- the circle inversion- Returns:
- the transformed shape
-
size
public int size()
Description copied from interface:PointShape2DReturns the number of points in the set.- Specified by:
sizein interfacePointShape2D- Returns:
- the number of points
-
points
public java.util.Collection<Point2D> points()
Description copied from interface:PointShape2DReturns the points in the shape as a collection.- Specified by:
pointsin interfacePointShape2D- Returns:
- the collection of points
-
distance
public double distance(Point2D point)
Computes the distance between this and the pointpoint.
-
distance
public double distance(double x, double y)Computes the distance between current point and point with coordinate(x,y). Uses theMath.hypot()function for better robustness than simple square root.
-
isBounded
public boolean isBounded()
Returns true if the point is bounded. A point is unbounded if at least one of its coordinates is infinite or NaN.
-
isEmpty
public boolean isEmpty()
Returns false, as a point can not be empty.
-
contains
public boolean contains(double x, double y)Returns true if the two points are equal.
-
contains
public boolean contains(Point2D p)
Returns true if the two points are equal.
-
clip
public PointSet2D clip(Box2D box)
Returns a PointSet2D, containing 0 or 1 point, depending on whether the point lies inside the specified box.- Specified by:
clipin interfacePointShape2D- Specified by:
clipin interfaceShape2D- Parameters:
box- the clipping box- Returns:
- the clipped shape
-
boundingBox
public Box2D boundingBox()
Returns a bounding box with zero width and zero height, whose coordinates limits are point coordinates.- Specified by:
boundingBoxin interfaceShape2D- Returns:
- the bounding box of the shape.
-
transform
public Point2D transform(AffineTransform2D trans)
Returns the transformed point.- Specified by:
transformin interfacePointShape2D- Specified by:
transformin interfaceShape2D- Parameters:
trans- an affine transform- Returns:
- the transformed shape
-
draw
public void draw(java.awt.Graphics2D g2)
Draws the point on the specified Graphics2D, using default radius equal to 1.
-
draw
public void draw(java.awt.Graphics2D g2, double r)Draws the point on the specified Graphics2D, by filling a disc with a given radius.- Parameters:
g2- the graphics to draw the point
-
almostEquals
public boolean almostEquals(GeometricObject2D obj, double eps)
Test whether this object is the same as another point, with respect to a given threshold along each coordinate.- Specified by:
almostEqualsin interfaceGeometricObject2D- Parameters:
obj- the object to compareeps- a threshold value, for example the minimal coordinate difference- Returns:
- true if both object have the same value up to the threshold
-
iterator
public java.util.Iterator<Point2D> iterator()
- Specified by:
iteratorin interfacejava.lang.Iterable<Point2D>
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
Two points are considered equal if their Euclidean distance is less than Shape2D.ACCURACY.- Overrides:
equalsin classjava.lang.Object
-
clone
@Deprecated public Point2D clone()
Deprecated. not necessary to clone immutable objects (0.11.2)- Overrides:
clonein classjava.lang.Object
-
-
DMelt 3.0 © DataMelt by jWork.ORG