Vector2D.java
1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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());
}
}