类 Point2D

  • 所有已实现的接口:
    java.lang.Comparable<Point2D>

    public final class Point2D
    extends java.lang.Object
    implements java.lang.Comparable<Point2D>
    The Point class is an immutable data type to encapsulate a two-dimensional point with real-value coordinates.

    Note: in order to deal with the difference behavior of double and Double with respect to -0.0 and +0.0, the Point2D constructor converts any coordinates that are -0.0 to +0.0.

    For additional documentation, see Section 1.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.

    • 字段概要

      字段 
      修饰符和类型 字段 说明
      static java.util.Comparator<Point2D> R_ORDER
      Compares two points by polar radius.
      static java.util.Comparator<Point2D> X_ORDER
      Compares two points by x-coordinate.
      static java.util.Comparator<Point2D> Y_ORDER
      Compares two points by y-coordinate.
    • 构造器概要

      构造器 
      构造器 说明
      Point2D​(double x, double y)
      Initializes a new point (x, y).
    • 方法概要

      修饰符和类型 方法 说明
      static double area2​(Point2D a, Point2D b, Point2D c)
      Returns twice the signed area of the triangle a-b-c.
      java.util.Comparator<Point2D> atan2Order()
      Compares two points by atan2() angle (between –π and π) with respect to this point.
      static int ccw​(Point2D a, Point2D b, Point2D c)
      Returns true if a→b→c is a counterclockwise turn.
      int compareTo​(Point2D that)
      Compares two points by y-coordinate, breaking ties by x-coordinate.
      double distanceSquaredTo​(Point2D that)
      Returns the square of the Euclidean distance between this point and that point.
      double distanceTo​(Point2D that)
      Returns the Euclidean distance between this point and that point.
      java.util.Comparator<Point2D> distanceToOrder()
      Compares two points by distance to this point.
      void draw()
      Plot this point using standard draw.
      void drawTo​(Point2D that)
      Plot a line from this point to that point using standard draw.
      boolean equals​(java.lang.Object other)
      Compares this point to the specified point.
      int hashCode()
      Returns an integer hash code for this point.
      static void main​(java.lang.String[] args)
      Unit tests the point data type.
      java.util.Comparator<Point2D> polarOrder()
      Compares two points by polar angle (between 0 and 2π) with respect to this point.
      double r()
      Returns the polar radius of this point.
      double theta()
      Returns the angle of this point in polar coordinates.
      java.lang.String toString()
      Return a string representation of this point.
      double x()
      Returns the x-coordinate.
      double y()
      Returns the y-coordinate.
      • 从类继承的方法 java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • 字段详细资料

      • X_ORDER

        public static final java.util.Comparator<Point2D> X_ORDER
        Compares two points by x-coordinate.
      • Y_ORDER

        public static final java.util.Comparator<Point2D> Y_ORDER
        Compares two points by y-coordinate.
      • R_ORDER

        public static final java.util.Comparator<Point2D> R_ORDER
        Compares two points by polar radius.
    • 构造器详细资料

      • Point2D

        public Point2D​(double x,
                       double y)
        Initializes a new point (x, y).
        参数:
        x - the x-coordinate
        y - the y-coordinate
        抛出:
        java.lang.IllegalArgumentException - if either x or y is Double.NaN, Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY
    • 方法详细资料

      • x

        public double x()
        Returns the x-coordinate.
        返回:
        the x-coordinate
      • y

        public double y()
        Returns the y-coordinate.
        返回:
        the y-coordinate
      • r

        public double r()
        Returns the polar radius of this point.
        返回:
        the polar radius of this point in polar coordiantes: sqrt(x*x + y*y)
      • theta

        public double theta()
        Returns the angle of this point in polar coordinates.
        返回:
        the angle (in radians) of this point in polar coordiantes (between –π and π)
      • ccw

        public static int ccw​(Point2D a,
                              Point2D b,
                              Point2D c)
        Returns true if a→b→c is a counterclockwise turn.
        参数:
        a - first point
        b - second point
        c - third point
        返回:
        { -1, 0, +1 } if a→b→c is a { clockwise, collinear; counterclocwise } turn.
      • area2

        public static double area2​(Point2D a,
                                   Point2D b,
                                   Point2D c)
        Returns twice the signed area of the triangle a-b-c.
        参数:
        a - first point
        b - second point
        c - third point
        返回:
        twice the signed area of the triangle a-b-c
      • distanceTo

        public double distanceTo​(Point2D that)
        Returns the Euclidean distance between this point and that point.
        参数:
        that - the other point
        返回:
        the Euclidean distance between this point and that point
      • distanceSquaredTo

        public double distanceSquaredTo​(Point2D that)
        Returns the square of the Euclidean distance between this point and that point.
        参数:
        that - the other point
        返回:
        the square of the Euclidean distance between this point and that point
      • compareTo

        public int compareTo​(Point2D that)
        Compares two points by y-coordinate, breaking ties by x-coordinate. Formally, the invoking point (x0, y0) is less than the argument point (x1, y1) if and only if either y0 < y1 or if y0 == y1 and x0 < x1.
        指定者:
        compareTo 在接口中 java.lang.Comparable<Point2D>
        参数:
        that - the other point
        返回:
        the value 0 if this string is equal to the argument string (precisely when equals() returns true); a negative integer if this point is less than the argument point; and a positive integer if this point is greater than the argument point
      • polarOrder

        public java.util.Comparator<Point2D> polarOrder()
        Compares two points by polar angle (between 0 and 2π) with respect to this point.
        返回:
        the comparator
      • atan2Order

        public java.util.Comparator<Point2D> atan2Order()
        Compares two points by atan2() angle (between –π and π) with respect to this point.
        返回:
        the comparator
      • distanceToOrder

        public java.util.Comparator<Point2D> distanceToOrder()
        Compares two points by distance to this point.
        返回:
        the comparator
      • equals

        public boolean equals​(java.lang.Object other)
        Compares this point to the specified point.
        覆盖:
        equals 在类中 java.lang.Object
        参数:
        other - the other point
        返回:
        true if this point equals other; false otherwise
      • toString

        public java.lang.String toString()
        Return a string representation of this point.
        覆盖:
        toString 在类中 java.lang.Object
        返回:
        a string representation of this point in the format (x, y)
      • hashCode

        public int hashCode()
        Returns an integer hash code for this point.
        覆盖:
        hashCode 在类中 java.lang.Object
        返回:
        an integer hash code for this point
      • draw

        public void draw()
        Plot this point using standard draw.
      • drawTo

        public void drawTo​(Point2D that)
        Plot a line from this point to that point using standard draw.
        参数:
        that - the other point
      • main

        public static void main​(java.lang.String[] args)
        Unit tests the point data type.
        参数:
        args - the command-line arguments