-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvectors.java
More file actions
47 lines (36 loc) · 1.16 KB
/
vectors.java
File metadata and controls
47 lines (36 loc) · 1.16 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
class Vector2D {
private double x;
private double y;
public Vector2D(double x, double y) {
this.x = x;
this.y = y;
}
public Vector2D add(Vector2D other) {
return new Vector2D(this.x + other.x, this.y + other.y);
}
public Vector2D subtract(Vector2D other) {
return new Vector2D(this.x - other.x, this.y - other.y);
}
public double dot(Vector2D other) {
return this.x * other.x + this.y * other.y;
}
public double magnitude() {
return Math.sqrt(x * x + y * y);
}
@Override
public String toString() {
return "(" + x + ", " + y + ")";
}
}
public class vectors {
public static void main(String[] args) {
Vector2D v1 = new Vector2D(2, 3);
Vector2D v2 = new Vector2D(4, 1);
System.out.println("v1 = " + v1);
System.out.println("v2 = " + v2);
System.out.println("Addition: " + v1.add(v2));
System.out.println("Subtraction: " + v1.subtract(v2));
System.out.println("Dot product: " + v1.dot(v2));
System.out.println("Magnitude of v1: " + v1.magnitude());
}
}