Class Point2D

Object
Point2D
All Implemented Interfaces:
Point
Direct Known Subclasses:
Node2D, Vector2D

public class Point2D extends Object implements Point
Class representing a point in 2D Cartesian space.
Author:
Christoph Hauert
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    double
    The x-coordinate of the point.
    double
    The y-coordinate of the point.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new 2D point with coordinates (0,0).
    Point2D(double x, double y)
    Create a new 2D point with coordinates (x,y).
    Create a copy of the 2D point p.
  • Method Summary

    Modifier and Type
    Method
    Description
    double
    Calculate the distance from the origin (0,0): \(\sqrt{x^2+y^2}\).
    double
    Calculate the distance between the two 2D points p and q: \(\sqrt{(p.x-q.x)^2+(p.y-q.y)^2}\).
    double
    Calculate the distance squared from the origin (0,0): \(x^2+y^2\).
    double
    Calculate the distance squared between the two 2D points p and q: \((p.x-q.x)^2+(p.y-q.y)^2\).
    double
    Get the x-coordinate of this 2D point.
    double
    Get the y-coordinate of this 2D point.
    scale(double s)
    Scale the coordinates of this 2D point by a factor s to (s*x,s*y).
    scale(double sx, double sy)
    Scale the coordinates of this 2D point by scalar factors sx and sy, respectively, to (sx*x, sy*y).
    set(double x, double y)
    Set x- and y-coordinates of the point.
    Set x- and y-coordinates of the point to those of the 2D point p.
    void
    setLocation(double x, double y)
    Compatibility method to cover Point2D.setLocation(double, double).
    void
    Compatibility method to cover Point2D.setLocation(java.awt.geom.Point2D).
    setX(double x)
    Set the x-coordinate of this 2D point.
    setY(double y)
    Set the y-coordinate of this 2D point.
    shake(double quake)
    Randomly shake the position of this point by an amount scaled by quake.
    shift(double dx, double dy)
    Shift the 2D point by dx, and dy in the x-, y-coordinates, respectively, to (x+dx,y+dy).
    Shift the 2D point by p.x, and p.y in the x-, y-coordinates, respectively, to (x+p.x,y+p.y).
     

    Methods inherited from class Object

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

    • x

      public double x
      The x-coordinate of the point.
    • y

      public double y
      The y-coordinate of the point.
  • Constructor Details

    • Point2D

      public Point2D()
      Create a new 2D point with coordinates (0,0).
    • Point2D

      public Point2D(Point2D p)
      Create a copy of the 2D point p.
      Parameters:
      p - the 2D point to copy
    • Point2D

      public Point2D(double x, double y)
      Create a new 2D point with coordinates (x,y).
      Parameters:
      x - the x coordinate
      y - the y coordinate
  • Method Details

    • set

      public Point2D set(double x, double y)
      Set x- and y-coordinates of the point.
      Parameters:
      x - the x-coordinate
      y - the y-coordinate
      Returns:
      this point
    • set

      public Point2D set(Point2D p)
      Set x- and y-coordinates of the point to those of the 2D point p.
      Parameters:
      p - the point to copy coordinates from
      Returns:
      this point
    • setLocation

      public void setLocation(double x, double y)
      Compatibility method to cover Point2D.setLocation(double, double).
      Parameters:
      x - the x-coordinate
      y - the y-coordinate
      See Also:
    • setLocation

      public void setLocation(Point2D p)
      Compatibility method to cover Point2D.setLocation(java.awt.geom.Point2D).
      Parameters:
      p - the 2D point to copy the position from
      See Also:
    • shift

      public Point2D shift(Point2D p)
      Shift the 2D point by p.x, and p.y in the x-, y-coordinates, respectively, to (x+p.x,y+p.y).
      Parameters:
      p - the shift in the (x, y)-coordinates
      Returns:
      the shifted point
    • shift

      public Point2D shift(double dx, double dy)
      Shift the 2D point by dx, and dy in the x-, y-coordinates, respectively, to (x+dx,y+dy).
      Parameters:
      dx - the shift in the x-coordinate
      dy - the shift in the y-coordinate
      Returns:
      the shifted point
    • scale

      public Point2D scale(double s)
      Scale the coordinates of this 2D point by a factor s to (s*x,s*y).
      Parameters:
      s - the scaling factor
      Returns:
      the scaled point
    • scale

      public Point2D scale(double sx, double sy)
      Scale the coordinates of this 2D point by scalar factors sx and sy, respectively, to (sx*x, sy*y).
      Parameters:
      sx - the scaling of the x-coordinate
      sy - the scaling of the y-coordinate
      Returns:
      the scaled point
    • shake

      public Point2D shake(double quake)
      Description copied from interface: Point
      Randomly shake the position of this point by an amount scaled by quake.
      Specified by:
      shake in interface Point
      Parameters:
      quake - the amount to shake the point
      Returns:
      this point to enable chains of manipulations
    • getX

      public double getX()
      Get the x-coordinate of this 2D point.
      Returns:
      the x-coordinate
    • setX

      public Point2D setX(double x)
      Set the x-coordinate of this 2D point.
      Parameters:
      x - the new x-coordinate
      Returns:
      this point
    • getY

      public double getY()
      Get the y-coordinate of this 2D point.
      Returns:
      the y-coordinate
    • setY

      public Point2D setY(double y)
      Set the y-coordinate of this 2D point.
      Parameters:
      y - the new y-coordinate
      Returns:
      this point
    • distance

      public double distance()
      Calculate the distance from the origin (0,0): \(\sqrt{x^2+y^2}\).

      For computational efficiency the fairly expensive square-roots calculations should be avoided whenever possible.

      Returns:
      the distance
      See Also:
    • distance2

      public double distance2()
      Calculate the distance squared from the origin (0,0): \(x^2+y^2\).
      Returns:
      the squared distance
    • distance

      public double distance(Point2D q)
      Calculate the distance between the two 2D points p and q: \(\sqrt{(p.x-q.x)^2+(p.y-q.y)^2}\).

      For computational efficiency the fairly expensive square-roots calculations should be avoided whenever possible.

      Parameters:
      q - the point to calculate the distance to
      Returns:
      the distance between the two points
      See Also:
    • distance2

      public double distance2(Point2D q)
      Calculate the distance squared between the two 2D points p and q: \((p.x-q.x)^2+(p.y-q.y)^2\).
      Parameters:
      q - the point to calculate distance to
      Returns:
      the distance squared between the two points
    • toString

      public String toString()
      Overrides:
      toString in class Object