package matrix; // Homogenous Vector3D class to represent // homogenous 3D coordinates whose represented // cartesian coordinate is obtained // with the homogenize() function. // This is useful for 3D graphics transformations. class Vector3Dh extends Vector3D { public double w; public Vector3Dh(double x, double y, double z) { super(x, y, z); w = 1; //{{INIT_CONTROLS //}} } public Vector3Dh(double x, double y, double z, double w) { super(x, y, z); this.w = w; } public Vector3Dh(Vector3D v) { super(v.x(), v.y(), v.z()); w = 1; } // Get the homogenized Cartesian coordinate // by dividing x, y, and z by w. // If w = 0, PointInfinity exception is thrown. public Vector3D homogenized() throws PointAtInfinity { if (w == 0) throw new PointAtInfinity(this); return (Vector3D)this.mult(1/w); } // Get the 2D homogenized Vector -- for projecting // a 3D point onto a 2D plane. Vector2D homogenized2D() throws PointAtInfinity { if (w == 0) throw new PointAtInfinity(this); return new Vector2D(x()/w, y()/w); } // Get a homogenized Cartesian coordinate // by dividing x and y (leaving z alone) by w. // This is useful for perspective transformations // where x and y are homogenized to get a screen // coordinate and z is retained so the distance // is still known. // If w = 0, PointAtInfinity exception is thrown. Vector3D xyHomogenized() throws PointAtInfinity { if (w == 0) throw new PointAtInfinity(this); return new Vector3D(x()/w, y()/w, z()); } //{{DECLARE_CONTROLS //}} }