import java.awt.*; import java.util.*; import java.lang.*; public class Poly extends Polygon implements Item { // Inherited: // // public int npoints; // public int xpoints[]; // public int ypoints[]; public int zpoints[]; private Color faceColor; private World world; public Poly() { npoints = 0; faceColor = Color.green; } public Poly(World w) { npoints = 0; faceColor = Color.green; world = w; } public Poly(long seed) { Random random = new Random(seed); faceColor = new Color(random.nextFloat(), random.nextFloat(), random.nextFloat()); addPoint((random.nextInt() % 200), // x: -200..200 (random.nextInt() % 150), // y: -150..150 (random.nextInt() % 50 - 50)); // z: -100..0 // No further than 50 away from previous point // in x, y, z; not positive in z addPoint(xpoints[0] + (random.nextInt() % 100), ypoints[0] + (random.nextInt() % 100), -Math.abs(zpoints[0] + ((random.nextInt() % 50) - 50))); addPoint(xpoints[1] + (random.nextInt() % 100), ypoints[1] + (random.nextInt() % 100), -Math.abs(zpoints[1] + ((random.nextInt() % 50) - 50))); } public Poly(long seed, World w) { Random random = new Random(seed); faceColor = new Color(random.nextFloat(), random.nextFloat(), random.nextFloat()); addPoint((random.nextInt() % 200), // x: -200..200 (random.nextInt() % 150), // y: -150..150 (random.nextInt() % 50 - 50)); // z: -100..0 // No further than 50 away from previous point // in x, y, z; not positive in z addPoint(xpoints[0] + (random.nextInt() % 100), ypoints[0] + (random.nextInt() % 100), -Math.abs(zpoints[0] + ((random.nextInt() % 50) - 50))); addPoint(xpoints[1] + (random.nextInt() % 100), ypoints[1] + (random.nextInt() % 100), -Math.abs(zpoints[1] + ((random.nextInt() % 50) - 50))); world = w; } public Poly(int[] xp, int[] yp, int[] zp, int n) { npoints = n; xpoints = new int[n]; ypoints = new int[n]; zpoints = new int[n]; for (int i=0; i max) max = zpoints[i]; } return (double) max; } public double zIntersectCalc(int x, int y, boolean VERBOSE) throws ItemException { // Note that this assumes that the points on the polygon // are in fact coplanar..! If they're not somebody screwed // up. Anyway it only uses the first three... if (npoints < 3) throw new ItemException("zIntersect: not coplanar"); double x0, y0, z0; double x1, y1, z1; double x2, y2, z2; double td; double m11, m21, m22; double a, b, c; double zret; int iteration = 0; x0 = (double) xpoints[0]; y0 = (double) ypoints[0]; z0 = (double) zpoints[0]; x1 = (double) xpoints[1]; y1 = (double) ypoints[1]; z1 = (double) zpoints[1]; x2 = (double) xpoints[2]; y2 = (double) ypoints[2]; z2 = (double) zpoints[2]; m11 = x1 / x0; while(iteration < 6 && (x0 == 0 || y1 == 0 || m11 == 1.0 || y1/y0 == m11)) { if (iteration++ % 2 == 0) { td = x0; x0 = x1; x1 = td; td = y0; y0 = y1; y1 = td; td = z0; z0 = z1; z1 = td; } else { td = x2; x2 = x1; x1 = td; td = y2; y2 = y1; y1 = td; td = z2; z2 = z1; z1 = td; } m11 = x1 / x0; } try { m11 = x1 / x0; m21 = x2 / x0; m22 = (y2 - m21 * y0) / (y1 - m11 * y0); c = (z2 - m21*z0 - m22*z1 + m22*m11*z0) / (1 - m11 - m22 + m11*m22); b = (z1 - m11*z0 - c + c*m11) / (y1 - m11*y0); a = (z0 - b*y0 - c) / x0; zret = (a * (double)x + b * (double)y + c); if (VERBOSE && x % 50 == 0 && y % 50 == 0) { System.out.println("x0 = " + x0 + ", y0 = " + y0 + ", z0 = " + z0); System.out.println("x1 = " + x1 + ", y1 = " + y1 + ", z1 = " + z1); System.out.println("x2 = " + x2 + ", y2 = " + y2 + ", z2 = " + z2); System.out.println("m11 = " + m11 + ", m21 = " + m21 + ", m22 = " + m22); System.out.println("Equation: " + a + "x + " + b + "y + " + c + " = z"); System.out.println("x = " + x + ", y = " + y + " --> z = " + zret); if (iteration == 6) { System.out.println("----------- :( -------------------"); throw new ItemException("zIntersect: singular matrix"); } System.out.println("----------------------------------"); } } catch (ArithmeticException e) { throw new ItemException("zIntersect: " + e.getMessage()); } return zret; } public double zIntersect(int x, int y) throws ItemException { return zIntersectCalc(x, y, false); } public double zIntersectVerbose(int x, int y) throws ItemException { return zIntersectCalc(x, y, true); } public void translate(int deltax, int deltay, int deltaz) { for (int i=0; i