Package selector

Class PolyLine

java.lang.Object
selector.PolyLine

public class PolyLine extends Object
Represents an immutable path made up of straight line segments. While it is intended to be immutable, its interface sacrifices some encapsulation in order to be used efficiently for AWT graphics (that is, it does not create defensive copies of arrays), so clients must not modify the contents of arrays returned by instances of this class.
  • Constructor Summary

    Constructors
    Constructor
    Description
    PolyLine(int[] xs, int[] ys)
    Create a poly-line whose points have x-coordinates `xs` and y-coordinates `ys`.
    PolyLine(Point start, Point end)
    Create a straight line segment starting at `start` and ending at `end`.
  • Method Summary

    Modifier and Type
    Method
    Description
    end()
    Return the last (ending) point along this poly-line.
    boolean
    equals(Object other)
     
    int
     
    static Polygon
    Return an AWT Polygon enclosed by the sequence of poly-lines in `segments`.
    int
    Return the number of points along this poly-line, including both endpoints.
    Return the first (starting) point along this poly-line.
     
    int[]
    xs()
    Return the sequence of the x coordinates of the points along this poly-line, in start-to-end order.
    int[]
    ys()
    Return the sequence of the y coordinates of the points along this poly-line, in start-to-end order.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • PolyLine

      public PolyLine(Point start, Point end)
      Create a straight line segment starting at `start` and ending at `end`. Does not store any references to its (mutable) arguments.
    • PolyLine

      public PolyLine(int[] xs, int[] ys)
      Create a poly-line whose points have x-coordinates `xs` and y-coordinates `ys`. Rep exposure: the new object takes ownership of the argument arrays (it does not make defensive copies), so clients must not mutate the contents of these arguments after construction. Requires that `xs` and `ys` have the same length no less than 2.
  • Method Details

    • xs

      public int[] xs()
      Return the sequence of the x coordinates of the points along this poly-line, in start-to-end order. Rep exposure: clients must not mutate the contents of the returned array.
    • ys

      public int[] ys()
      Return the sequence of the y coordinates of the points along this poly-line, in start-to-end order. Rep exposure: clients must not mutate the contents of the returned array.
    • size

      public int size()
      Return the number of points along this poly-line, including both endpoints. Will be at least 2. The number of straight-line segments is therefore `size() - 1`.
    • start

      public Point start()
      Return the first (starting) point along this poly-line.
    • end

      public Point end()
      Return the last (ending) point along this poly-line.
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • makePolygon

      public static Polygon makePolygon(List<PolyLine> segments)
      Return an AWT Polygon enclosed by the sequence of poly-lines in `segments`. The end of the last segment in the sequence will be joined to the start of the first segment in the sequence. Any duplicate points where segments join are omitted. Requires `segments` is non-empty.
    • toString

      public String toString()
      Overrides:
      toString in class Object