Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/vs/base/common/numbers.ts
13405 views
1
//!!! DO NOT modify, this file was COPIED from 'microsoft/vscode'
2
3
/*---------------------------------------------------------------------------------------------
4
* Copyright (c) Microsoft Corporation. All rights reserved.
5
* Licensed under the MIT License. See License.txt in the project root for license information.
6
*--------------------------------------------------------------------------------------------*/
7
8
import { assert } from './assert';
9
10
export function clamp(value: number, min: number, max: number): number {
11
return Math.min(Math.max(value, min), max);
12
}
13
14
export function rot(index: number, modulo: number): number {
15
return (modulo + (index % modulo)) % modulo;
16
}
17
18
export class Counter {
19
private _next = 0;
20
21
getNext(): number {
22
return this._next++;
23
}
24
}
25
26
export class MovingAverage {
27
28
private _n = 1;
29
private _val = 0;
30
31
update(value: number): number {
32
this._val = this._val + (value - this._val) / this._n;
33
this._n += 1;
34
return this._val;
35
}
36
37
get value(): number {
38
return this._val;
39
}
40
}
41
42
export class SlidingWindowAverage {
43
44
private _n: number = 0;
45
private _val = 0;
46
47
private readonly _values: number[] = [];
48
private _index: number = 0;
49
private _sum = 0;
50
51
constructor(size: number) {
52
this._values = new Array(size);
53
this._values.fill(0, 0, size);
54
}
55
56
update(value: number): number {
57
const oldValue = this._values[this._index];
58
this._values[this._index] = value;
59
this._index = (this._index + 1) % this._values.length;
60
61
this._sum -= oldValue;
62
this._sum += value;
63
64
if (this._n < this._values.length) {
65
this._n += 1;
66
}
67
68
this._val = this._sum / this._n;
69
return this._val;
70
}
71
72
get value(): number {
73
return this._val;
74
}
75
}
76
77
/** Returns whether the point is within the triangle formed by the following 6 x/y point pairs */
78
export function isPointWithinTriangle(
79
x: number, y: number,
80
ax: number, ay: number,
81
bx: number, by: number,
82
cx: number, cy: number
83
) {
84
const v0x = cx - ax;
85
const v0y = cy - ay;
86
const v1x = bx - ax;
87
const v1y = by - ay;
88
const v2x = x - ax;
89
const v2y = y - ay;
90
91
const dot00 = v0x * v0x + v0y * v0y;
92
const dot01 = v0x * v1x + v0y * v1y;
93
const dot02 = v0x * v2x + v0y * v2y;
94
const dot11 = v1x * v1x + v1y * v1y;
95
const dot12 = v1x * v2x + v1y * v2y;
96
97
const invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
98
const u = (dot11 * dot02 - dot01 * dot12) * invDenom;
99
const v = (dot00 * dot12 - dot01 * dot02) * invDenom;
100
101
return u >= 0 && v >= 0 && u + v < 1;
102
}
103
104
export function randomChance(p: number): boolean {
105
assert(p >= 0 && p <= 1, 'p must be between 0 and 1');
106
return Math.random() < p;
107
}
108
109