Created
March 12, 2014 17:30
-
-
Save MitMaro/9511922 to your computer and use it in GitHub Desktop.
A vector class in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package ca.mitmaro.cs4751.a03; | |
| public class Vector { | |
| public static class Vector2D extends Vector { | |
| public Vector2D(Vector v) { | |
| this(v.components[Vector.X], v.components[Vector.Y]); | |
| } | |
| public Vector2D(double x, double y) { | |
| super(new double[]{x, y}); | |
| } | |
| public Vector2D() { | |
| super(2); | |
| } | |
| public double getX() { | |
| return this.components[Vector.X]; | |
| } | |
| public double getY() { | |
| return this.components[Vector.Y]; | |
| } | |
| public void setX(double value) { | |
| this.components[Vector.X] = value; | |
| } | |
| public void setY(double value) { | |
| this.components[Vector.Y] = value; | |
| } | |
| public Vector2D getPerpendicular() { | |
| return new Vector2D(-this.getX(), this.getY()); | |
| } | |
| } | |
| public static class Vector3D extends Vector { | |
| public Vector3D(Vector v) { | |
| this(v.components[Vector.X], v.components[Vector.Y], v.components[Vector.Z]); | |
| } | |
| public Vector3D(double x, double y, double z) { | |
| super(new double[]{x, y, z}); | |
| } | |
| public Vector3D() { | |
| super(3); | |
| } | |
| public double getX() { | |
| return this.components[Vector.X]; | |
| } | |
| public double getY() { | |
| return this.components[Vector.Y]; | |
| } | |
| public double getZ() { | |
| return this.components[Vector.Z]; | |
| } | |
| public void setX(double value) { | |
| this.components[Vector.X] = value; | |
| } | |
| public void setY(double value) { | |
| this.components[Vector.Y] = value; | |
| } | |
| public void setZ(double value) { | |
| this.components[Vector.Z] = value; | |
| } | |
| public static Vector3D crossProduct(Vector3D a, Vector3D b) { | |
| return new Vector3D ( | |
| a.getY() * b.getZ() - a.getZ() * b.getY(), | |
| a.getZ() * b.getX() - a.getX() * b.getZ(), | |
| a.getX() * b.getY() - a.getY() * b.getX() | |
| ); | |
| } | |
| } | |
| public static final int X = 0; | |
| public static final int Y = 1; | |
| public static final int Z = 2; | |
| protected final double[] components; | |
| public Vector(double[] components) { | |
| this.components = components; | |
| } | |
| public Vector(int numberComponents) { | |
| this.components = new double[numberComponents]; | |
| } | |
| public void mulitply(double d) { | |
| for (int i = 0; i < this.components.length; i++) { | |
| this.components[i] = this.components[i] * d; | |
| } | |
| } | |
| public void divide(double d) { | |
| for (int i = 0; i < this.components.length; i++) { | |
| this.components[i] = this.components[i] / d; | |
| } | |
| } | |
| public void setComponent(int component, int value) { | |
| this.components[component] = value; | |
| } | |
| public void checkVectorDimensions(Vector v) { | |
| if (this.components.length != v.components.length) { | |
| throw new RuntimeException(Vector.class.getName() + " are of different size"); | |
| } | |
| } | |
| public double magnitude() { | |
| double value = 0; | |
| for (int i = 0; i < this.components.length; i++) { | |
| value += this.components[i] * this.components[i]; | |
| } | |
| return Math.sqrt(value); | |
| } | |
| public static Vector add(Vector a, Vector b) { | |
| a.checkVectorDimensions(b); | |
| double[] newComponents = new double[a.components.length]; | |
| for (int i = 0; i < newComponents.length; i++) { | |
| newComponents[i] = a.components[i] + b.components[i]; | |
| } | |
| return new Vector(newComponents); | |
| } | |
| public static Vector subtract(Vector a, Vector b) { | |
| a.checkVectorDimensions(b); | |
| double[] newComponents = new double[a.components.length]; | |
| for (int i = 0; i < newComponents.length; i++) { | |
| newComponents[i] = a.components[i] - b.components[i]; | |
| } | |
| return new Vector(newComponents); | |
| } | |
| public static double dotProduct(Vector a, Vector b) { | |
| double result = 0; | |
| a.checkVectorDimensions(b); | |
| for (int i = 0; i < a.components.length; i++) { | |
| result += a.components[i] * b.components[i]; | |
| } | |
| return result; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment