-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.java
More file actions
80 lines (74 loc) · 1.67 KB
/
Vector.java
File metadata and controls
80 lines (74 loc) · 1.67 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
78
79
80
public class Vector {
double x,y;
public Vector(double x,double y){
this.x=x;
this.y=y;
}
public Vector(Vector other){
this.x=other.x;
this.y=other.y;
}
public static Vector add(Vector v1,Vector v2){
return new Vector(v1.x+ v2.x, v1.y+ v2.y);
}
public static Vector sub(Vector v1,Vector v2){
return new Vector(v1.x- v2.x, v1.y- v2.y);
}
public Vector set(double x,double y){
this.x=x;
this.y=y;
return this;
}
public Vector limit(double m){
if (this.mag()>m){
this.setMag(m);
}
return this;
}
public Vector copy(){
return new Vector(this);
}
public Vector setMag(double mag){
this.normalize();
this.mul(mag);
return this;
}
public static double dotProduct(Vector v1,Vector v2){
double product=0;
product+=v1.x*v2.x;
product+=v1.y*v2.y;
return product;
}
public Vector normalize(){
double len=Math.sqrt(x*x+y*y);
this.div(len);
return this;
}
public double mag(){
return Math.sqrt(x*x+y*y);
}
public Vector set(Vector v){
this.x=v.x;
this.y=v.y;
return this;
}
public void add(Vector v2){
x+=v2.x;
y+=v2.y;
}
public void sub(Vector v2){
x-=v2.x;
y-=v2.y;
}
public Vector mul(double m){
x*=m;
y*=m;
return this;
}
public void div(double m){
if (m!=0){
x/=m;
y/=m;
}
}
}