Package drawit

Class IntPoint

java.lang.Object
drawit.IntPoint

public class IntPoint
extends Object
An immutable abstraction for a point in the two-dimensional plane with int coordinates.
Immutable
Any two calls of any zero-argument getter of this class on the same target object return equal values. (A getter is a method whose name starts with "get" or "is" followed by a capital letter.)
  • Constructor Summary

    Constructors 
    Constructor Description
    IntPoint​(int x, int y)
    Initializes this point with the given coordinates.
  • Method Summary

    Modifier and Type Method Description
    DoublePoint asDoublePoint()
    Returns a DoublePoint object that represents the same 2D point represented by this IntPoint object.
    boolean equals​(IntPoint other)
    Returns true if this point has the same coordinates as the given point; returns false otherwise.
    int getX()
    Returns this point's X coordinate.
    int getY()
    Returns this point's Y coordinate.
    boolean isOnLineSegment​(IntPoint b, IntPoint c)
    Returns true iff this point is on open line segment bc.
    static boolean lineSegmentsIntersect​(IntPoint a, IntPoint b, IntPoint c, IntPoint d)
    Returns true iff the open line segment ab intersects the open line segment cd.
    IntVector minus​(IntPoint other)
    Returns an IntVector object representing the displacement from other to this.
    IntPoint plus​(IntVector vector)
    Returns an IntPoint object representing the point obtained by displacing this point by the given vector.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • IntPoint

      public IntPoint​(int x, int y)
      Initializes this point with the given coordinates.
      Mutates:
      this
      Postconditions:
      getX() == x
      getY() == y
  • Method Details

    • getX

      public int getX()
      Returns this point's X coordinate.
    • getY

      public int getY()
      Returns this point's Y coordinate.
    • equals

      public boolean equals​(IntPoint other)
      Returns true if this point has the same coordinates as the given point; returns false otherwise.
      Preconditions:
      other != null
      Postconditions:
      result == (this.getX() == other.getX() && this.getY() == other.getY())
    • minus

      public IntVector minus​(IntPoint other)
      Returns an IntVector object representing the displacement from other to this.
      Preconditions:
      other != null
      Postconditions:
      result != null
      result.getX() == this.getX() - other.getX()
      result.getY() == this.getY() - other.getY()
    • isOnLineSegment

      public boolean isOnLineSegment​(IntPoint b, IntPoint c)
      Returns true iff this point is on open line segment bc. An open line segment does not include its endpoints.

      Implementation hints: Call this point a. First check if ba is collinear with bc. If not, return false. Then check that the dot product of ba and bc is between zero and the dot product of bc and bc.

      Preconditions:
      b != null
      0 <= b.getX() && b.getX() <= 10000 && 0 <= b.getY() && b.getY() <= 10000
      c != null
      0 <= c.getX() && c.getX() <= 10000 && 0 <= c.getY() && c.getY() <= 10000
      Postconditions:
      result == (
        this.minus(b).isCollinearWith(c.minus(b)) &&
        0 < this.minus(b).dotProduct(c.minus(b)) &&
        this.minus(b).dotProduct(c.minus(b)) < c.minus(b).dotProduct(c.minus(b))
      )
    • asDoublePoint

      public DoublePoint asDoublePoint()
      Returns a DoublePoint object that represents the same 2D point represented by this IntPoint object.
      Postconditions:
      result != null
      result.getX() == this.getX()
      result.getY() == this.getY()
    • plus

      public IntPoint plus​(IntVector vector)
      Returns an IntPoint object representing the point obtained by displacing this point by the given vector.
      Preconditions:
      vector != null
      Postconditions:
      result != null
      result.getX() == this.getX() + vector.getX()
      result.getY() == this.getY() + vector.getY()
    • lineSegmentsIntersect

      public static boolean lineSegmentsIntersect​(IntPoint a, IntPoint b, IntPoint c, IntPoint d)
      Returns true iff the open line segment ab intersects the open line segment cd.

      Implementation Hints: Assume the precondition holds. Then ab intersects cd if and only if ab straddles the carrier of cd and cd straddles the carrier of ab. Two points straddle a line if they are on opposite sides of the line.

      Specifically, cd straddles the carrier of ab iff (the signum of the cross product of ac and ab) times (the signum of the cross product of ad and ab) is negative.

      The signum of a number x is -1 if x is negative, 0 if x is zero, and 1 otherwise. See Math.signum(double).

      Preconditions:
      a != null
      b != null
      c != null
      d != null
      The line segments have at most one point in common.