This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Addition = { | |
| type: 'addition' | |
| next: any | |
| } | |
| type Change = { | |
| type: 'change' | |
| prev: any | |
| next: any | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function adjacentPairs<T>( | |
| itr: Iterable<T>, | |
| n: number, | |
| isClosed?: boolean, | |
| ): IterableIterator<T[]> { | |
| const buffer = new Array<T>(n); | |
| const it = itr[Symbol.iterator](); | |
| let res = it.next(); | |
| for (let i = 0; i < n; ++i) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export interface Neighbor { | |
| index: number; | |
| count: number; | |
| } | |
| export class UndirectedMultiGraph { | |
| edges = [] as Neighbor[][]; | |
| addEdge(u: number, v: number) { | |
| this.connectDirected(u, v); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Use https://gist.github.com/Phryxia/72f336275a9807e7797d93fc31673393 | |
| import { UndirectedMultiGraph } from './UndirectedMultiGraph'; | |
| /** | |
| * Let v1, v2, ..., vn be vertices of unknown undirected multi graph. | |
| * This function find any euler path using only given random-ordered edges. | |
| * If there is euler circuit, return circuit. | |
| * If there is no euler path or circuit, the behavior is undefined. | |
| * | |
| * @param edges |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function decideCCW(...path: Vector2[]) { | |
| if (path.length < 3) return 0; | |
| const yBase = path.reduce((acc, v) => Math.min(acc, v.y), Infinity); | |
| // https://stackoverflow.com/questions/1165647/how-to-determine-if-a-list-of-polygon-points-are-in-clockwise-order | |
| let sum = 0; | |
| for (const [v1, v2] of adjacentPairs(path, 2, true)) { | |
| const area = (v2.x - v1.x) * (v2.y + v1.y - 2 * yBase); | |
| sum += area; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface Edge { | |
| neighbor: number | |
| isReversed?: boolean | |
| } | |
| interface Graph { | |
| numberOfVertices: number | |
| edges: Edge[][] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export class SymmetricMatrix<T> { | |
| protected data: T[]; | |
| constructor( | |
| public readonly size: number, | |
| initializer: (i: number, j: number) => T, | |
| ) { | |
| this.data = new Array(size); | |
| for (let i = 0; i < size; ++i) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface FloodingParams<T> { | |
| seeds: T[]; | |
| spanner(seed: T): T[]; | |
| serializer(seed: T): number | string; | |
| iterationLimit?: number; | |
| } | |
| export function flooding<T>({ | |
| seeds, | |
| spanner, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Vector2 } from 'three'; | |
| export function findCircumCircle(vertices: Vector2[]) { | |
| if (vertices.length === 1) { | |
| return [vertices[0], 0]; | |
| } | |
| if (vertices.length === 2) { | |
| return [ | |
| vertices[0].clone().add(vertices[1]).multiplyScalar(0.5), | |
| vertices[0].distanceTo(vertices[1]) * 0.5, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CumulativeSum { | |
| private sums: number[] = [0]; | |
| constructor(itr: Iterable<number>) { | |
| const values = [...itr]; | |
| for (let i = 0; i < values.length; ++i) { | |
| this.sums[i + 1] = this.sums[i] + values[i]; | |
| } | |
| } |