Skip to content
This repository has been archived by the owner on Sep 27, 2020. It is now read-only.

Simple vector calculation #316

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions Java/VectorCalculations.java
Original file line number Diff line number Diff line change
@@ -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())));
}
}