org.openimaj.math.geometry.shape

## Class Triangle

• All Implemented Interfaces:
Cloneable, GeometricObject2d, Shape

```public class Triangle
extends Object
implements Shape```
A triangle shape
Author:
Jonathon Hare (jsh2@ecs.soton.ac.uk)
• ### Field Summary

Fields
Modifier and Type Field and Description
`Point2d[]` `vertices`
The vertices of the triangle
• ### Constructor Summary

Constructors
Constructor and Description
`Triangle(Point2d[] vertices)`
Construct a Triangle with the given vertices.
```Triangle(Point2d vertex1, Point2d vertex2, Point2d vertex3)```
Construct a Triangle with the given vertices.
• ### Method Summary

All Methods
Modifier and Type Method and Description
`Polygon` `asPolygon()`
Convert the shape to a polygon representation
`double` `calculateArea()`
Calculate the area of the shape
`Point2d` `calculateCentroid()`
Calculate the centroid of the shape
`double` `calculatePerimeter()`
Calculate the perimeter of the shape
`Rectangle` `calculateRegularBoundingBox()`
Compute the regular (oriented to the axes) bounding box of the shape.
`Triangle` `clone()`
`boolean` `equals(Object obj)`
`Point2d` `firstVertex()`
`List<Line2d>` `getEdges()`
`double` `getHeight()`
`double` `getWidth()`
`int` `hashCode()`
`double` `intersectionArea(Shape that)`
Calls `Polygon.intersectionArea(Shape, int)` with 1 step per pixel dimension.
`double` ```intersectionArea(Shape that, int nStepsPerDimention)```
Return an estimate for the area of the intersection of this polygon and another polygon.
`Map<Line2d,Point2d>` `intersectionSides(Line2d line)`
The intersection of this triangle with the line defined by y = mx + c.
`boolean` `isConvex()`
Test if the shape is convex.
`boolean` `isInside(Point2d point)`
Test whether the point p is inside the shape.
`boolean` `isInsideOnLine(Point2d point)`
Like `isInside(Point2d)` but counts being "on the line" as being inside also
`double` `maxX()`
`double` `maxY()`
`RotatedRectangle` `minimumBoundingRectangle()`
Compute the minimum size rotated bounding rectangle that contains this shape.
`double` `minX()`
`double` `minY()`
`void` `scale(float sc)`
Scale the shape by the given amount about (0,0).
`void` ```scale(Point2d centre, float sc)```
Scale the shape by the given amount about the given point.
`void` `scaleCentroid(float sc)`
Scale the shape about its centroid.
`Point2d` `secondVertex()`
`boolean` `sharesVertex(Triangle other)`
Test whether this triangle shares a vertex with another triangle.
`Point2d` `thirdVertex()`
`String` `toString()`
`Triangle` `transform(Jama.Matrix transform)`
Apply a 3x3 transform matrix to a copy of the `GeometricObject2d` and return it
`void` ```translate(float x, float y)```
Translate the shapes position
• ### Methods inherited from class java.lang.Object

`finalize, getClass, notify, notifyAll, wait, wait, wait`
• ### Field Detail

• #### vertices

`public Point2d[] vertices`
The vertices of the triangle
• ### Constructor Detail

• #### Triangle

```public Triangle(Point2d vertex1,
Point2d vertex2,
Point2d vertex3)```
Construct a Triangle with the given vertices.
Parameters:
`vertex1` - first vertex
`vertex2` - second vertex
`vertex3` - third vertex
• #### Triangle

`public Triangle(Point2d[] vertices)`
Construct a Triangle with the given vertices.
Parameters:
`vertices` - the vertices
• ### Method Detail

• #### isInsideOnLine

`public final boolean isInsideOnLine(Point2d point)`
Like `isInside(Point2d)` but counts being "on the line" as being inside also
Parameters:
`point` - the point being tested
Returns:
true if the point is inside or on the line; false otherwise
• #### isInside

`public final boolean isInside(Point2d point)`
Description copied from interface: `Shape`
Test whether the point p is inside the shape.
Specified by:
`isInside` in interface `Shape`
Parameters:
`point` - the point
Returns:
true if the point is inside; false otherwise
• #### calculateRegularBoundingBox

`public Rectangle calculateRegularBoundingBox()`
Description copied from interface: `GeometricObject2d`
Compute the regular (oriented to the axes) bounding box of the shape.
Specified by:
`calculateRegularBoundingBox` in interface `GeometricObject2d`
Returns:
the regular bounding box as [x,y,width,height]
• #### translate

```public void translate(float x,
float y)```
Description copied from interface: `GeometricObject2d`
Translate the shapes position
Specified by:
`translate` in interface `GeometricObject2d`
Parameters:
`x` - x-translation
`y` - y-translation
• #### scale

`public void scale(float sc)`
Description copied from interface: `GeometricObject2d`
Scale the shape by the given amount about (0,0). Scalefactors between 0 and 1 shrink the shape.
Specified by:
`scale` in interface `GeometricObject2d`
Parameters:
`sc` - the scale factor.
• #### scale

```public void scale(Point2d centre,
float sc)```
Description copied from interface: `GeometricObject2d`
Scale the shape by the given amount about the given point. Scalefactors between 0 and 1 shrink the shape.
Specified by:
`scale` in interface `GeometricObject2d`
Parameters:
`centre` - the centre of the scaling operation
`sc` - the scale factor
• #### scaleCentroid

`public void scaleCentroid(float sc)`
Description copied from interface: `GeometricObject2d`
Scale the shape about its centroid. Scalefactors between 0 and 1 shrink the shape.
Specified by:
`scaleCentroid` in interface `GeometricObject2d`
Parameters:
`sc` - the scale factor
• #### calculateCentroid

`public Point2d calculateCentroid()`
Description copied from interface: `GeometricObject2d`
Calculate the centroid of the shape
Specified by:
`calculateCentroid` in interface `GeometricObject2d`
Returns:
the centroid of the shape
• #### calculateArea

`public double calculateArea()`
Description copied from interface: `Shape`
Calculate the area of the shape
Specified by:
`calculateArea` in interface `Shape`
Returns:
the area of the shape
• #### minX

`public double minX()`
Specified by:
`minX` in interface `GeometricObject2d`
Returns:
the minimum x-ordinate
• #### minY

`public double minY()`
Specified by:
`minY` in interface `GeometricObject2d`
Returns:
the minimum y-ordinate
• #### maxX

`public double maxX()`
Specified by:
`maxX` in interface `GeometricObject2d`
Returns:
the maximum x-ordinate
• #### maxY

`public double maxY()`
Specified by:
`maxY` in interface `GeometricObject2d`
Returns:
the maximum y-ordinate
• #### getWidth

`public double getWidth()`
Specified by:
`getWidth` in interface `GeometricObject2d`
Returns:
the width of the regular bounding box
• #### getHeight

`public double getHeight()`
Specified by:
`getHeight` in interface `GeometricObject2d`
Returns:
the height of the regular bounding box
• #### transform

`public Triangle transform(Jama.Matrix transform)`
Description copied from interface: `GeometricObject2d`
Apply a 3x3 transform matrix to a copy of the `GeometricObject2d` and return it
Specified by:
`transform` in interface `GeometricObject2d`
Specified by:
`transform` in interface `Shape`
Parameters:
`transform` - 3x3 transform matrix
Returns:
the transformed shape
• #### asPolygon

`public Polygon asPolygon()`
Description copied from interface: `Shape`
Convert the shape to a polygon representation
Specified by:
`asPolygon` in interface `Shape`
Returns:
a polygon representation of the shape
• #### firstVertex

`public Point2d firstVertex()`
Returns:
The first vertex.
• #### secondVertex

`public Point2d secondVertex()`
Returns:
The second vertex.
• #### thirdVertex

`public Point2d thirdVertex()`
Returns:
The third vertex.
• #### getEdges

`public List<Line2d> getEdges()`
Returns:
The edges of the triangle.
• #### sharesVertex

`public boolean sharesVertex(Triangle other)`
Test whether this triangle shares a vertex with another triangle.
Parameters:
`other` - the other triangle.
Returns:
true if a vertex is shared; false otherwise.
• #### intersectionArea

`public double intersectionArea(Shape that)`
Description copied from interface: `Shape`
Calls `Polygon.intersectionArea(Shape, int)` with 1 step per pixel dimension. Subsequently this function returns the shared whole pixels of this polygon and that.
Specified by:
`intersectionArea` in interface `Shape`
Returns:
intersection area
• #### intersectionArea

```public double intersectionArea(Shape that,
int nStepsPerDimention)```
Description copied from interface: `Shape`
Return an estimate for the area of the intersection of this polygon and another polygon. For each pixel step 1 is added if the point is inside both polygons. The length of a step in each direction is calculated as follows: max(intersectionWidth,intersectionHeight)/ (nStepsPerDimention) The total number of points inside the intersection of the shames is divided by the number of points read and multiplied by the total area of the intersection.
Specified by:
`intersectionArea` in interface `Shape`
Returns:
normalised intersection area
• #### clone

`public Triangle clone()`
Specified by:
`clone` in interface `Shape`
Overrides:
`clone` in class `Object`
Returns:
a copy of the shape
• #### toString

`public String toString()`
Overrides:
`toString` in class `Object`
• #### hashCode

`public int hashCode()`
Overrides:
`hashCode` in class `Object`
• #### equals

`public boolean equals(Object obj)`
Overrides:
`equals` in class `Object`
• #### intersectionSides

`public Map<Line2d,Point2d> intersectionSides(Line2d line)`
The intersection of this triangle with the line defined by y = mx + c. The line crosses at either 0, 1 or 2 points.
Parameters:
`line` - the line
Returns:
the intersecting edges of the triangle and points of intersection
• #### calculatePerimeter

`public double calculatePerimeter()`
Description copied from interface: `Shape`
Calculate the perimeter of the shape
Specified by:
`calculatePerimeter` in interface `Shape`
Returns:
the perimeter of the shape
• #### minimumBoundingRectangle

`public RotatedRectangle minimumBoundingRectangle()`
Description copied from interface: `Shape`
Compute the minimum size rotated bounding rectangle that contains this shape.
Specified by:
`minimumBoundingRectangle` in interface `Shape`
Returns:
the minimum bounding box
• #### isConvex

`public boolean isConvex()`
Description copied from interface: `Shape`
Test if the shape is convex.
Specified by:
`isConvex` in interface `Shape`
Returns:
true if the shape is convex; false if non-convex