// Generation of fractle mountains - triangle floor plan // // Author Matthew Caryl // Created 6.4.97 // // Although under copywrite to the author (Matthew Caryl) this code can be copied and modified for non-commercial // purposes as long as any derivatives contain this condition. package Mountains; import java.awt.Color; import ADT.Random; import ADT.Vector; import ADT.Entity; final class Triangle extends FloorPlan { private static final float D000 = 0f; private static final float D060 = 2f * (float) Math.PI / 3f; private static final float D120 = 4f * (float) Math.PI / 3f; private static final float D240 = 8f * (float) Math.PI / 3f; private float variation; private int size; private float[][] xs; // triangle vertices stored in triangular array private float[][] ys; private float[][] zs; public Triangle(Random R, float X, float Y, float Z, float L, float S) { variation = L * S; size = 1; int v = (int) (variation); float h = L / 2f / (float) Math.sin(D060); xs = new float[2][]; ys = new float[2][]; zs = new float[2][]; xs[0] = new float[1]; ys[0] = new float[1]; zs[0] = new float[1]; xs[0][0] = X - (float) Math.sin(D000) * h; ys[0][0] = Y - (float) Math.cos(D000) * h; zs[0][0] = Z + R.range(v) - v / 2; xs[1] = new float[2]; ys[1] = new float[2]; zs[1] = new float[2]; xs[1][0] = X - (float) Math.sin(D120) * h; ys[1][0] = Y - (float) Math.cos(D120) * h; zs[1][0] = Z + R.range(v) - v / 2; xs[1][1] = X - (float) Math.sin(D240) * h; ys[1][1] = Y - (float) Math.cos(D240) * h; zs[1][1] = Z + R.range(v) - v / 2; } public void recurse(Random R) { variation /= 2f; size *= 4; int v = (int) (variation); float[][] XS = new float[2 * xs.length - 1][]; float[][] YS = new float[2 * ys.length - 1][]; float[][] ZS = new float[2 * zs.length - 1][]; // work with old horizontal lines first for (int i = xs.length - 1; i >= 0; i--) { XS[2 * i] = new float[2 * i + 1]; YS[2 * i] = new float[2 * i + 1]; ZS[2 * i] = new float[2 * i + 1]; // duplicating vertices for (int j = xs[i].length - 1; j >= 0; j--) { XS[2 * i][2 * j] = xs[i][j]; YS[2 * i][2 * j] = ys[i][j]; ZS[2 * i][2 * j] = zs[i][j]; } // and making new ones inbetween for (int j = xs[i].length - 2; j >= 0; j--) { XS[2 * i][2 * j + 1] = (xs[i][j] + xs[i][j + 1]) / 2f; YS[2 * i][2 * j + 1] = (ys[i][j] + ys[i][j + 1]) / 2f; ZS[2 * i][2 * j + 1] = (zs[i][j] + zs[i][j + 1]) / 2f + R.range(v) - v / 2; } } // then add new horizontal lines for (int i = xs.length - 2; i >= 0; i--) { XS[2 * i + 1] = new float[2 * i + 2]; YS[2 * i + 1] = new float[2 * i + 2]; ZS[2 * i + 1] = new float[2 * i + 2]; for (int j = xs[i].length - 1; j >= 0; j--) { XS[2 * i + 1][2 * j] = (xs[i][j] + xs[i + 1][j]) / 2f; YS[2 * i + 1][2 * j] = (ys[i][j] + ys[i + 1][j]) / 2f; ZS[2 * i + 1][2 * j] = (zs[i][j] + zs[i + 1][j]) / 2f + R.range(v) - v / 2; XS[2 * i + 1][2 * j + 1] = (xs[i][j] + xs[i + 1][j + 1]) / 2f; YS[2 * i + 1][2 * j + 1] = (ys[i][j] + ys[i + 1][j + 1]) / 2f; ZS[2 * i + 1][2 * j + 1] = (zs[i][j] + zs[i + 1][j + 1]) / 2f + R.range(v) - v / 2; } } xs = XS; ys = YS; zs = ZS; } public int getSize() { return size; } public Entity getObject() { Vector[] vs = new Vector[xs.length * (xs.length + 1) / 2]; int[] ts = new int[3 * (xs.length - 1) * (xs.length - 1)]; Color[] cs = new Color[(xs.length - 1) * (xs.length - 1)]; // transfer all vertices for (int i = xs.length - 1, o = vs.length - 1; i >= 0; i--) for (int j = xs[i].length - 1; j >= 0; j--, o--) vs[o] = new Vector(xs[i][j], ys[i][j], zs[i][j]); // work from bottom-left triangle to top-right for (int i = xs.length - 2, o = 0; i >= 0; i--) { for (int j = xs[i].length - 1; j >= 0; j--, o++) { ts[3 * o + 0] = i * (i + 1) / 2 + j; ts[3 * o + 1] = (i + 1) * (i + 2) / 2 + j; ts[3 * o + 2] = (i + 1) * (i + 2) / 2 + 1 + j; cs[o] = Color.white; } for (int j = xs[i].length - 2; j >= 0; j--, o++) { ts[3 * o + 0] = i * (i + 1) / 2 + j; ts[3 * o + 1] = (i + 1) * (i + 2) / 2 + 1 + j; ts[3 * o + 2] = i * (i + 1) / 2 + 1 + j; cs[o] = Color.white; } } return new Entity(vs, ts, cs, null); } }