Repository navigation
Expand file tree
/
Copy pathVector.java
More file actions
77 lines (66 loc) · 1.47 KB
/
Copy pathVector.java
File metadata and controls
77 lines (66 loc) · 1.47 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* This class basically does the same thing
* as class Pair from our labs: it stores coordinates in a single object
* There are also methods that perform mathematical operations with vectors
* Note: position will also be a vector
* @author ikramgabiyev
*
*/
public class Vector
{
public double x;
public double y;
public double magnitude;
public Vector(double _x, double _y)
{
this.x = _x;
this.y = _y;
this.magnitude = Math.sqrt((_x*_x) + (_y*_y));
}
/**
* adds two vectors
* @param _v2
* @return
*/
public Vector add(Vector _v2)
{
return new Vector(this.x + _v2.x, this.y + _v2.y);
}
/**
* subtract vectors from each other
* @param _v2
* @return
*/
public Vector subtract(Vector _v2)
{
return new Vector(this.x - _v2.x, this.y - _v2.y);
}
/**
* multiply every component
* of the vector by some coefficient
* @param coefficient
* @return
*/
public Vector multiply(double coefficient)
{
return new Vector(this.x * coefficient, this.y * coefficient);
}
/**
* divide every component of the vector
* by some coefficient
* @param coefficient
* @return
*/
public Vector divide(double coefficient)
{
return new Vector(this.x / coefficient, this.y / coefficient);
}
/**
* get the magnitude of the difference
* between two vectors (such as position)
*/
public static double getDistance(Vector _v1, Vector _v2)
{
return Math.sqrt( Math.pow(_v1.x - _v2.x, 2) + Math.pow(_v1.y - _v2.y, 2) );
}
}