Class Point2d

    • Field Detail

      • x

        public final double x
        The x-coordinate.
      • y

        public final double y
        The y-coordinate.
    • Method Detail

      • getX

        public final double getX()
        Return the x-coordinate. When the point is not in Cartesian space, a calculation to Cartesian space has to be made.
        Specified by:
        getX in interface Point<Point2d,​Space2d>
        Returns:
        double; the x-coordinate
      • getY

        public final double getY()
        Return the y-coordinate. When the point is not in Cartesian space, a calculation to Cartesian space has to be made.
        Specified by:
        getY in interface Point<Point2d,​Space2d>
        Returns:
        double; the y-coordinate
      • distance

        public double distance​(Point2d otherPoint)
        Return the distance to another point.
        Specified by:
        distance in interface Point<Point2d,​Space2d>
        Parameters:
        otherPoint - P; P the other point
        Returns:
        double; the distance (2d or 3d as applicable) to the other point
      • distanceSquared

        public double distanceSquared​(Point2d otherPoint)
                               throws NullPointerException
        Return the squared distance between this point and the provided point.
        Specified by:
        distanceSquared in interface Point<Point2d,​Space2d>
        Parameters:
        otherPoint - P; the other point
        Returns:
        double; the squared distance between this point and the other point
        Throws:
        NullPointerException - when otherPoint is null
      • size

        public int size()
        Retrieve the number of points that make up the object.
        Specified by:
        size in interface Drawable<Point2d,​Space2d>
        Returns:
        int; the number of points that make up the object
      • getPoints

        public Iterator<? extends Point2d> getPoints()
        Retrieve, or generate all points that make up the object.
        Specified by:
        getPoints in interface Drawable<Point2d,​Space2d>
        Returns:
        Iterable<Point2d>; an iterator that generates all points that make up the object
      • translate

        public Point2d translate​(double dx,
                                 double dy)
        Return a new Point with a translation by the provided dx and dy.
        Parameters:
        dx - double; the horizontal translation
        dy - double; the vertical translation
        Returns:
        P; a new point with the translated coordinates
        Throws:
        IllegalArgumentException - when dx, or dy is NaN
      • translate

        public Point3d translate​(double dx,
                                 double dy,
                                 double dz)
        Return a new Point3d with a translation by the provided delta-x, delta-y and deltaZ.
        Parameters:
        dx - double; the x translation
        dy - double; the y translation
        dz - double; the z translation
        Returns:
        Point2d; a new point with the translated coordinates
        Throws:
        IllegalArgumentException - when dx, dy, or dz is NaN
      • scale

        public Point2d scale​(double factor)
        Return a new Point with the coordinates of this point scaled by the provided factor.
        Specified by:
        scale in interface Point<Point2d,​Space2d>
        Parameters:
        factor - double; the scale factor
        Returns:
        Point; a new point with the coordinates of this point scaled by the provided factor
      • neg

        public Point2d neg()
        Return a new Point with negated coordinate values.
        Specified by:
        neg in interface Point<Point2d,​Space2d>
        Returns:
        Point; a new point with negated coordinate values
      • abs

        public Point2d abs()
        Return a new Point with absolute coordinate values.
        Specified by:
        abs in interface Point<Point2d,​Space2d>
        Returns:
        Point; a new point with absolute coordinate values
      • interpolate

        public Point2d interpolate​(Point2d point,
                                   double fraction)
        Interpolate towards another Point with a fraction. It is allowed for fraction to be less than zero or larger than 1. In that case the interpolation turns into an extrapolation.
        Specified by:
        interpolate in interface Point<Point2d,​Space2d>
        Parameters:
        point - P; the other point
        fraction - double; the factor for interpolation towards the other point. When <code>fraction</code> is between 0 and 1, it is an interpolation, otherwise an extrapolation. If fraction is 0; this Point is returned; if fraction is 1, the other point is returned
        Returns:
        P; the point that is fraction away on the line between this point and the other point
      • epsilonEquals

        public boolean epsilonEquals​(Point2d other,
                                     double epsilon)
        A comparison with another point that returns true of each of the coordinates is less than epsilon apart.
        Specified by:
        epsilonEquals in interface Point<Point2d,​Space2d>
        Parameters:
        other - P; the point to compare with
        epsilon - double; the upper bound of difference for one of the coordinates
        Returns:
        boolean; true if both x, y and z (if a Point3d) are less than epsilon apart, otherwise false
      • getBounds

        public Bounds2d getBounds()
        Retrieve the bounding rectangle of the object.
        Specified by:
        getBounds in interface Drawable2d
        Returns:
        Bounds2d; the bounding box of the object
      • intersectionOfLines

        public static Point2d intersectionOfLines​(Point2d line1P1,
                                                  Point2d line1P2,
                                                  Point2d line2P1,
                                                  Point2d line2P2)
        Compute the 2D intersection of two lines. Both lines are defined by two points (that should be distinct).
        Parameters:
        line1P1 - Point2d; first point of line 1
        line1P2 - Point2d; second point of line 1
        line2P1 - Point2d; first point of line 2
        line2P2 - Point2d; second point of line 2
        Returns:
        Point2d; the intersection of the two lines, or null if the lines are (almost) parallel
      • intersectionOfLineSegments

        public static Point2d intersectionOfLineSegments​(Point2d line1P1,
                                                         Point2d line1P2,
                                                         Point2d line2P1,
                                                         Point2d line2P2)
        Compute the 2D intersection of two line segments. Both line segments are defined by two points (that should be distinct).
        Parameters:
        line1P1 - Point2d; first point of line segment 1
        line1P2 - Point2d; second point of line segment 1
        line2P1 - Point2d; first point of line segment 2
        line2P2 - Point2d; second point of line segment 2
        Returns:
        Point2d; the intersection of the two line segments, or null if the lines are (almost) parallel, or do not intersect
      • closestPointOnSegment

        public final Point2d closestPointOnSegment​(Point2d segmentPoint1,
                                                   Point2d segmentPoint2)
        Project a point on a line segment. If the the projected points lies outside the line segment, the nearest end point of the line segment is returned. Otherwise the returned point lies between the end points of the line segment.
        Adapted from example code provided by Paul Bourke.
        Specified by:
        closestPointOnSegment in interface Point<Point2d,​Space2d>
        Parameters:
        segmentPoint1 - P; start of line segment
        segmentPoint2 - P; end of line segment
        Returns:
        P; either segmentPoint1, or segmentPoint2 or a new Point2d that lies somewhere in between those two.
      • closestPointOnLine

        public final Point2d closestPointOnLine​(Ray2d ray)
        Closest point on a ray.
        Parameters:
        ray - Ray2d; the ray
        Returns:
        Point2d; the point on the ray that is closest to this
      • circleIntersections

        public static final List<Point2d> circleIntersections​(Point2d center1,
                                                              double radius1,
                                                              Point2d center2,
                                                              double radius2)
                                                       throws NullPointerException,
                                                              DrawRuntimeException
        Return the zero, one or two intersections between two circles. The circles must be different. Derived from pseudo code by Paul Bourke and C implementation by Tim Voght .
        Parameters:
        center1 - Point2d; the center of circle 1
        radius1 - double; the radius of circle 1
        center2 - Point2d; the center of circle 2
        radius2 - double; the radius of circle 2
        Returns:
        List<Point2d> a list of zero, one or two points
        Throws:
        NullPointerException - when center1 or center2 is null
        DrawRuntimeException - when the two circles are identical, or radius1 < 0 or radius2 < 0
      • directionTo

        public double directionTo​(Point2d otherPoint)
        Return the direction to another Point2d.
        Parameters:
        otherPoint - Point2d; the other point
        Returns:
        double; the direction to the other point in Radians (towards infinite X is 0; towards infinite Y is π / 2; etc.). If the points are identical; this method returns NaN.
      • toPoint2D

        public Point2D toPoint2D()
        Return the coordinates as an AWT Point2D.Double object.
        Returns:
        Point2D; the coordinates as an AWT Point2D.Double object
      • toString

        public String toString​(int fractionDigits)
        Return a string representation of the point with a certain number of fraction digits for the coordinates.
        Specified by:
        toString in interface Point<Point2d,​Space2d>
        Parameters:
        fractionDigits - int; the number of fraction digits. Should be 0 or a positive number
        Returns:
        String; a formatted string with a certain number of fraction digits for the coordinates
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object