Package org.eclipse.gef.geometry.planar
Class Polygon
- java.lang.Object
-
- org.eclipse.gef.geometry.planar.Polygon
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable,IGeometry,IRotatable<Polygon>,IScalable<Polygon>,IShape,ITranslatable<Polygon>
public class Polygon extends java.lang.Object implements IShape
Represents the geometric shape of a convex polygon. Note that while all manipulations (e.g. within shrink, expand) within this class are based on double precision, all comparisons (e.g. within contains, intersects, equals, etc.) are based on a limited precision (with an accuracy defined withinPrecisionUtils) to compensate for rounding effects.- See Also:
- Serialized Form
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Objectclone()Overridden with public visibility as recommended withinCloneable.booleancontains(double x, double y)Checks whether the point that is represented by its x- and y-coordinates is contained within thisPolygon.booleancontains(IGeometry g)booleancontains(Line line)booleancontains(Point p)booleancontains(Polygon p)booleancontains(Polyline p)booleancontains(Rectangle r)booleanequals(java.lang.Object o)booleanequals(Point[] points)Checks whether thisPolygonand the one that is indirectly given via the given array of points are regarded to be equal.doublegetArea()Computes the area of thisPolygon.RectanglegetBounds()PointgetCentroid()Computes the centroid of thisAbstractPointListBasedGeometry.double[]getCoordinates()Returns a double array which represents the sequence of coordinates of thePoints that make up thisAbstractPointListBasedGeometry.PolygongetCopy()Returns a copy of thisPolygon, which is made up by the same points.PolylinegetOutline()Line[]getOutlineSegments()Point[]getPoints()Returns a copy of thePoints that make up thisAbstractPointListBasedGeometry.TgetRotatedCCW(Angle alpha)TgetRotatedCCW(Angle angle, double cx, double cy)TgetRotatedCCW(Angle alpha, Point center)TgetRotatedCW(Angle alpha)TgetRotatedCW(Angle angle, double cx, double cy)TgetRotatedCW(Angle alpha, Point center)TgetScaled(double factor)Scales a copy of the calling object by the given factor relative to its centerPoint.TgetScaled(double factorX, double factorY)Scales a copy of the calling object by the given factors relative to its centerPoint.TgetScaled(double factor, double cx, double cy)Scales a copy of the calling object by the given factor relative to the given centerPoint(cx, cy).TgetScaled(double fx, double fy, double cx, double cy)Scales a copy of the calling object by the given factors relative to the given centerPoint(cx, cy).TgetScaled(double factorX, double factorY, Point center)Scales a copy of the calling object by the given factors relative to the given centerPoint.TgetScaled(double factor, Point center)Scales a copy of the calling object by the given factor relative to the given centerPoint.doublegetSignedArea()Computes the signed area of thisPolygon.PolygongetTransformed(AffineTransform t)TgetTranslated(double dx, double dy)Translates a copy of this object by the given values in x and y direction.TgetTranslated(Point pt)Translates a copy of this object by the givenPoint.Polygon[]getTriangulation()Naive, recursive ear-clipping algorithm to triangulate this simple, planarPolygon.inthashCode()TrotateCCW(Angle alpha)Rotates thisAbstractPointListBasedGeometrycounter-clockwise (CCW) by the givenAnglearound its centroid (seegetCentroid()).TrotateCCW(Angle alpha, double cx, double cy)TrotateCCW(Angle alpha, Point center)TrotateCW(Angle alpha)Rotates thisAbstractPointListBasedGeometryclockwise (CW) by the givenAnglearound its centroid (seegetCentroid()).TrotateCW(Angle alpha, double cx, double cy)TrotateCW(Angle alpha, Point center)Tscale(double factor)Scales the calling object by the given factor relative to its centerPoint.Tscale(double fx, double fy)Scales the calling object by the given factors relative to the given centerPoint.Tscale(double factor, double cx, double cy)Scales the calling object by the given factor relative to the given centerPoint(cx, cy).Tscale(double fx, double fy, double cx, double cy)Scales the calling object by the given factors relative to the given centerPoint(cx, cy).Tscale(double fx, double fy, Point center)Scales the calling object by the given factors relative to the given centerPoint.Tscale(double factor, Point center)Scales the calling object by the given factor relative to the given centerPoint.PathtoPath()java.lang.StringtoString()booleantouches(IGeometry g)Ttranslate(double dx, double dy)Translates the object by the given values in x and y direction.Ttranslate(Point p)Translates the object by the givenPoint.
-
-
-
Constructor Detail
-
Polygon
public Polygon(double... coordinates)
Constructs a newPolygonfrom a even-numbered sequence of coordinates.- Parameters:
coordinates- an alternating, even-numbered sequence of x and y coordinates, representing thePoints from which thePolygonis to be created- See Also:
Polygon(Point...)
-
Polygon
public Polygon(Point... points)
Constructs a newPolygonfrom the given sequence ofPoints. ThePolygonthat is created will be automatically closed, i.e. it will not only contain a segment between succeeding points of the sequence but as well back from the last to the first point.- Parameters:
points- a sequence of points, from which thePolygonis to be created.
-
-
Method Detail
-
contains
public boolean contains(double x, double y)Checks whether the point that is represented by its x- and y-coordinates is contained within thisPolygon.- Parameters:
x- the x-coordinate of the point to testy- the y-coordinate of the point to test- Returns:
trueif the point represented by its coordinates if contained within thisPolygon,falseotherwise
-
contains
public boolean contains(Line line)
-
contains
public boolean contains(Polygon p)
-
contains
public boolean contains(Polyline p)
- Parameters:
p- ThePolylineto test for containment.- Returns:
- true if it is contained, false otherwise
-
contains
public boolean contains(Rectangle r)
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
equals
public boolean equals(Point[] points)
Checks whether thisPolygonand the one that is indirectly given via the given array of points are regarded to be equal. ThePolygons will be regarded equal, if they are characterized by the same segments. As aPolygonis always regarded to be closed, the list of points may not have to correspond in each index value, they may also be shifted by a certain offset. Moreover, the vertices of two equallyPolygons may be reverted in order.
-
getArea
public double getArea()
Computes the area of thisPolygon.- Returns:
- the area of this
Polygon
-
getCopy
public Polygon getCopy()
Returns a copy of thisPolygon, which is made up by the same points.
-
getOutline
public Polyline getOutline()
Description copied from interface:IShape- Specified by:
getOutlinein interfaceIShape- Returns:
- An
ICurverepresenting thisIShape's outline.
-
getOutlineSegments
public Line[] getOutlineSegments()
Returns a sequence ofLines, representing the segments that are obtained by linking each two successive point of thisPolygon(including the last and the first one).- Specified by:
getOutlineSegmentsin interfaceIShape- Returns:
- an array of
Lines, representing the segments that make up thisPolygon
-
getSignedArea
public double getSignedArea()
Computes the signed area of thisPolygon. The sign of the area is negative for counter clockwise ordered vertices. It is positive for clockwise ordered vertices.- Returns:
- the signed area of this
Polygon
-
getTransformed
public Polygon getTransformed(AffineTransform t)
Default implementation returning a transformedPathrepresentation of thisIGeometry. Subclasses may override this method to return a more specific representation.- Specified by:
getTransformedin interfaceIGeometry- Specified by:
getTransformedin interfaceIShape- Parameters:
t- TheAffineTransformto be applied- Returns:
- a transformed
Pathrepresentation of thisIGeometry - See Also:
IGeometry.getTransformed(AffineTransform)
-
getTriangulation
public Polygon[] getTriangulation()
Naive, recursive ear-clipping algorithm to triangulate this simple, planarPolygon.- Returns:
- triangulation
Polygons (triangles)
-
toPath
public Path toPath()
Description copied from interface:IGeometry- Specified by:
toPathin interfaceIGeometry- Returns:
- A new
Pathrepresentation for thisIGeometry. - See Also:
IGeometry.toPath()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
getCentroid
public Point getCentroid()
Computes the centroid of thisAbstractPointListBasedGeometry. The centroid is the "center of gravity", i.e. assuming aPolygonis spanned by thePoints of thisAbstractPointListBasedGeometryand it is made of a material of constant density, then it is in a balanced state, if you put it on a pin that is placed exactly on its centroid.- Returns:
- the center
Point(or centroid) of thisAbstractPointListBasedGeometry
-
getCoordinates
public final double[] getCoordinates()
Returns a double array which represents the sequence of coordinates of thePoints that make up thisAbstractPointListBasedGeometry.- Returns:
- an array that alternately contains the x and y coordinates of
this
AbstractPointListBasedGeometry's points
-
getPoints
public final Point[] getPoints()
Returns a copy of thePoints that make up thisAbstractPointListBasedGeometry.
-
getRotatedCCW
public T getRotatedCCW(Angle alpha)
Description copied from interface:IRotatableRotates the calling object by specifiedAnglecounter-clock-wise (CCW) around its centerPoint. Does not necessarily return an object of the same type.- Specified by:
getRotatedCCWin interfaceIRotatable<T extends org.eclipse.gef.geometry.planar.AbstractPointListBasedGeometry<?>>- Parameters:
alpha- rotationAngle- Returns:
- an
IGeometryrepresenting the result of the rotation
-
getRotatedCCW
public T getRotatedCCW(Angle angle, double cx, double cy)
Description copied from interface:IRotatableRotates the calling object by the specifiedAnglecounter-clock-wise (CCW) around the specified centerPoint(cx, cy). Does not necessarily return an object of the same type.- Specified by:
getRotatedCCWin interfaceIRotatable<T extends org.eclipse.gef.geometry.planar.AbstractPointListBasedGeometry<?>>- Parameters:
angle- rotationAnglecx- x-coordinate of the relativePointfor the rotationcy- y-coordinate of the relativePointfor the rotation- Returns:
- an
IGeometryrepresenting the result of the rotation
-
getRotatedCCW
public T getRotatedCCW(Angle alpha, Point center)
Description copied from interface:IRotatableRotates the calling object by the specifiedAnglecounter-clock-wise (CCW) around the specified centerPoint. Does not necessarily return an object of the same type.- Specified by:
getRotatedCCWin interfaceIRotatable<T extends org.eclipse.gef.geometry.planar.AbstractPointListBasedGeometry<?>>- Parameters:
alpha- rotationAnglecenter- relativePointfor the rotation- Returns:
- an
IGeometryrepresenting the result of the rotation
-
getRotatedCW
public T getRotatedCW(Angle alpha)
Description copied from interface:IRotatableRotates the calling object by specifiedAngleclock-wise (CW) around its centerPoint. Does not necessarily return an object of the same type.- Specified by:
getRotatedCWin interfaceIRotatable<T extends org.eclipse.gef.geometry.planar.AbstractPointListBasedGeometry<?>>- Parameters:
alpha- rotationAngle- Returns:
- an
IGeometryrepresenting the result of the rotation
-
getRotatedCW
public T getRotatedCW(Angle angle, double cx, double cy)
Description copied from interface:IRotatableRotates the calling object by the specifiedAngleclock-wise (CW) around the specified centerPoint(cx, cy). Does not necessarily return an object of the same type.- Specified by:
getRotatedCWin interfaceIRotatable<T extends org.eclipse.gef.geometry.planar.AbstractPointListBasedGeometry<?>>- Parameters:
angle- rotationAnglecx- x-coordinate of the relativePointfor the rotationcy- y-coordinate of the relativePointfor the rotation- Returns:
- an
IGeometryrepresenting the result of the rotation
-
getRotatedCW
public T getRotatedCW(Angle alpha, Point center)
Description copied from interface:IRotatableRotates the calling object by the specifiedAngleclock-wise (CW) around the specified centerPoint. Does not necessarily return an object of the same type.- Specified by:
getRotatedCWin interfaceIRotatable<T extends org.eclipse.gef.geometry.planar.AbstractPointListBasedGeometry<?>>- Parameters:
alpha- rotationAnglecenter- relativePointfor the rotation- Returns:
- an
IGeometryrepresenting the result of the rotation
-
getScaled
public T getScaled(double factor)
Description copied from interface:IScalableScales a copy of the calling object by the given factor relative to its centerPoint.
-
getScaled
public T getScaled(double factorX, double factorY)Description copied from interface:IScalableScales a copy of the calling object by the given factors relative to its centerPoint.
-
getScaled
public T getScaled(double factor, double cx, double cy)Description copied from interface:IScalableScales a copy of the calling object by the given factor relative to the given centerPoint(cx, cy).- Specified by:
getScaledin interfaceIScalable<T extends org.eclipse.gef.geometry.planar.AbstractPointListBasedGeometry<?>>- Parameters:
factor- scale-factorcx- x-coordinate of the relativePointfor the scalingcy- y-coordinate of the relativePointfor the scaling- Returns:
- the new, scaled object
-
getScaled
public T getScaled(double fx, double fy, double cx, double cy)Description copied from interface:IScalableScales a copy of the calling object by the given factors relative to the given centerPoint(cx, cy).- Specified by:
getScaledin interfaceIScalable<T extends org.eclipse.gef.geometry.planar.AbstractPointListBasedGeometry<?>>- Parameters:
fx- x-scale-factorfy- y-scale-factorcx- x-coordinate of the relativePointfor the scalingcy- y-coordinate of the relativePointfor the scaling- Returns:
- the new, scaled object
-
getScaled
public T getScaled(double factorX, double factorY, Point center)Description copied from interface:IScalableScales a copy of the calling object by the given factors relative to the given centerPoint.
-
getScaled
public T getScaled(double factor, Point center)Description copied from interface:IScalableScales a copy of the calling object by the given factor relative to the given centerPoint.
-
getTranslated
public T getTranslated(double dx, double dy)Description copied from interface:ITranslatableTranslates a copy of this object by the given values in x and y direction.- Specified by:
getTranslatedin interfaceITranslatable<T extends org.eclipse.gef.geometry.planar.AbstractPointListBasedGeometry<?>>- Parameters:
dx- x-translationdy- y-translation- Returns:
- a new, translated object
-
getTranslated
public T getTranslated(Point pt)
Description copied from interface:ITranslatableTranslates a copy of this object by the givenPoint.- Specified by:
getTranslatedin interfaceITranslatable<T extends org.eclipse.gef.geometry.planar.AbstractPointListBasedGeometry<?>>- Parameters:
pt- translationPoint- Returns:
- a new, translated object
-
rotateCCW
public T rotateCCW(Angle alpha)
Rotates thisAbstractPointListBasedGeometrycounter-clockwise (CCW) by the givenAnglearound its centroid (seegetCentroid()).- Parameters:
alpha- the rotationAngle- Returns:
thisfor convenience- See Also:
rotateCCW(Angle, Point)
-
rotateCCW
public T rotateCCW(Angle alpha, double cx, double cy)
Rotates thisAbstractPointListBasedGeometrycounter-clockwise (CCW) by the givenAnglearound thePointspecified by the passed-in x and y coordinates.- Parameters:
alpha- the rotationAnglecx- the x coordinate of thePointto rotate aroundcy- the y coordinate of thePointto rotate around- Returns:
thisfor convenience- See Also:
rotateCCW(Angle, Point)
-
rotateCW
public T rotateCW(Angle alpha)
Rotates thisAbstractPointListBasedGeometryclockwise (CW) by the givenAnglearound its centroid (seegetCentroid()).- Parameters:
alpha- the rotationAngle- Returns:
thisfor convenience- See Also:
rotateCW(Angle, Point)
-
rotateCW
public T rotateCW(Angle alpha, double cx, double cy)
Rotates thisAbstractPointListBasedGeometryclockwise (CW) by the givenAnglearound thePointspecified by the passed-in x and y coordinates.- Parameters:
alpha- the rotationAnglecx- the x coordinate of thePointto rotate aroundcy- the y coordinate of thePointto rotate around- Returns:
thisfor convenience- See Also:
rotateCW(Angle, Point)
-
scale
public T scale(double factor)
Description copied from interface:IScalableScales the calling object by the given factor relative to its centerPoint.
-
scale
public T scale(double fx, double fy)Description copied from interface:IScalableScales the calling object by the given factors relative to the given centerPoint.
-
scale
public T scale(double factor, double cx, double cy)Description copied from interface:IScalableScales the calling object by the given factor relative to the given centerPoint(cx, cy).
-
scale
public T scale(double fx, double fy, double cx, double cy)Description copied from interface:IScalableScales the calling object by the given factors relative to the given centerPoint(cx, cy).- Specified by:
scalein interfaceIScalable<T extends org.eclipse.gef.geometry.planar.AbstractPointListBasedGeometry<?>>- Parameters:
fx- x-scale-factorfy- y-scale-factorcx- x-coordinate of the relativePointfor the scalingcy- y-coordinate of the relativePointfor the scaling- Returns:
thisfor convenience
-
scale
public T scale(double fx, double fy, Point center)Description copied from interface:IScalableScales the calling object by the given factors relative to the given centerPoint.
-
scale
public T scale(double factor, Point center)Description copied from interface:IScalableScales the calling object by the given factor relative to the given centerPoint.
-
translate
public T translate(double dx, double dy)Description copied from interface:ITranslatableTranslates the object by the given values in x and y direction.- Specified by:
translatein interfaceITranslatable<T extends org.eclipse.gef.geometry.planar.AbstractPointListBasedGeometry<?>>- Parameters:
dx- x-translationdy- y-translation- Returns:
thisfor convenience
-
translate
public T translate(Point p)
Description copied from interface:ITranslatableTranslates the object by the givenPoint.- Specified by:
translatein interfaceITranslatable<T extends org.eclipse.gef.geometry.planar.AbstractPointListBasedGeometry<?>>- Parameters:
p- translationPoint- Returns:
thisfor convenience
-
clone
public java.lang.Object clone()
Overridden with public visibility as recommended withinCloneable.- Overrides:
clonein classjava.lang.Object
-
hashCode
public final int hashCode()
- Overrides:
hashCodein classjava.lang.Object- See Also:
Object.hashCode()
-
-