Vector2D.java 1.01 KB
package osp.utils;

import java.awt.geom.Point2D;

/*
 * Class Vector
*/

public class Vector2D {
	
	   protected Point2D a;
	   protected Point2D b;

	   /*
	    * Constructor with 2 params
	    * @param Coordinate Point A
	    * @param Coordinate Point B
	    */
	   
	   public Vector2D( Point2D a, Point2D b ) {
	      this.a = a;
	      this.b = b;
	   }
	   
	   // Get Point A value
	   
	   public Point2D getA(){
		   return a;
	   }
	   
	   // Get Point B value
	   
	   public Point2D getB(){
		   return b;
	   }

	   // Convert vector to string
	    
	   @Override
	public String toString() {
	      return "(A: [" + a.getX() + "," + a.getY() + "] B: [" + b.getX() + "," + b.getY() + "])";
	   }

	   // Return length
	 
	   public double length() {
		   return Math.sqrt (Math.pow(b.getX() - a.getX(), 2)+ Math.pow(b.getY() - a.getY(), 2));
	   }

	   // Dot product

	   public double dotProduct ( Vector2D v1 ) {
		   return ((b.getX() - a.getX()) * v1.getA().getX() + (b.getY() - a.getY()) * v1.getA().getY());
	   }

}