Class WobblyLine

java.lang.Object
squidpony.squidmath.WobblyLine

public class WobblyLine
extends Object
A drunkard's-walk-like algorithm for line-drawing "wobbly" paths. This produces lines as ArrayList of Coord, where Coords that are adjacent in the ArrayList are guaranteed to be orthogonally adjacent, but the path as a whole is not guaranteed to have all unique Coords (that is, the line may cross over its previous path). If you don't want the line to cross itself, you can use TwistedLine, though the API is different.
The line() methods here use an IRNG (and will make their own if they don't take one as a parameter) to make a choice between orthogonal directions to travel in. Because they can go around the target instead of straight to it, they also need a width and height for the map so they don't wander over the edge. You can also pass a weight to one of the line() methods, which affects how straight the wobbly path will be (1.0 being just about perfectly straight, 0.5 being very chaotic, and less than 0.5 being almost unrecognizable as a path). Lower weights make the case where the path crosses itself more likely.
Based on Michael Patraw's C code, used for cave carving in his map generator. https://github.com/mpatraw/butterfly Created by Tommy Ettinger on 1/10/2016.
  • Method Summary

    Modifier and Type Method Description
    static ArrayList<Coord> line​(int startX, int startY, int endX, int endY, int width, int height)
    Draws a line from (startX, startY) to (endX, endY) using the Drunkard's Walk algorithm.
    static ArrayList<Coord> line​(int startX, int startY, int endX, int endY, int width, int height, double weight, IRNG rng)
    Draws a line from (startX, startY) to (endX, endY) using the Drunkard's Walk algorithm.
    static ArrayList<Coord> line​(Coord start, Coord end, int width, int height)
    Draws a line from start to end using the Drunkard's Walk algorithm.

    Methods inherited from class java.lang.Object

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

    • line

      public static ArrayList<Coord> line​(int startX, int startY, int endX, int endY, int width, int height)
      Draws a line from (startX, startY) to (endX, endY) using the Drunkard's Walk algorithm. Returns a List of Coord in order.
      Equivalent to calling line(startX, startY, endX, endY, width, height, 0.75, new RNG()) .
      Parameters:
      startX - x of starting point
      startY - y of starting point
      endX - x of ending point
      endY - y of ending point
      width - maximum map width
      height - maximum map height
      Returns:
      List of Coord, including (startX, startY) and (endX, endY) and all points walked between
    • line

      public static ArrayList<Coord> line​(int startX, int startY, int endX, int endY, int width, int height, double weight, IRNG rng)
      Draws a line from (startX, startY) to (endX, endY) using the Drunkard's Walk algorithm. 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
      width - maximum map width
      height - maximum map height
      weight - between 0.5 and 1.0, usually. 0.6 makes very random walks, 0.9 is almost a straight line.
      rng - the random number generator to use
      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, int width, int height)
      Draws a line from start to end using the Drunkard's Walk algorithm. Returns a List of Coord in order.
      Parameters:
      start - starting point
      end - ending point
      width - maximum map width
      height - maximum map height
      Returns:
      List of Coord, including start and end and all points walked between