Class Triangle2D

java.lang.Object
com.irurueta.geometry.Triangle2D
All Implemented Interfaces:
Serializable

public class Triangle2D extends Object implements Serializable
This class defines a triangle in the 2D space.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
    Default threshold value.
    static final double
    Minimum allowed threshold value.
    static final int
    Constant defining number of vertices on a triangle.
    private Point2D
    1st vertex of this triangle.
    private Point2D
    2nd vertex of this triangle.
    private Point2D
    3rd vertex of this triangle.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Triangle2D(Point2D vertex1, Point2D vertex2, Point2D vertex3)
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    area(Point2D vertex1, Point2D vertex2, Point2D vertex3)
    Returns area of a triangle formed by provided vertices.
    static double
    area(Triangle2D triangle)
    Returns area of provided triangle.
    boolean
    Indicates whether the vertices of this triangle are provided in clockwise order or not.
    boolean
    areVerticesClockwise(double threshold)
    Indicates whether the vertices of this triangle are provided in clockwise order or not.
    boolean
    Determines whether vertices of this triangle are considered to be co-linear.
    boolean
    areVerticesColinear(double threshold)
    Determines whether vertices of this triangle are considered to be co-linear up to certain threshold.
    void
    center(Point2D result)
    Computes the center of this triangle and stores the result in provided point.
    static Point2D
    center(Point2D vertex1, Point2D vertex2, Point2D vertex3)
    Computes the center of a triangle formed by provided vertices.
    static void
    center(Point2D vertex1, Point2D vertex2, Point2D vertex3, Point2D result)
    Computes the center of a triangle formed by provided vertices and stores the result in provided result point.
    static Point2D
    Computes the center of provided triangle.
    static void
    Computes the center of provided triangle and stores the result in provided result point.
    void
    closestPoint(Point2D point, Point2D result)
    Computes the point which is locus of this triangle closest to provided point and stores the result in provided result point.
    void
    closestPoint(Point2D point, Point2D result, double threshold)
    Computes the point which is locus of this triangle (up to a certain threshold) closest to provided point and stores the result in provided result point.
    double
    Returns area of this triangle.
    Returns center of this triangle, which is the result of averaging its vertices.
    Returns the point which is locus of this triangle closest to provided point.
    getClosestPoint(Point2D point, double threshold)
    Returns the point which is locus of this triangle (up to a certain threshold) closest to provided point.
    double
    Returns perimeter of this triangle.
    double
    Computes the shortest distance from a given point to the boundaries of this triangle, considering its boundaries as lines with a finite length Distance is computed up to triangle boundary, no matter if point lies inside the triangle or not.
    double
    Returns area of this triangle with sign.
    Returns 1st vertex of this triangle.
    Returns 2nd vertex of this triangle.
    Returns 3rd vertex of this triangle.
    Returns vertices of this triangle as a list of points.
    boolean
    Indicates whether provided point lies inside this triangle or not.
    boolean
    isInside(Point2D point, double threshold)
    Indicates whether provided point lies inside this triangle or not up to a certain threshold.
    static boolean
    isInside(Point2D vertex1, Point2D vertex2, Point2D vertex3, Point2D point)
    Indicates whether provided point lies inside a triangle formed by provided vertices or not.
    static boolean
    isInside(Point2D vertex1, Point2D vertex2, Point2D vertex3, Point2D point, double threshold)
    Indicates whether provided point lies inside a triangle formed by provided vertices or not up to a certain threshold.
    static boolean
    isInside(Triangle2D triangle, Point2D point)
    Indicates whether provided point lies inside provided triangle or not.
    static boolean
    isInside(Triangle2D triangle, Point2D point, double threshold)
    Indicates whether provided point lies inside provided triangle or not up to a certain threshold.
    boolean
    Returns boolean indicating if provided point is locus of this triangle (i.e. lies within this triangle boundaries).
    boolean
    isLocus(Point2D point, double threshold)
    Returns boolean indicating if provided point is locus of this triangle (i.e. lies within this triangle boundaries) up to a certain threshold.
    static double
    perimeter(Point2D vertex1, Point2D vertex2, Point2D vertex3)
    Returns perimeter of triangle formed by provided vertices.
    static double
    Returns perimeter of provided triangle.
    void
    Sets 1st vertex of this triangle.
    void
    Sets 2nd vertex of this triangle.
    void
    Sets 3rd vertex of this triangle.
    final void
    setVertices(Point2D vertex1, Point2D vertex2, Point2D vertex3)
    Sets all vertices of this triangle.
    static double
    shortestDistance(Point2D vertex1, Point2D vertex2, Point2D vertex3, Point2D point)
    Computes the shortest distance from a given point to the boundaries of a triangle formed by provided vertices, where those boundaries are considered to be lines with a finite length.
    static double
    Computes the shortest distance from a given point to the boundaries of provided triangle, considering its boundaries as lines with a finite length.
    static double
    signedArea(Point2D vertex1, Point2D vertex2, Point2D vertex3)
    Returns area with sign of the triangle formed by provided vertices.
    static double
    Returns area of provided triangle with sign.
    void
    Stores vertices of this triangle in provided list.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_THRESHOLD

      public static final double DEFAULT_THRESHOLD
      Default threshold value. Thresholds are used to determine whether a point lies inside the triangle or not, or if it's locus or not, etc.
      See Also:
    • MIN_THRESHOLD

      public static final double MIN_THRESHOLD
      Minimum allowed threshold value.
      See Also:
    • NUM_VERTICES

      public static final int NUM_VERTICES
      Constant defining number of vertices on a triangle.
      See Also:
    • vertex1

      private Point2D vertex1
      1st vertex of this triangle.
    • vertex2

      private Point2D vertex2
      2nd vertex of this triangle.
    • vertex3

      private Point2D vertex3
      3rd vertex of this triangle.
  • Constructor Details

    • Triangle2D

      public Triangle2D(Point2D vertex1, Point2D vertex2, Point2D vertex3)
      Constructor.
      Parameters:
      vertex1 - 1st vertex.
      vertex2 - 2nd vertex.
      vertex3 - 3rd vertex.
      Throws:
      NullPointerException - Raised if any of the vertices is null.
  • Method Details

    • getVertex1

      public Point2D getVertex1()
      Returns 1st vertex of this triangle.
      Returns:
      1st vertex.
    • setVertex1

      public void setVertex1(Point2D vertex1)
      Sets 1st vertex of this triangle.
      Parameters:
      vertex1 - 1st vertex.
      Throws:
      NullPointerException - Raised if provided vertex is null.
    • getVertex2

      public Point2D getVertex2()
      Returns 2nd vertex of this triangle.
      Returns:
      2nd vertex.
    • setVertex2

      public void setVertex2(Point2D vertex2)
      Sets 2nd vertex of this triangle.
      Parameters:
      vertex2 - 2nd vertex.
      Throws:
      NullPointerException - Raised if provided vertex is null.
    • getVertex3

      public Point2D getVertex3()
      Returns 3rd vertex of this triangle.
      Returns:
      3rd vertex.
    • setVertex3

      public void setVertex3(Point2D vertex3)
      Sets 3rd vertex of this triangle.
      Parameters:
      vertex3 - 3rd vertex.
      Throws:
      NullPointerException - Raised if provided vertex is null.
    • getVertices

      public List<Point2D> getVertices()
      Returns vertices of this triangle as a list of points.
      Returns:
      Vertices of this triangle.
    • vertices

      public void vertices(List<Point2D> result)
      Stores vertices of this triangle in provided list. Note that content of list will be cleared before storing this triangle's vertices.
      Parameters:
      result - list where vertices will be stored.
    • setVertices

      public final void setVertices(Point2D vertex1, Point2D vertex2, Point2D vertex3)
      Sets all vertices of this triangle.
      Parameters:
      vertex1 - 1st vertex.
      vertex2 - 2nd vertex.
      vertex3 - 3rd vertex.
      Throws:
      NullPointerException - Raised if any of the vertices is null.
    • signedArea

      public static double signedArea(Triangle2D triangle)
      Returns area of provided triangle with sign. If vertices are defined clockwise area is positive, otherwise returned area is negative.
      Parameters:
      triangle - Triangle to be evaluated.
      Returns:
      Area of triangle with sign. Positive sign indicates that vertices are clockwise, negative sign indicates that vertices are counterclockwise.
    • signedArea

      public static double signedArea(Point2D vertex1, Point2D vertex2, Point2D vertex3)
      Returns area with sign of the triangle formed by provided vertices. If vertices are defined clockwise area is negative, otherwise returned area is positive.
      Parameters:
      vertex1 - 1st vertex of a triangle.
      vertex2 - 2nd vertex of a triangle.
      vertex3 - 3rd vertex of a triangle.
      Returns:
      Area of a triangle with sign. Negative sign indicates that vertices are clockwise, positive sign indicates that vertices are counterclockwise.
    • getSignedArea

      public double getSignedArea()
      Returns area of this triangle with sign. If vertices are defined clockwise area is positive, otherwise returned area is negative.
      Returns:
      Area of this triangle with sign. Positive sign indicates that vertices are clockwise, negative sign indicates that vertices are counterclockwise.
    • area

      public static double area(Triangle2D triangle)
      Returns area of provided triangle.
      Parameters:
      triangle - Triangle to be checked.
      Returns:
      Area of triangle.
    • area

      public static double area(Point2D vertex1, Point2D vertex2, Point2D vertex3)
      Returns area of a triangle formed by provided vertices.
      Parameters:
      vertex1 - 1st vertex of a triangle.
      vertex2 - 2nd vertex of a triangle.
      vertex3 - 3rd vertex of a triangle.
      Returns:
      Area of a triangle.
    • getArea

      public double getArea()
      Returns area of this triangle.
      Returns:
      Area of this triangle.
    • areVerticesColinear

      public boolean areVerticesColinear()
      Determines whether vertices of this triangle are considered to be co-linear. Points are considered to be colinear when area of triangle is very small.
      Returns:
      True if vertices are colinear, false otherwise.
    • areVerticesColinear

      public boolean areVerticesColinear(double threshold)
      Determines whether vertices of this triangle are considered to be co-linear up to certain threshold. Points are considered to be colinear when are of triangle is very small.
      Parameters:
      threshold - Threshold to determine whether vertices are colinear. Vertices will be colinear when area of triangle is smaller than provided threshold.
      Returns:
      True if vertices are colinear, false otherwise.
      Throws:
      IllegalArgumentException - Raised if provided threshold is negative.
    • perimeter

      public static double perimeter(Triangle2D triangle)
      Returns perimeter of provided triangle.
      Parameters:
      triangle - Perimeter of provided triangle.
      Returns:
      Perimeter of provided triangle.
    • perimeter

      public static double perimeter(Point2D vertex1, Point2D vertex2, Point2D vertex3)
      Returns perimeter of triangle formed by provided vertices.
      Parameters:
      vertex1 - 1st vertex of a triangle.
      vertex2 - 2nd vertex of a triangle.
      vertex3 - 3rd vertex of a triangle.
      Returns:
      Perimeter of a triangle.
    • getPerimeter

      public double getPerimeter()
      Returns perimeter of this triangle.
      Returns:
      Perimeter of this triangle.
    • isInside

      public boolean isInside(Point2D point)
      Indicates whether provided point lies inside this triangle or not.
      Parameters:
      point - Point to be checked.
      Returns:
      True if point lies inside this triangle, false otherwise.
    • isInside

      public boolean isInside(Point2D point, double threshold)
      Indicates whether provided point lies inside this triangle or not up to a certain threshold.
      Parameters:
      point - Point to be checked.
      threshold - Threshold to determine whether point is inside this triangle, or not. This should usually be a small value.
      Returns:
      True if point lies inside this triangle, false otherwise.
      Throws:
      IllegalArgumentException - Raised if provided threshold is negative.
    • isInside

      public static boolean isInside(Triangle2D triangle, Point2D point)
      Indicates whether provided point lies inside provided triangle or not.
      Parameters:
      triangle - A triangle.
      point - Point to be checked.
      Returns:
      True if point lies inside provided triangle, false otherwise.
    • isInside

      public static boolean isInside(Triangle2D triangle, Point2D point, double threshold)
      Indicates whether provided point lies inside provided triangle or not up to a certain threshold.
      Parameters:
      triangle - A triangle.
      point - Point to be checked.
      threshold - Threshold to determine whether point is inside this triangle, or not. This should usually be a small value.
      Returns:
      True if point lies inside this triangle, false otherwise.
      Throws:
      IllegalArgumentException - Raised if provided threshold is negative.
    • isInside

      public static boolean isInside(Point2D vertex1, Point2D vertex2, Point2D vertex3, Point2D point)
      Indicates whether provided point lies inside a triangle formed by provided vertices or not.
      Parameters:
      vertex1 - 1st vertex of a triangle.
      vertex2 - 2nd vertex of a triangle.
      vertex3 - 3rd vertex of a triangle.
      point - Point to be checked.
      Returns:
      True if point lies inside triangle formed by provided vertices, false otherwise.
    • isInside

      public static boolean isInside(Point2D vertex1, Point2D vertex2, Point2D vertex3, Point2D point, double threshold)
      Indicates whether provided point lies inside a triangle formed by provided vertices or not up to a certain threshold.
      Parameters:
      vertex1 - 1st vertex of a triangle.
      vertex2 - 2nd vertex of a triangle.
      vertex3 - 3rd vertex of a triangle.
      point - Point to be checked.
      threshold - Threshold to determine whether point is inside the triangle formed by provided vertices or not. This should usually be a small value.
      Returns:
      True if point lies inside triangle formed by provided vertices, false otherwise.
      Throws:
      IllegalArgumentException - Raised if provided threshold is negative.
    • getCenter

      public Point2D getCenter()
      Returns center of this triangle, which is the result of averaging its vertices.
      Returns:
      Center of this triangle.
    • center

      public void center(Point2D result)
      Computes the center of this triangle and stores the result in provided point. The center of this triangle is computed as the average of its vertices.
      Parameters:
      result - Point instance where center will be stored.
    • center

      public static Point2D center(Point2D vertex1, Point2D vertex2, Point2D vertex3)
      Computes the center of a triangle formed by provided vertices. The center is computed as the average of the three vertices.
      Parameters:
      vertex1 - 1st vertex of a triangle.
      vertex2 - 2nd vertex of a triangle.
      vertex3 - 3rd vertex of a triangle.
      Returns:
      Center of a triangle formed by provided vertices.
    • center

      public static Point2D center(Triangle2D t)
      Computes the center of provided triangle. The center is computed as the average of the vertices of provided triangle.
      Parameters:
      t - A triangle.
      Returns:
      Center of provided triangle.
    • center

      public static void center(Point2D vertex1, Point2D vertex2, Point2D vertex3, Point2D result)
      Computes the center of a triangle formed by provided vertices and stores the result in provided result point. The center is computed as the average of provided vertices.
      Parameters:
      vertex1 - 1st vertex of a triangle.
      vertex2 - 2nd vertex of a triangle.
      vertex3 - 3rd vertex of a triangle.
      result - Point instance where center will be stored.
    • center

      public static void center(Triangle2D t, Point2D result)
      Computes the center of provided triangle and stores the result in provided result point. The center is computed as the average of the vertices of provided triangle.
      Parameters:
      t - A triangle.
      result - Point instance where center will be stored.
    • getShortestDistance

      public double getShortestDistance(Point2D point)
      Computes the shortest distance from a given point to the boundaries of this triangle, considering its boundaries as lines with a finite length Distance is computed up to triangle boundary, no matter if point lies inside the triangle or not.
      Parameters:
      point - Point to be checked.
      Returns:
      Shortest distance to this triangle.
    • shortestDistance

      public static double shortestDistance(Triangle2D triangle, Point2D point)
      Computes the shortest distance from a given point to the boundaries of provided triangle, considering its boundaries as lines with a finite length. Distance is computed up to triangle boundary, no matter if point lies inside the triangle or not.
      Parameters:
      triangle - A triangle.
      point - Point to be checked.
      Returns:
      Shortest distance to this triangle.
    • shortestDistance

      public static double shortestDistance(Point2D vertex1, Point2D vertex2, Point2D vertex3, Point2D point)
      Computes the shortest distance from a given point to the boundaries of a triangle formed by provided vertices, where those boundaries are considered to be lines with a finite length. Distance is computed up to triangle boundary, no matter if point lies inside the triangle or not.
      Parameters:
      vertex1 - 1st vertex of a triangle.
      vertex2 - 2nd vertex of a triangle.
      vertex3 - 3rd vertex of a triangle.
      point - Point to be checked.
      Returns:
      Shortest distance to the triangle formed by provided vertices.
    • getClosestPoint

      public Point2D getClosestPoint(Point2D point)
      Returns the point which is locus of this triangle closest to provided point.
      Parameters:
      point - Point to be checked.
      Returns:
      Closest point laying in this triangle boundaries.
    • getClosestPoint

      public Point2D getClosestPoint(Point2D point, double threshold)
      Returns the point which is locus of this triangle (up to a certain threshold) closest to provided point.
      Parameters:
      point - Point to be checked.
      threshold - Threshold to determine when a point is locus of this triangle or not.
      Returns:
      Closest point laying in this triangle boundaries.
      Throws:
      IllegalArgumentException - Raised if provided threshold is negative.
    • closestPoint

      public void closestPoint(Point2D point, Point2D result)
      Computes the point which is locus of this triangle closest to provided point and stores the result in provided result point.
      Parameters:
      point - Point to be checked.
      result - Point where result will be stored.
    • closestPoint

      public void closestPoint(Point2D point, Point2D result, double threshold)
      Computes the point which is locus of this triangle (up to a certain threshold) closest to provided point and stores the result in provided result point.
      Parameters:
      point - Point to be checked.
      result - Point where result will be stored.
      threshold - Threshold to determine when a point is locus of this triangle or not.
      Throws:
      IllegalArgumentException - Raised if provided threshold is negative.
    • isLocus

      public boolean isLocus(Point2D point, double threshold)
      Returns boolean indicating if provided point is locus of this triangle (i.e. lies within this triangle boundaries) up to a certain threshold.
      Parameters:
      point - Point to be checked.
      threshold - Threshold to determine if point is locus or not. This should usually be a small value.
      Returns:
      True if provided point is locus, false otherwise.
      Throws:
      IllegalArgumentException - Raised if provided threshold is negative.
    • isLocus

      public boolean isLocus(Point2D point)
      Returns boolean indicating if provided point is locus of this triangle (i.e. lies within this triangle boundaries).
      Parameters:
      point - Point to be checked.
      Returns:
      True if provided point is locus, false otherwise.
    • areVerticesClockwise

      public boolean areVerticesClockwise(double threshold)
      Indicates whether the vertices of this triangle are provided in clockwise order or not. Vertices of a triangle are in clockwise order when the triangle's signed area is negative.
      Parameters:
      threshold - Threshold to determine if vertices are clockwise or not. This should usually be 0.0 and there is no restriction in sign
      Returns:
      True if vertices of this triangle are in clockwise order, false if they are in counterclockwise order.
    • areVerticesClockwise

      public boolean areVerticesClockwise()
      Indicates whether the vertices of this triangle are provided in clockwise order or not.
      Returns:
      True if vertices of this triangle are in clockwise order, false if they are in counterclockwise order.