Exemplo n.º 1
0
  private float checkCube(float x, float y, int cubeX, int cubeY, Point[] results) {
    int numPoints;
    random.setSeed(571 * cubeX + 23 * cubeY);
    switch (gridType) {
      case RANDOM:
      default:
        numPoints = probabilities[random.nextInt() & 0x1fff];
        break;
      case SQUARE:
        numPoints = 1;
        break;
      case HEXAGONAL:
        numPoints = 1;
        break;
      case OCTAGONAL:
        numPoints = 2;
        break;
      case TRIANGULAR:
        numPoints = 2;
        break;
    }
    for (int i = 0; i < numPoints; i++) {
      float px = 0, py = 0;
      float weight = 1.0f;
      switch (gridType) {
        case RANDOM:
          px = random.nextFloat();
          py = random.nextFloat();
          break;
        case SQUARE:
          px = py = 0.5f;
          if (randomness != 0) {
            px += randomness * (random.nextFloat() - 0.5);
            py += randomness * (random.nextFloat() - 0.5);
          }
          break;
        case HEXAGONAL:
          if ((cubeX & 1) == 0) {
            px = 0.75f;
            py = 0;
          } else {
            px = 0.75f;
            py = 0.5f;
          }
          if (randomness != 0) {
            px += randomness * Noise.noise2(271 * (cubeX + px), 271 * (cubeY + py));
            py += randomness * Noise.noise2(271 * (cubeX + px) + 89, 271 * (cubeY + py) + 137);
          }
          break;
        case OCTAGONAL:
          switch (i) {
            case 0:
              px = 0.207f;
              py = 0.207f;
              break;
            case 1:
              px = 0.707f;
              py = 0.707f;
              weight = 1.6f;
              break;
          }
          if (randomness != 0) {
            px += randomness * Noise.noise2(271 * (cubeX + px), 271 * (cubeY + py));
            py += randomness * Noise.noise2(271 * (cubeX + px) + 89, 271 * (cubeY + py) + 137);
          }
          break;
        case TRIANGULAR:
          if ((cubeY & 1) == 0) {
            if (i == 0) {
              px = 0.25f;
              py = 0.35f;
            } else {
              px = 0.75f;
              py = 0.65f;
            }
          } else {
            if (i == 0) {
              px = 0.75f;
              py = 0.35f;
            } else {
              px = 0.25f;
              py = 0.65f;
            }
          }
          if (randomness != 0) {
            px += randomness * Noise.noise2(271 * (cubeX + px), 271 * (cubeY + py));
            py += randomness * Noise.noise2(271 * (cubeX + px) + 89, 271 * (cubeY + py) + 137);
          }
          break;
      }
      float dx = (float) Math.abs(x - px);
      float dy = (float) Math.abs(y - py);
      float d;
      dx *= weight;
      dy *= weight;
      if (distancePower == 1.0f) d = dx + dy;
      else if (distancePower == 2.0f) d = (float) Math.sqrt(dx * dx + dy * dy);
      else
        d =
            (float)
                Math.pow(
                    (float) Math.pow(dx, distancePower) + (float) Math.pow(dy, distancePower),
                    1 / distancePower);

      // Insertion sort the long way round to speed it up a bit
      if (d < results[0].distance) {
        Point p = results[2];
        results[2] = results[1];
        results[1] = results[0];
        results[0] = p;
        p.distance = d;
        p.dx = dx;
        p.dy = dy;
        p.x = cubeX + px;
        p.y = cubeY + py;
      } else if (d < results[1].distance) {
        Point p = results[2];
        results[2] = results[1];
        results[1] = p;
        p.distance = d;
        p.dx = dx;
        p.dy = dy;
        p.x = cubeX + px;
        p.y = cubeY + py;
      } else if (d < results[2].distance) {
        Point p = results[2];
        p.distance = d;
        p.dx = dx;
        p.dy = dy;
        p.x = cubeX + px;
        p.y = cubeY + py;
      }
    }
    return results[2].distance;
  }
 private int displacementMap(int x, int y) {
   return PixelUtils.clamp((int) (127 * (1 + Noise.noise2(x / xScale, y / xScale))));
 }