-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxy.ts
135 lines (114 loc) · 2.79 KB
/
xy.ts
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
export const XYToString = ({ x, y }: XY) => `${x},${y}`;
export const parseXY = (str: string) => {
const [x, y] = str.split(",");
return {
x: parseInt(x),
y: parseInt(y),
};
};
export interface XY {
x: number;
y: number;
}
export const combineXY = (coordinates: XY[]): XY => {
if (coordinates.length === 0)
throw new Error("combineXY requires at least one coordinate");
if (coordinates.length === 1) return { ...coordinates[0] };
const xy = { x: 0, y: 0 };
coordinates.forEach(({ x, y }) => {
xy.x += x;
xy.y += y;
});
return xy;
};
export const subtractXY = (coordinates: XY[]): XY => {
if (coordinates.length === 0)
throw new Error("subtractXY requires at least one coordinate");
const xy = { ...coordinates[0] };
if (coordinates.length === 1) {
return xy;
}
coordinates.slice(1).forEach(({ x, y }) => {
xy.x -= x;
xy.y -= y;
});
return xy;
};
export class XYSet {
private xySet: Set<string>;
constructor(coordinates: XY[]) {
this.xySet = new Set(
coordinates.map((coordinate) => {
this.noteIfOutermost(coordinate);
return XYToString(coordinate);
})
);
}
private noteIfOutermost(xy: XY) {
if (!this.topmost || xy.y < this.topmost.y) {
this.topmost = xy;
}
if (!this.bottommost || xy.y > this.bottommost.y) {
this.bottommost = xy;
}
if (!this.rightmost || xy.x > this.rightmost.x) {
this.rightmost = xy;
}
if (!this.leftmost || xy.x < this.leftmost.x) {
this.leftmost = xy;
}
}
topmost?: XY;
bottommost?: XY;
rightmost?: XY;
leftmost?: XY;
add(coordinate: XY) {
this.xySet.add(XYToString(coordinate));
this.noteIfOutermost(coordinate);
}
has(coordinate: XY) {
return this.xySet.has(XYToString(coordinate));
}
copy() {
return new XYSet(Array.from(this.xySet.values()).map((xy) => parseXY(xy)));
}
exclude(otherXYSet: XYSet) {
return this.coordinates.filter((xy) => !otherXYSet.has(xy));
}
remove(coordinate: XY) {
this.xySet.delete(XYToString(coordinate));
}
get coordinates() {
return Array.from(this.xySet.values()).map((xy) => parseXY(xy));
}
static empty() {
return new XYSet([]);
}
get size() {
return this.xySet.size;
}
}
export class XYMap<Value> {
private map: Map<string, Value>;
constructor() {
this.map = new Map();
}
set(coordinate: XY, value: Value) {
this.map.set(XYToString(coordinate), value);
}
get(coordinate: XY) {
return this.map.get(XYToString(coordinate));
}
delete(coordinate: XY) {
this.map.delete(XYToString(coordinate));
}
get coordinates() {
return Array.from(this.map.keys()).map((xy) => parseXY(xy));
}
get values() {
return Array.from(this.map.values());
}
get size() {
return this.map.size;
}
}