Class BitMatrix


  • public final class BitMatrix
    extends java.lang.Object

    Represents a 2D matrix of bits. In function arguments below, and throughout the common module, x is the column position, and y is the row position. The ordering is always x, y. The origin is at the top-left.

    Internally the bits are represented in a 1-D array of 32-bit ints. However, each row begins with a new int. This is done intentionally so that we can copy out a row into a BitArray very efficiently.

    The ordering of bits is row-major. Within each int, the least significant bits are used first, meaning they represent lower x values. This is compatible with BitArray's implementation.

    Since:
    5.0.2
    • Field Summary

      Fields 
      Modifier and Type Field Description
      int[] bits  
      int height  
      int rowSize  
      int width  
    • Constructor Summary

      Constructors 
      Constructor Description
      BitMatrix​(int dimension)  
      BitMatrix​(int width, int height)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clears all bits (sets to false).
      void flip​(int x, int y)
      Flips the given bit.
      boolean get​(int x, int y)
      Gets the requested bit, where true means black.
      int getDimension()
      This method is for compatibility with older code.
      int getHeight()  
      BitArray getRow​(int y, BitArray row)
      A fast method to retrieve one row of data from the matrix as a BitArray.
      int getWidth()  
      void set​(int x, int y)
      Sets the given bit to true.
      void setRegion​(int left, int top, int width, int height)
      Sets a square region of the bit matrix to true.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • width

        public final int width
      • height

        public final int height
      • rowSize

        public final int rowSize
      • bits

        public final int[] bits
    • Constructor Detail

      • BitMatrix

        public BitMatrix​(int dimension)
      • BitMatrix

        public BitMatrix​(int width,
                         int height)
    • Method Detail

      • get

        public boolean get​(int x,
                           int y)

        Gets the requested bit, where true means black.

        Parameters:
        x - The horizontal component (i.e. which column)
        y - The vertical component (i.e. which row)
        Returns:
        value of given bit in matrix
      • set

        public void set​(int x,
                        int y)

        Sets the given bit to true.

        Parameters:
        x - The horizontal component (i.e. which column)
        y - The vertical component (i.e. which row)
      • flip

        public void flip​(int x,
                         int y)

        Flips the given bit.

        Parameters:
        x - The horizontal component (i.e. which column)
        y - The vertical component (i.e. which row)
      • clear

        public void clear()
        Clears all bits (sets to false).
      • setRegion

        public void setRegion​(int left,
                              int top,
                              int width,
                              int height)

        Sets a square region of the bit matrix to true.

        Parameters:
        left - The horizontal position to begin at (inclusive)
        top - The vertical position to begin at (inclusive)
        width - The width of the region
        height - The height of the region
      • getRow

        public BitArray getRow​(int y,
                               BitArray row)
        A fast method to retrieve one row of data from the matrix as a BitArray.
        Parameters:
        y - The row to retrieve
        row - An optional caller-allocated BitArray, will be allocated if null or too small
        Returns:
        The resulting BitArray - this reference should always be used even when passing your own row
      • getWidth

        public int getWidth()
        Returns:
        The width of the matrix
      • getHeight

        public int getHeight()
        Returns:
        The height of the matrix
      • getDimension

        public int getDimension()
        This method is for compatibility with older code. It's only logical to call if the matrix is square, so I'm throwing if that's not the case.
        Returns:
        row/column dimension of this matrix
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object