org.openimaj.math.geometry.shape.util

## Class PolygonUtils

• ```public class PolygonUtils
extends Object```
`PolygonUtils` is a Java version of the General Polygon Clipper algorithm developed by Alan Murta (gpc@cs.man.ac.uk). The home page for the original source can be found at http://www.cs.man.ac.uk/aig/staff/alan/software/.

`polyClass:` Some of the public methods below take a `polyClass` argument. This `java.lang.Class` object is assumed to implement the `Polygon` interface and have a no argument constructor. This was done so that the user of the algorithm could create their own classes that implement the `Polygon` interface and still uses this algorithm.

Implementation Note: The converted algorithm does support the difference operation, but a public method has not been provided and it has not been tested. To do so, simply follow what has been done for intersection.

Author:
Dan Bridenbecker, Solution Engineering, Inc.
• ### Field Summary

Fields
Modifier and Type Field and Description
`static int` `ABOVE`
ABOVE
`static int` `BELOW`
BELOW
`static int` `CLIP`
CLIP
`static int` `LEFT`
LEFT
`static int` `RIGHT`
RIGHT
`static int` `SUBJ`
SUBJ
• ### Constructor Summary

Constructors
Constructor and Description
`PolygonUtils()`
Creates a new instance of PolygonUtils
• ### Method Summary

All Methods
Modifier and Type Method and Description
`Polygon` ```intersection(Polygon p1, Polygon p2)```
Return the intersection of `p1` and `p2` where the return type is of `PolyDefault`.
`Polygon` ```intersection(Polygon p1, Polygon p2, Class<Polygon> polyClass)```
Return the intersection of `p1` and `p2` where the return type is of `polyClass`.
`static int` ```NEXT_INDEX(int i, int n)```
`static int` ```PREV_INDEX(int i, int n)```
`Polygon` ```union(Polygon p1, Polygon p2)```
Return the union of `p1` and `p2` where the return type is of `PolyDefault`.
`Polygon` ```union(Polygon p1, Polygon p2, Class<Polygon> polyClass)```
Return the union of `p1` and `p2` where the return type is of `polyClass`.
`Polygon` ```xor(Polygon p1, Polygon p2)```
Return the xor of `p1` and `p2` where the return type is of `PolyDefault`.
`Polygon` ```xor(Polygon p1, Polygon p2, Class<Polygon> polyClass)```
Return the xor of `p1` and `p2` where the return type is of `polyClass`.
• ### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
• ### Field Detail

• #### LEFT

`public static final int LEFT`
LEFT
Constant Field Values
• #### RIGHT

`public static final int RIGHT`
RIGHT
Constant Field Values
• #### ABOVE

`public static final int ABOVE`
ABOVE
Constant Field Values
• #### BELOW

`public static final int BELOW`
BELOW
Constant Field Values
• #### CLIP

`public static final int CLIP`
CLIP
Constant Field Values
• #### SUBJ

`public static final int SUBJ`
SUBJ
Constant Field Values
• ### Constructor Detail

• #### PolygonUtils

`public PolygonUtils()`
Creates a new instance of PolygonUtils
• ### Method Detail

• #### intersection

```public Polygon intersection(Polygon p1,
Polygon p2,
Class<Polygon> polyClass)```
Return the intersection of `p1` and `p2` where the return type is of `polyClass`. See the note in the class description for more on polyClass.
Parameters:
`p1` - One of the polygons to perform the intersection with
`p2` - One of the polygons to perform the intersection with
`polyClass` - The type of `Polygon` to return
Returns:
the resultant polygon
• #### union

```public Polygon union(Polygon p1,
Polygon p2,
Class<Polygon> polyClass)```
Return the union of `p1` and `p2` where the return type is of `polyClass`. See the note in the class description for more on polyClass.
Parameters:
`p1` - One of the polygons to perform the union with
`p2` - One of the polygons to perform the union with
`polyClass` - The type of `Polygon` to return
Returns:
the resultant polygon
• #### xor

```public Polygon xor(Polygon p1,
Polygon p2,
Class<Polygon> polyClass)```
Return the xor of `p1` and `p2` where the return type is of `polyClass`. See the note in the class description for more on `polyClass`.
Parameters:
`p1` - One of the polygons to perform the xor with
`p2` - One of the polygons to perform the xor with
`polyClass` - The type of `Polygon` to return
Returns:
the resultant polygon
• #### intersection

```public Polygon intersection(Polygon p1,
Polygon p2)```
Return the intersection of `p1` and `p2` where the return type is of `PolyDefault`.
Parameters:
`p1` - One of the polygons to perform the intersection with
`p2` - One of the polygons to perform the intersection with
Returns:
the resultant polygon
• #### union

```public Polygon union(Polygon p1,
Polygon p2)```
Return the union of `p1` and `p2` where the return type is of `PolyDefault`.
Parameters:
`p1` - One of the polygons to performt he union with
`p2` - One of the polygons to performt he union with
Returns:
the resultant polygon
• #### xor

```public Polygon xor(Polygon p1,
Polygon p2)```
Return the xor of `p1` and `p2` where the return type is of `PolyDefault`.
Parameters:
`p1` - One of the polygons to perform the xor with
`p2` - One of the polygons to perform the xor with
Returns:
the resultant polygon
• #### PREV_INDEX

```public static int PREV_INDEX(int i,
int n)```
Parameters:
`i` -
`n` -
Returns:
previous index
• #### NEXT_INDEX

```public static int NEXT_INDEX(int i,
int n)```
Parameters:
`i` -
`n` -
Returns:
next index