/** * Specifies the angle of the texture. * * @param angle the angle of the texture. * @angle * @see #getAngle */ public void setAngle(float angle) { this.angle = angle; float cos = (float) Math.cos(angle); float sin = (float) Math.sin(angle); m00 = cos; m01 = sin; m10 = -sin; m11 = cos; }
public CellularFilter() { results = new Point[3]; for (int j = 0; j < results.length; j++) results[j] = new Point(); if (probabilities == null) { probabilities = new byte[8192]; float factorial = 1; float total = 0; float mean = 2.5f; for (int i = 0; i < 10; i++) { if (i > 1) factorial *= i; float probability = (float) Math.pow(mean, i) * (float) Math.exp(-mean) / factorial; int start = (int) (total * 8192); total += probability; int end = (int) (total * 8192); for (int j = start; j < end; j++) probabilities[j] = (byte) i; } } }
public float evaluate(float x, float y) { for (int j = 0; j < results.length; j++) results[j].distance = Float.POSITIVE_INFINITY; int ix = (int) x; int iy = (int) y; float fx = x - ix; float fy = y - iy; float d = checkCube(fx, fy, ix, iy, results); if (d > fy) d = checkCube(fx, fy + 1, ix, iy - 1, results); if (d > 1 - fy) d = checkCube(fx, fy - 1, ix, iy + 1, results); if (d > fx) { checkCube(fx + 1, fy, ix - 1, iy, results); if (d > fy) d = checkCube(fx + 1, fy + 1, ix - 1, iy - 1, results); if (d > 1 - fy) d = checkCube(fx + 1, fy - 1, ix - 1, iy + 1, results); } if (d > 1 - fx) { d = checkCube(fx - 1, fy, ix + 1, iy, results); if (d > fy) d = checkCube(fx - 1, fy + 1, ix + 1, iy - 1, results); if (d > 1 - fy) d = checkCube(fx - 1, fy - 1, ix + 1, iy + 1, results); } float t = 0; for (int i = 0; i < 3; i++) t += coefficients[i] * results[i].distance; if (angleCoefficient != 0) { float angle = (float) Math.atan2(y - results[0].y, x - results[0].x); if (angle < 0) angle += 2 * (float) Math.PI; angle /= 4 * (float) Math.PI; t += angleCoefficient * angle; } if (gradientCoefficient != 0) { float a = 1 / (results[0].dy + results[0].dx); t += gradientCoefficient * a; } return t; }
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; }