Marching Squares Implementation

This is an entirely straightforward implementation of the classic marching squares algorithm in Java (see for an introduction to the algorithm).

It consists of the following three classes, the source for which I am placing in the public domain:

Usage Example

The code anticipates that data will have been pre-thresholded into zero and non-zero byte values. Given a matrix of such values (supplied as a byte array) the code can find perimeters between regions of zero and non-zero values.

byte[] example = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }; MarchingSquares ms = new MarchingSquares(5,6, example); Path path = ms.identifyPerimeter(); //the path returned will be [S, S, E, S, E, N, N, N, W, W] //the origin of the path being the point (2,-2)

The diagram below shows exhibits the coordinate system used by the algorithm.

A diagram highlighting the coordinate system used in the implementation.

For more information see the comments in the supplied code.