May 4th, 2008
Releasing Java Vector Class
This semester, I took a class called “Computational Physics,” where we simulated various physical properties. For my final project, I wrote a simulation of the Solar System, and in the process, I wrote a “Vector” class for Java. It allows you to do 3 dimensional vector math operations pretty easily, like in the examples below:
// Create vector <1,2,3> Vector v1 = new Vector(1,2,3); // Unit Vector <0.27, 0.53, 0.80> Vector unit = v1.getUnitVector(); // Magnitude (3.74) double mag = v1.getMagnitude(); // Get each value double x = v1.getX(); // 1 double y = v1.getY(); // 2 double z = v1.getZ(); // 3 // Multiply v1 by scalar (<5,10,15>) Vector scalarM = v1.scalarMult(5); // Create vector <-4,-5,-6> Vector v2 = new Vector(-4,-5,6); // Add v1 and v2 (<-3,-3,-3>) Vector add = v1.add(v2); // Subtract v2 from v1 (<5, 7, 9>) Vector sub = v1.minus(v2); // Dot v1 and v2 (-32) Double dot = v1.dot(v2); // Cross v1 and v2 (<3,-6,3>) Vector cross = v1.cross(v2);
I’ve never really used Java before, so my code might not be the best, but it gets the job done. If you want to download the Vector class, here’s the link. If this comes in handy to anyone, I’d really appreciate it if you left a comment!

