singleCellular2 method

double singleCellular2(
  1. double x,
  2. double y
)

Implementation

double singleCellular2(double x, double y) {
  final xr = x.round();
  final yr = y.round();
  var distance = 999999.0;
  var xc = 0;
  var yc = 0;

  switch (cellularDistanceFunction) {
    case CellularDistanceFunction.euclidean:
      for (var xi = xr - 1; xi <= xr + 1; xi++) {
        for (var yi = yr - 1; yi <= yr + 1; yi++) {
          final vec = cell2d[hash2D(seed, xi, yi) & 255];

          final vecX = xi - x + vec.x;
          final vecY = yi - y + vec.y;
          final newDistance = vecX * vecX + vecY * vecY;

          if (newDistance < distance) {
            distance = newDistance;
            xc = xi;
            yc = yi;
          }
        }
      }
      break;
    case CellularDistanceFunction.manhattan:
      for (var xi = xr - 1; xi <= xr + 1; xi++) {
        for (var yi = yr - 1; yi <= yr + 1; yi++) {
          final vec = cell2d[hash2D(seed, xi, yi) & 255];

          final vecX = xi - x + vec.x;
          final vecY = yi - y + vec.y;
          final newDistance = vecX.abs() + vecY.abs();

          if (newDistance < distance) {
            distance = newDistance;
            xc = xi;
            yc = yi;
          }
        }
      }
      break;
    case CellularDistanceFunction.natural:
      for (var xi = xr - 1; xi <= xr + 1; xi++) {
        for (var yi = yr - 1; yi <= yr + 1; yi++) {
          final vec = cell2d[hash2D(seed, xi, yi) & 255];

          final vecX = xi - x + vec.x;
          final vecY = yi - y + vec.y;
          final newDistance =
              (vecX.abs() + vecY.abs()) + (vecX * vecX + vecY * vecY);

          if (newDistance < distance) {
            distance = newDistance;
            xc = xi;
            yc = yi;
          }
        }
      }
      break;
  }

  switch (cellularReturnType) {
    case CellularReturnType.cellValue:
      return valCoord2D(0, xc, yc);

    case CellularReturnType.distance:
      return distance - 1.0;
    default:
      return .0;
  }
}