aim4.util

## Class GeomMath

- java.lang.Object
- - aim4.util.GeomMath

-     public final class GeomMath
      extends Object

  Class of static utility methods for geometric computation.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `GeomMath.IntersectionPoint` A class for storing the result of findLineLineIntersection.

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static double` | `HALF_PI` Math.PI / 2.0
    `static double` | `ONE_AND_HALF_PI` Math.PI * 3.0 / 2.0
    `static double` | `PI` Math.PI
    `static double` | `QUARTER_PI` Math.PI / 4.0
    `static double` | `TWO_PI` Math.PI * 2.0

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `static double` | `angleDiff(double ang1, double ang2)` Determine the angle between two angles.
    `static double` | `angleToPoint(Point2D p, Point2D startPoint)` Find the angle of the heading to a point from a given starting point.
    `static double` | `canonicalAngle(double angle)` Get the "canonical" angle.
    `static Point2D` | `centroid(List<Point2D> points)` Find the centroid of a list of points.
    `static double` | `crossProduct(Point2D p1, Point2D p2)` Compute the cross product of two 2D vectors.
    `static double` | `dotProduct(Point2D p1, Point2D p2)` Compute the dot product of two 2D vectors.
    `static Area` | `filledArea(Shape s)` Find the Area corresponding to the provided Shape with all holes filled in.
    `static Point2D` | `findLineLineIntersection(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)` Compute the intersection of two lines defined by two points each.
    `static Point2D` | `findLineLineIntersection(Line2D l1, Line2D l2)` Compute the intersection of two lines.
    `static GeomMath.IntersectionPoint` | `findLineLineIntersection(Point2D p1, double slope1, Point2D p2, double slope2)` Compute the intersection of two lines defined by two points each.
    `static boolean` | `intervalsOverlap(double t1start, double t1end, double t2start, double t2end)` Determine whether two intervals overlap.
    `static String` | `ordinalize(int num)` Turn a cardinal number into an ordinal number.
    `static Point2D` | `polarAdd(Point2D p, double r, double theta)` Find a point displaced by the given distance in the given direction.
    `static List<Double>` | `polygonalShapeAreas(Shape s)` Given a polygonal, non-overlapping Shape, return the areas of the closed portions of the shape.
    `static Point2D` | `polygonalShapeCentroid(Shape s)` Given a polygonal Shape, return the centroid of the shape.
    `static List<Line2D>` | `polygonalShapePerimeterSegments(Shape s)` Given a polygonal Shape, return a list of segments describing its perimeter.
    `static List<List<Point2D>>` | `polygonalSubShapeVertices(Shape s)` Given a polygonal Shape, return a list of lists of points that are the vertices of the closed polygonal sub-shapes.
    `static double` | `quadraticFormula(double a, double b, double c)` Solve the quadratic formula ax2 + bx + c = 0 given coefficients a, b, and c, returning the minimum nonnegative root, or the largest root if both are negative.
    `static List<Area>` | `subareas(Shape s)` Find all the subareas of a Shape.
    `static Point2D` | `subtract(Point2D p1, Point2D p2)` Subtract two 2D vectors.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### QUARTER_PI

          public static final double QUARTER_PI

      Math.PI / 4.0
      See Also:
        Constant Field Values

    - #### HALF_PI

          public static final double HALF_PI

      Math.PI / 2.0
      See Also:
        Constant Field Values

    - #### PI

          public static final double PI

      Math.PI
      See Also:
        Constant Field Values

    - #### ONE_AND_HALF_PI

          public static final double ONE_AND_HALF_PI

      Math.PI * 3.0 / 2.0
      See Also:
        Constant Field Values

    - #### TWO_PI

          public static final double TWO_PI

      Math.PI * 2.0
      See Also:
        Constant Field Values

  - ### Method Detail

    - #### ordinalize

          public static String ordinalize(int num)

      Turn a cardinal number into an ordinal number.
      Parameters:
        `num` - the cardinal number
      Returns:
        the ordinal version of the given number

    - #### canonicalAngle

          public static double canonicalAngle(double angle)

      Get the "canonical" angle. This is used for recentering angles in [0.0, 2*Pi).
      Parameters:
        `angle` - the angle
      Returns:
        an equivalent angle that is greater than or equal to 0.0 and less than 2*PI

    - #### angleToPoint

          public static double angleToPoint(Point2D p,
                            Point2D startPoint)

      Find the angle of the heading to a point from a given starting point.
      Parameters:
        `p` - the point to which to find the angle
        `startPoint` - the point from which to start
      Returns:
        the angle of the heading to the first point when starting at the second point

    - #### polarAdd

          public static Point2D polarAdd(Point2D p,
                         double r,
                         double theta)

      Find a point displaced by the given distance in the given direction.
      Parameters:
        `p` - the starting point
        `r` - the distance to move from the point
        `theta` - the angle at which to move from the point
      Returns:
        the point displaced the given distance in the given direction from the original point

    - #### subtract

          public static Point2D subtract(Point2D p1,
                         Point2D p2)

      Subtract two 2D vectors.
      Parameters:
        `p1` - the first vector
        `p2` - the second vector
      Returns:
        a Point representing the subtraction of the second vector from the first

    - #### dotProduct

          public static double dotProduct(Point2D p1,
                          Point2D p2)

      Compute the dot product of two 2D vectors.
      Parameters:
        `p1` - the first vector
        `p2` - the second vector
      Returns:
        the dot product of the two vectors

    - #### crossProduct

          public static double crossProduct(Point2D p1,
                            Point2D p2)

      Compute the cross product of two 2D vectors.
      Parameters:
        `p1` - the first vector
        `p2` - the second vector
      Returns:
        the cross product of the two vectors

    - #### findLineLineIntersection

          public static Point2D findLineLineIntersection(Line2D l1,
                                         Line2D l2)

      Compute the intersection of two lines.
      Parameters:
        `l1` - the first line
        `l2` - the second line
      Returns:
        the intersection point of the two lines

    - #### findLineLineIntersection

          public static Point2D findLineLineIntersection(double x1,
                                         double y1,
                                         double x2,
                                         double y2,
                                         double x3,
                                         double y3,
                                         double x4,
                                         double y4)

      Compute the intersection of two lines defined by two points each. Undefined behavior if the lines are parallel.
      Parameters:
        `x1` - the x coordinate of the first point of the first line
        `y1` - the y coordinate of the first point of the first line
        `x2` - the x coordinate of the second point of the first line
        `y2` - the y coordinate of the second point of the first line
        `x3` - the x coordinate of the first point of the second line
        `y3` - the y coordinate of the first point of the second line
        `x4` - the x coordinate of the second point of the second line
        `y4` - the y coordinate of the second point of the second line
      Returns:
        the intersection point of the two lines.

    - #### findLineLineIntersection

          public static GeomMath.IntersectionPoint findLineLineIntersection(Point2D p1,
                                                            double slope1,
                                                            Point2D p2,
                                                            double slope2)

      Compute the intersection of two lines defined by two points each. Undefined behavior if the lines are parallel.
      Parameters:
        `p1` - an intercepting point of the first line
        `slope1` - the slope of the first line
        `p2` - an intercepting point of the second line
        `slope2` - the slope of the second line
      Returns:
        the intersection point of the two lines, with the distances between the intersection points and the intercepting points.

    - #### polygonalSubShapeVertices

          public static List<List<Point2D>> polygonalSubShapeVertices(Shape s)

      Given a polygonal Shape, return a list of lists of points that are the vertices of the closed polygonal sub-shapes.
      Parameters:
        `s` - the polygonal Shape
      Returns:
        the lists of sub-shape vertices
      Throws:
        `IllegalArgumentException` - if the Shape is not polygonal

    - #### polygonalShapePerimeterSegments

          public static List<Line2D> polygonalShapePerimeterSegments(Shape s)

      Given a polygonal Shape, return a list of segments describing its perimeter.
      Parameters:
        `s` - the polygonal Shape
      Returns:
        a list of segments describing the perimeter of the Shape
      Throws:
        `IllegalArgumentException` - if the Shape is not polygonal

    - #### polygonalShapeAreas

          public static List<Double> polygonalShapeAreas(Shape s)

      Given a polygonal, non-overlapping Shape, return the areas of the closed portions of the shape. If the Shape is not polygonal or it overlaps itself, behavior is undefined.
      Parameters:
        `s` - the polygonal Shape
      Returns:
        the areas of the closed portions of the Shape

    - #### polygonalShapeCentroid

          public static Point2D polygonalShapeCentroid(Shape s)

      Given a polygonal Shape, return the centroid of the shape. If the Shape is not polygonal, behavior is undefined.
      Parameters:
        `s` - the polygonal Shape
      Returns:
        the centroid of the Shape

    - #### centroid

          public static Point2D centroid(List<Point2D> points)

      Find the centroid of a list of points.
      Parameters:
        `points` - the List of Points
      Returns:
        the centroid of the Points

    - #### filledArea

          public static Area filledArea(Shape s)

      Find the Area corresponding to the provided Shape with all holes filled in. Takes the union of all the subareas according to `subareas(Shape s)`.
      Parameters:
        `s` - the Shape to fill in
      Returns:
        an Area representing the Shape with all holes filled in

    - #### subareas

          public static List<Area> subareas(Shape s)

      Find all the subareas of a Shape. This means that if a Shape is defined as a Shape with a piece missing from it, this will return both an Area for the full Shape as well as one for the hole in the middle. If multiple pieces exist, they will each be returned individually.
      Parameters:
        `s` - the Shape to deconstruct.
      Returns:
        a List of subareas that make up the Shape

    - #### quadraticFormula

          public static double quadraticFormula(double a,
                                double b,
                                double c)

      Solve the quadratic formula ax2 + bx + c = 0 given coefficients a, b, and c, returning the minimum nonnegative root, or the largest root if both are negative.
      Parameters:
        `a` - the coefficient of the x2 term
        `b` - the coefficient of the x term
        `c` - the constant term
      Returns:
        the minimum nonnegative root, or the largest root if both are negative

    - #### intervalsOverlap

          public static boolean intervalsOverlap(double t1start,
                                 double t1end,
                                 double t2start,
                                 double t2end)

      Determine whether two intervals overlap. This is determined by checking if either of two things is true: the start of the first interval falls inside the duration of the second interval, or the start of the second interval falls within the duration of the first interval. If neither is true, they cannot overlap.
      Parameters:
        `t1start` - the start of the first interval
        `t1end` - the end of the first interval
        `t2start` - the start of the second interval
        `t2end` - the end of the second interval
      Returns:
        whether the provided two intervals overlap

    - #### angleDiff

          public static double angleDiff(double ang1,
                         double ang2)

      Determine the angle between two angles. This includes the cases where the shortest angle between the two crosses the positive X axis.
      Parameters:
        `ang1` - the first angle
        `ang2` - the second angle
      Returns:
        the angle between the two given angles

Copyright © 2013. All rights reserved.