Class Ray2d

All Implemented Interfaces:
Serializable, Directed<DirectedPoint2d>, Directed2d<DirectedPoint2d>, Drawable<Point2d>, Drawable2d, Project<Point2d>, Ray<Ray2d,DirectedPoint2d,Point2d>, Point<Point2d>

public class Ray2d extends DirectedPoint2d implements Drawable2d, Ray<Ray2d,DirectedPoint2d,Point2d>
Ray2d is a half-line; it has one end point with finite coordinates; the other end point is infinitely far away.

Copyright (c) 2020-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See DJUTILS License.

Author:
Alexander Verbraeck, Peter Knoppers
See Also:
  • Constructor Details

    • Ray2d

      public Ray2d(double x, double y, double dirZ) throws DrawRuntimeException
      Construct a new Ray2d.
      Parameters:
      x - double; the x coordinate of the finite end point of the ray
      y - double; the y coordinate of the finite end point of the ray
      dirZ - double; the angle from the positive X axis direction in radians.
      Throws:
      DrawRuntimeException - when dirZ is NaN
    • Ray2d

      public Ray2d(double[] xy, double dirZ) throws IllegalArgumentException
      Construct a new Ray2d from x and y coordinates in a double[] and a direction.
      Parameters:
      xy - double[]; the x and y coordinates of the finite end point in that order
      dirZ - double; the counter-clockwise rotation around the point in radians
      Throws:
      NullPointerException - when xy is null
      IllegalArgumentException - when the dimension of xy is not 2 or any value in xy is NaN or rotZ is NaN
    • Ray2d

      public Ray2d(Point2D point, double dirZ) throws IllegalArgumentException
      Construct a new Ray2d from an AWT Point2D and a direction.
      Parameters:
      point - Point2D; an AWT Point2D
      dirZ - double; the counter-clockwise rotation around the point in radians
      Throws:
      IllegalArgumentException - when any coordinate in point is NaN, or rotZ is NaN
    • Ray2d

      public Ray2d(Point2d point, double dirZ) throws NullPointerException, DrawRuntimeException
      Construct a new Ray2d from a Point2d and a direction.
      Parameters:
      point - Point2d; the finite end point of the ray
      dirZ - double; the angle from the positive X axis direction in radians.
      Throws:
      NullPointerException - when point is null
      DrawRuntimeException - when dirZ is NaN
    • Ray2d

      public Ray2d(double x, double y, double throughX, double throughY) throws DrawRuntimeException
      Construct a new Ray2d.
      Parameters:
      x - double; the x coordinate of the finite end point of the ray
      y - double; the y coordinate of the finite end point of the ray
      throughX - double; the x coordinate of another point on the ray
      throughY - double; the y coordinate of another point on the ray
      Throws:
      DrawRuntimeException - when throughX == x and throughY == y, or any through-value is NaN
    • Ray2d

      public Ray2d(Point2d point, double throughX, double throughY) throws NullPointerException, DrawRuntimeException
      Construct a new Ray2d.
      Parameters:
      point - Point2d; the finite end point of the ray
      throughX - double; the x coordinate of another point on the ray
      throughY - double; the y coordinate of another point on the ray
      Throws:
      NullPointerException - when point is null
      DrawRuntimeException - when throughX == point.x and throughY == point.y, or any through-value is NaN
    • Ray2d

      public Ray2d(double x, double y, Point2d throughPoint) throws NullPointerException, DrawRuntimeException
      Construct a new Ray2d.
      Parameters:
      x - double; the x coordinate of the finite end point of the ray
      y - double; the y coordinate of the finite end point of the ray
      throughPoint - Point2d; another point on the ray
      Throws:
      NullPointerException - when throughPoint is null
      DrawRuntimeException - when throughPoint is exactly at (x, y)
    • Ray2d

      public Ray2d(Point2d point, Point2d throughPoint) throws NullPointerException, DrawRuntimeException
      Construct a new Ray2d.
      Parameters:
      point - Point2d; the finite end point of the ray
      throughPoint - Point2d; another point on the ray
      Throws:
      NullPointerException - when point is null or throughPoint is null
      DrawRuntimeException - when throughPoint is exactly at point
    • Ray2d

      public Ray2d(DirectedPoint2d directedPoint)
      Construct a new Ray2d.
      Parameters:
      directedPoint - DirectedPoint2d; point and direction of the new Ray2d
  • Method Details

    • getDirZ

      public final double getDirZ()
      Description copied from interface: Directed2d
      Retrieve the angle from the positive x-axis. Positive rotates towards the positive y-axis (and beyond).
      Specified by:
      getDirZ in interface Directed2d<DirectedPoint2d>
      Specified by:
      getDirZ in interface Ray<Ray2d,DirectedPoint2d,Point2d>
      Overrides:
      getDirZ in class DirectedPoint2d
      Returns:
      double; dirZ
    • getEndPoint

      public DirectedPoint2d getEndPoint()
      Description copied from interface: Ray
      Get the finite end point of this Ray.
      Specified by:
      getEndPoint in interface Ray<Ray2d,DirectedPoint2d,Point2d>
      Returns:
      P; the finite end point of this Ray
    • size

      public int size()
      Description copied from interface: Drawable
      Retrieve the number of points that make up the object.
      Specified by:
      size in interface Drawable<Point2d>
      Overrides:
      size in class Point2d
      Returns:
      int; the number of points that make up the object
    • getPoints

      public Iterator<DirectedPoint2d> getPoints()
      Description copied from interface: Drawable
      Retrieve, or generate all points that make up the object.
      Specified by:
      getPoints in interface Drawable<Point2d>
      Overrides:
      getPoints in class DirectedPoint2d
      Returns:
      Iterable<Point2d>; an iterator that generates all points that make up the object
    • getBounds

      public Bounds2d getBounds()
      Description copied from interface: Drawable2d
      Retrieve the bounding rectangle of the object.
      Specified by:
      getBounds in interface Drawable2d
      Overrides:
      getBounds in class Point2d
      Returns:
      Bounds2d; the bounding box of the object
    • neg

      public Ray2d neg()
      Description copied from interface: Point
      Return a new Point with negated coordinate values. If this is a DirectedPoint, dirY and dirZ are negated.
      Specified by:
      neg in interface Directed<DirectedPoint2d>
      Specified by:
      neg in interface Point<Point2d>
      Overrides:
      neg in class DirectedPoint2d
      Returns:
      Point; a new point with negated coordinate values
    • flip

      public Ray2d flip()
      Description copied from interface: Ray
      Flip the direction of the Ray (creates and returns a new Ray instance).
      Specified by:
      flip in interface Ray<Ray2d,DirectedPoint2d,Point2d>
      Returns:
      R; Ray at the same location, but with dirZ (in case of a Ray3d) incremented by π and dirY subtracted from π
    • getLocationExtended

      public Ray2d getLocationExtended(double position) throws DrawRuntimeException
      Description copied from interface: Ray
      Get the location at a position on the line, with its direction. Position must be a finite value
      Specified by:
      getLocationExtended in interface Ray<Ray2d,DirectedPoint2d,Point2d>
      Parameters:
      position - double; the position on the line for which to calculate the point on the line
      Returns:
      R; a ray with the same direction as this ray
      Throws:
      DrawRuntimeException - when position infinite, or NaN.
    • closestPointOnRay

      public Point2d closestPointOnRay(Point2d point) throws NullPointerException
      Description copied from interface: Ray
      Project a Point on a Ray. If the the projected points lies outside the ray, the start point of the ray is returned. Otherwise the closest point on the ray is returned.
      Adapted from example code provided by Paul Bourke.
      Specified by:
      closestPointOnRay in interface Ray<Ray2d,DirectedPoint2d,Point2d>
      Parameters:
      point - P; the point to project onto the segment
      Returns:
      D; either the start point, or DirectedPoint that lies somewhere on this Ray.
      Throws:
      NullPointerException - when point is null
    • projectOrthogonal

      public Point2d projectOrthogonal(Point2d point) throws NullPointerException
      Description copied from interface: Project
      Project a point onto this object. For PolyLines and Polygons, there may be multiple valid solutions. In that case the solution that lies on the closest segment is returned. If there is no valid solution on the closest segment, null is returned.
      Specified by:
      projectOrthogonal in interface Project<Point2d>
      Parameters:
      point - P; the point
      Returns:
      P; the projection of the point (may be null if no sensible projection is possible). If the result is not null; the result lies somewhere on this object.
      Throws:
      NullPointerException - when point is null;
    • projectOrthogonalExtended

      public Point2d projectOrthogonalExtended(Point2d point)
      Description copied from interface: Project
      Project a point onto this object. For PolyLines and Polygons, there may be multiple valid solutions. In that case the solution that lies on the closest segment is returned.
      Specified by:
      projectOrthogonalExtended in interface Project<Point2d>
      Parameters:
      point - P; the point
      Returns:
      P; the projection of the point. This result cannot be null, but it may not lie on this object, but, in stead, lie on a line, or plane that extends this object
    • projectOrthogonalFractional

      public double projectOrthogonalFractional(Point2d point) throws NullPointerException
      Description copied from interface: Project
      Project a point onto this object. For PolyLines and Polygons, there may be multiple valid solutions. In that case the solution that lies on the closest segment is returned. If there is no valid solution on the closest segment, NaN is returned.
      Specified by:
      projectOrthogonalFractional in interface Project<Point2d>
      Parameters:
      point - P; the point
      Returns:
      double; the fractional position of the projection of the point (may be NaN if no sensible projection is possible). If the result is not NaN; the result lies somewhere on this object.
      Throws:
      NullPointerException - when point is null;
    • projectOrthogonalFractionalExtended

      public double projectOrthogonalFractionalExtended(Point2d point) throws NullPointerException
      Description copied from interface: Project
      Project a point onto this object. For PolyLines and Polygons, there may be multiple valid solutions. In that case the solution that lies on the closest segment is returned.
      Specified by:
      projectOrthogonalFractionalExtended in interface Project<Point2d>
      Parameters:
      point - P; the point
      Returns:
      double; the fractional position of the projection of the point. This result cannot be NaN, but it may be outside the range 0.0 .. 1.0.
      Throws:
      NullPointerException - when point is null;
    • toString

      public String toString()
      Description copied from interface: Drawable
      Produce a string describing the Drawable using default conversion for the (double) coordinate values. Regrettably, it is not allowed to provide a default implementation here.
      Specified by:
      toString in interface Drawable<Point2d>
      Overrides:
      toString in class DirectedPoint2d
      Returns:
      String; a string describing the Drawable
    • toString

      public String toString(String doubleFormat, boolean doNotIncludeClassName)
      Description copied from interface: Drawable
      Produce a String describing the Drawable.
      Specified by:
      toString in interface Drawable<Point2d>
      Overrides:
      toString in class DirectedPoint2d
      Parameters:
      doubleFormat - String; a format string (something like "%6.3f") which will be used to render every coordinate value)
      doNotIncludeClassName - boolean; if true; the output of toString is not prefixed by the class name. This is useful for concatenating the textual representation of lots of Drawables (e.g. an array, or a List).
      Returns:
      String; textual representation of the Drawable
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class DirectedPoint2d
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class DirectedPoint2d