-
Notifications
You must be signed in to change notification settings - Fork 1
/
Circle.ts
41 lines (34 loc) · 1.07 KB
/
Circle.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
import {scalar, vec2} from 'linearly'
/**
* A circle represented as a tuple of a center and a radius.
* @category Types
*/
export type Circle = readonly [center: vec2, radius: number]
/**
* Functions for manipulating circles represented as {@link Circle}.
*/
export namespace Circle {
/**
* Returns the circumscribed circle of the given triangle.
* @param a The first point of the triangle
* @param b The second point of the triangle
* @param c The third point of the triangle
* @returns The circumscribed circle of the given triangle
*/
export function circumcircle(a: vec2, b: vec2, c: vec2): Circle | null {
const d =
2 * (a[0] * (b[1] - c[1]) + b[0] * (c[1] - a[1]) + c[0] * (a[1] - b[1]))
if (scalar.approx(d, 0)) {
return null
}
const A2 = vec2.sqrLen(a)
const B2 = vec2.sqrLen(b)
const C2 = vec2.sqrLen(c)
const center: vec2 = [
(A2 * (b[1] - c[1]) + B2 * (c[1] - a[1]) + C2 * (a[1] - b[1])) / d,
(A2 * (c[0] - b[0]) + B2 * (a[0] - c[0]) + C2 * (b[0] - a[0])) / d,
]
const radius = vec2.dist(center, a)
return [center, radius]
}
}