Package drawit

Class PointArrays

java.lang.Object
drawit.PointArrays

public class PointArrays
extends Object
Declares a number of methods useful for working with arrays of IntPoint objects.
  • Method Summary

    Modifier and Type Method Description
    static String checkDefinesProperPolygon​(IntPoint[] points)
    Returns null if the given array of points defines a proper polygon; otherwise, returns a string describing why it does not.
    static IntPoint[] copy​(IntPoint[] points)
    Returns a new array with the same contents as the given array.
    static IntPoint[] insert​(IntPoint[] points, int index, IntPoint point)
    Returns a new array whose elements are the elements of the given array with the given point inserted at the given index.
    static IntPoint[] remove​(IntPoint[] points, int index)
    Returns a new array whose elements are the elements of the given array with the element at the given index removed.
    static IntPoint[] scale​(IntPoint[] points, IntPoint origin, double xFactor, double yFactor)
    Returns a new array whose elements are the elements of the given array, scaled by the given X and Y factors.
    static IntPoint[] translate​(IntPoint[] points, IntVector delta)
    Returns a new array whose elements are the elements of the given array, translated along the given vector.
    static IntPoint[] update​(IntPoint[] points, int index, IntPoint value)
    Returns a new array whose elements are the elements of the given array with the element at the given index replaced by the given point.

    Methods inherited from class java.lang.Object

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

    • checkDefinesProperPolygon

      public static String checkDefinesProperPolygon​(IntPoint[] points)
      Returns null if the given array of points defines a proper polygon; otherwise, returns a string describing why it does not.

      We interpret an array of N points as the polygon whose vertices are the given points and whose edges are the open line segments between point I and point (I + 1) % N, for I = 0, ..., N - 1.

      A proper polygon is one where no two vertices coincide and no vertex is on any edge and no two edges intersect.

      If there are exactly two points, the polygon is not proper. Notice that if there are more than two points and no two vertices coincide and no vertex is on any edge, then no two edges intersect at more than one point.

      Preconditions:
      points != null
      Arrays.stream(points).allMatch(p -> p != null)
      Inspects:
      points
      Mutates:
      nothing
      Postconditions:
      (result == null) == (
          points.length != 2 &&
          IntStream.range(0, points.length).allMatch(i ->
              IntStream.range(0, points.length).allMatch(j -> i == j || !points[i].equals(points[j]))) &&
          IntStream.range(0, points.length).allMatch(i ->
              IntStream.range(0, points.length).allMatch(j -> !points[i].isOnLineSegment(points[j], points[(j + 1) % points.length]))) &&
          IntStream.range(0, points.length).allMatch(i ->
              IntStream.range(0, points.length).allMatch(j -> i == j ||
                  !IntPoint.lineSegmentsIntersect(points[i], points[(i + 1) % points.length], points[j], points[(j + 1) % points.length])))
      )
    • copy

      public static IntPoint[] copy​(IntPoint[] points)
      Returns a new array with the same contents as the given array.
      Preconditions:
      points != null
      Arrays.stream(points).allMatch(p -> p != null)
      Inspects:
      points
      Mutates:
      nothing
      Creates:
      result
      Postconditions:
      result != null
      result.length == points.length
      IntStream.range(0, points.length).allMatch(i -> result[i].equals(points[i]))
    • insert

      public static IntPoint[] insert​(IntPoint[] points, int index, IntPoint point)
      Returns a new array whose elements are the elements of the given array with the given point inserted at the given index.
      Preconditions:
      points != null
      Arrays.stream(points).allMatch(p -> p != null)
      0 <= index && index <= points.length
      point != null
      Inspects:
      points
      Mutates:
      nothing
      Creates:
      result
      Postconditions:
      result != null
      result.length == points.length + 1
      IntStream.range(0, index).allMatch(i -> result[i].equals(points[i]))
      result[index].equals(point)
      IntStream.range(index, points.length).allMatch(i -> result[i + 1].equals(points[i]))
    • remove

      public static IntPoint[] remove​(IntPoint[] points, int index)
      Returns a new array whose elements are the elements of the given array with the element at the given index removed.
      Preconditions:
      points != null
      Arrays.stream(points).allMatch(p -> p != null)
      0 <= index && index < points.length
      Inspects:
      points
      Mutates:
      nothing
      Creates:
      result
      Postconditions:
      result != null
      result.length == points.length - 1
      IntStream.range(0, index).allMatch(i -> result[i].equals(points[i]))
      IntStream.range(index + 1, points.length).allMatch(i -> result[i - 1].equals(points[i]))
    • update

      public static IntPoint[] update​(IntPoint[] points, int index, IntPoint value)
      Returns a new array whose elements are the elements of the given array with the element at the given index replaced by the given point.
      Preconditions:
      points != null
      0 <= index && index < points.length
      value != null
      Inspects:
      points
      Mutates:
      nothing
      Creates:
      result
      Postconditions:
      result != null
      result.length == points.length
      IntStream.range(0, points.length).allMatch(i -> result[i].equals(i == index ? value : points[i]))
    • translate

      public static IntPoint[] translate​(IntPoint[] points, IntVector delta)
      Returns a new array whose elements are the elements of the given array, translated along the given vector.
    • scale

      public static IntPoint[] scale​(IntPoint[] points, IntPoint origin, double xFactor, double yFactor)
      Returns a new array whose elements are the elements of the given array, scaled by the given X and Y factors. The scaling is done with respect to the given origin. For example: scaling point (20, 20) with respect to origin (10, 10) by xFactor == 30 and yFactor == 50 results in point (310, 510).