From 9229750ad203384d66da0c12efedfce641c16b52 Mon Sep 17 00:00:00 2001 From: Abenezer Adane <32374905+PB020@users.noreply.github.com> Date: Tue, 8 Oct 2019 23:06:42 -0400 Subject: [PATCH] Simple vector calculation This programm contains methods related to simple vector calculations. --- Java/VectorCalculations.java | 114 +++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 Java/VectorCalculations.java diff --git a/Java/VectorCalculations.java b/Java/VectorCalculations.java new file mode 100644 index 0000000..3119475 --- /dev/null +++ b/Java/VectorCalculations.java @@ -0,0 +1,114 @@ +/** + * VectorCalculations.java + * This programm contains methods related to simple vector calculations. + * + * @author Abenezer Adane + */ + +public class VectorCalculations { + + public static class Vector3D { + private int x, y, z; + + public Vector3D(int x, int y, int z){ + this.x = x; + this.y = y; + this.z = z; + } + + public int getX(){ + return this.x; + } + + public void setX(int x){ + this.x = x; + } + + public int getY(){ + return this.y; + } + + public void setY(int y){ + this.y = y; + } + + public int getZ(){ + return this.z; + } + + public void setZ(int z){ + this.z = z; + } + + public String toString(){ + return "<" + getX() + ", " + getY() + ", " + getZ() + ">"; + } + } + + /* This is a 2-Dimentional Vector object */ + public static class Vector2D { + private int x, y; + + public Vector2D(int x, int y){ + this.x = x; + this.y = y; + } + + public int getX(){ + return this.x; + } + + public void setX(int x){ + this.x = x; + } + + public int getY(){ + return this.y; + } + + public void setY(int y){ + this.y = y; + } + + public String toString(){ + return "<" + getX() + ", " + getY() + ">"; + } + } + + /* This method calculates the sum of two 2D vectors */ + public static Vector2D addition2D(Vector2D a, Vector2D b) { + return new Vector2D((a.getX() + b.getX()), (a.getY() + b.getY())); + } + + /* This method calculates the difference of two 2D vectors */ + public static Vector2D subtraction2D(Vector2D a, Vector2D b) { + return new Vector2D((a.getX() - b.getX()), (a.getY() - b.getY())); + } + + /* This method calculates the sum of two 3D vectors */ + public static Vector3D addition3D(Vector3D a, Vector3D b) { + return new Vector3D((a.getX() + b.getX()), (a.getY() + b.getY()), (a.getZ() + b.getZ())); + } + + /* This method calculates the difference of two 3D vectors */ + public static Vector3D subtraction3D(Vector3D a, Vector3D b) { + return new Vector3D((a.getX() - b.getX()), (a.getY() - b.getY()), (a.getZ() - b.getZ())); + } + + /* This method calculates the dot product of two 2D vectors */ + public static int dotProduct2D(Vector2D a, Vector2D b){ + return ((a.getX() * b.getX()) + (a.getY() * b.getY())); + } + + /* This method calculates the dot product of two 3D vectors */ + public static int dotProduct3D(Vector3D a, Vector3D b){ + return ((a.getX() * b.getX()) + (a.getY() * b.getY()) + (a.getZ() * b.getZ())); + } + + /* This method returns the cross product of two vectors */ + 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()))); + } +}