Class OrthoLine

java.lang.Object
squidpony.squidmath.OrthoLine

public class OrthoLine
extends Object
A simple line-drawing algorithm that only takes orthogonal steps; may be useful for LOS in games that use Manhattan distances for measurements. Algorithm is from http://www.redblobgames.com/grids/line-drawing.html#stepping , thanks Amit! Created by Tommy Ettinger on 1/10/2016.
  • Constructor Details

  • Method Details

    • line

      public static ArrayList<Coord> line​(int startX, int startY, int endX, int endY)
      Draws a line from (startX, startY) to (endX, endY) using only N/S/E/W movement. Returns a List of Coord in order.
      Parameters:
      startX - x of starting point
      startY - y of starting point
      endX - x of ending point
      endY - y of ending point
      Returns:
      List of Coord, including (startX, startY) and (endX, endY) and all points walked between
    • line

      public static ArrayList<Coord> line​(Coord start, Coord end)
      Draws a line from start to end using only N/S/E/W movement. Returns a List of Coord in order.
      Parameters:
      start - starting point
      end - ending point
      Returns:
      List of Coord, including start and end and all points walked between
    • line_

      public static Coord[] line_​(int startX, int startY, int endX, int endY)
      Draws a line from (startX, startY) to (endX, endY) using only N/S/E/W movement. Returns an array of Coord in order.
      Parameters:
      startX - x of starting point
      startY - y of starting point
      endX - x of ending point
      endY - y of ending point
      Returns:
      array of Coord, including (startX, startY) and (endX, endY) and all points walked between
    • line_

      public static Coord[] line_​(Coord start, Coord end)
      Draws a line from start to end using only N/S/E/W movement. Returns a List of Coord in order.
      Parameters:
      start - starting point
      end - ending point
      Returns:
      List of Coord, including start and end and all points walked between
    • lineChars

      public static char[] lineChars​(Coord[] line)
      Given an array of Coord as produced by line_(Coord, Coord) or line_(int, int, int, int), this gets a char array of box-drawing characters that connect when drawn at the corresponding Coord positions in the given line. This can be useful for drawing highlight lines or showing what path something will take, as long as it only uses 4-way orthogonal connections between Coords. Any connections that require a diagonal will not be handled by this method (returning a straight line without much accuracy), and any Coords that aren't adjacent will cause an IllegalStateException if this has to draw a line between them. If this method is called on the result of this class' line_() method, then it should always return a valid result; if it is called on a path made with some other method, such as from Bresenham.line2D_(Coord, Coord), then it shouldn't throw an exception but may produce a low-quality (disconnected visually) line.
      Parameters:
      line - a Coord array where each Coord is orthogonally adjacent to its neighbor(s) in the array; usually produced via line_(Coord, Coord) or line_(int, int, int, int)
      Returns:
      a char array of box-drawing chars that will connect when drawn at the same points as in line
    • lineChars

      public static char[] lineChars​(List<Coord> line)
      Given a List of Coord as produced by line(Coord, Coord) or line(int, int, int, int), this gets a char array of box-drawing characters that connect when drawn at the corresponding Coord positions in the given line. This can be useful for drawing highlight lines or showing what path something will take, as long as it only uses 4-way orthogonal connections between Coords. Any connections that require a diagonal will not be handled by this method (returning a straight line without much accuracy), and any Coords that aren't adjacent will cause an IllegalStateException if this has to draw a line between them. If this method is called on the result of this class' line() method, then it should always return a valid result; if it is called on a path made with some other method, such as from Bresenham.line2D(Coord, Coord), then it shouldn't throw an exception but may produce a low-quality (disconnected visually) line.
      Parameters:
      line - a List of Coord where each Coord is orthogonally adjacent to its neighbor(s) in the List; usually produced via line(Coord, Coord) or line(int, int, int, int)
      Returns:
      a char array of box-drawing chars that will connect when drawn at the same points as in line