Repository navigation
Expand file tree
/
Copy pathVector2d.java
More file actions
100 lines (82 loc) · 2.56 KB
/
Copy pathVector2d.java
File metadata and controls
100 lines (82 loc) · 2.56 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import java.util.Objects;
import static java.lang.Integer.max;
import static java.lang.Integer.min;
public class Vector2d {
private int x;
private int y;
public Vector2d(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "(" + this.x + "," + this.y + ")";
}
public boolean precedes(Vector2d other) {
return this.x <= other.x && this.y <= other.y;
}
public boolean follows(Vector2d other) {
return this.x >= other.x && this.y >= other.y;
}
public Vector2d upperRight(Vector2d other) {
return new Vector2d(max(this.x, other.x), max(this.y, other.y));
}
public Vector2d lowerLeft(Vector2d other) {
return new Vector2d(min(this.x, other.x), min(this.y, other.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 boolean equals(Object other) {
if (this == other)
return true;
if (!(other instanceof Vector2d))
return false;
Vector2d that = (Vector2d) other;
return that.x == this.x && that.y == this.y;
}
public int hashCode() {
return Objects.hash(x, y);
}
public Vector2d opposite() {
return new Vector2d(-this.x, -this.y);
}
public Vector2d limitWithMapBounds(Vector2d v1) {
return new Vector2d((v1.x + x) % v1.x, (y + v1.y) % v1.y);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Vector2d multiplyByScalar(int a) {
return new Vector2d(x * a, y * a);
}
public MapDirection toMapDirection() {
if(this.x == 0 && this.y == 1) {
return MapDirection.NORTH;
}
if(this.x == 1 && this.y == 1) {
return MapDirection.NORTHEAST;
}
if(this.x == 1 && this.y == 0) {
return MapDirection.EAST;
}
if(this.x == 1 && this.y == -1) {
return MapDirection.SOUTHEAST;
}
if(this.x == 0 && this.y == -1) {
return MapDirection.SOUTH;
}
if(this.x == -1 && this.y == -1) {
return MapDirection.SOUTHWEST;
}
if(this.x == -1 && this.y == 0) {
return MapDirection.WEST;
}
return MapDirection.NORTHWEST;
}
}