-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoint.ts
More file actions
63 lines (56 loc) · 1.16 KB
/
point.ts
File metadata and controls
63 lines (56 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { assert } from './assert.js';
export interface Point {
x: number;
y: number;
}
export function norm(point: Point): number {
return Math.sqrt(point.x ** 2 + point.y ** 2);
}
export function add(pointA: Point, pointB: Point): Point {
return {
x: pointA.x + pointB.x,
y: pointA.y + pointB.y,
};
}
export function subtract(pointA: Point, pointB: Point): Point {
return {
x: pointA.x - pointB.x,
y: pointA.y - pointB.y,
};
}
export function mulScalar(point: Point, scalar: number): Point {
return {
x: point.x * scalar,
y: point.y * scalar,
};
}
export function dotProduct(pointA: Point, pointB: Point): number {
return pointA.x * pointB.x + pointA.y * pointB.y;
}
export function getBoundaries(points: Point[]) {
assert(points.length > 1, 'must pass at least 2 points');
let maxX = 0;
let minX = Number.MAX_VALUE;
let maxY = 0;
let minY = Number.MAX_VALUE;
for (const { x, y } of points) {
if (x < minX) {
minX = x;
}
if (x > maxX) {
maxX = x;
}
if (y < minY) {
minY = y;
}
if (y > maxY) {
maxY = y;
}
}
return {
minX,
maxX,
minY,
maxY,
};
}