-
Notifications
You must be signed in to change notification settings - Fork 12
/
xyMinYPoint.ts
55 lines (48 loc) · 1.23 KB
/
xyMinYPoint.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
import { DataXY } from 'cheminfo-types';
import { PointWithIndex } from '../types';
import { xGetFromToIndex } from '../x';
import { xyCheck } from './xyCheck';
export interface XYMinYPointOptions {
/**
* First value for xyMinYPoint in the X scale
*/
from?: number;
/**
* First point for xyMinYPoint
* @default 0
* */
fromIndex?: number;
/**
* Last point for xyMinYPoint
* @default x.length-1
* */
toIndex?: number;
/**
* Last value for xyMinYPoint in the X scale
*/
to?: number;
}
/**
* Finds the min y value in a range and return a {x,y} point
*
* @param data - Object that contains property x (an ordered increasing array) and y (an array)
* @param options - Options
*/
export function xyMinYPoint(
data: DataXY,
options: XYMinYPointOptions = {},
): PointWithIndex {
xyCheck(data, { minLength: 1 });
const { x, y } = data;
if (x.length === 1) return { x: x[0], y: y[0], index: 0 };
const { fromIndex, toIndex } = xGetFromToIndex(x, options);
let current: PointWithIndex = {
x: x[fromIndex],
y: y[fromIndex],
index: fromIndex,
};
for (let i = fromIndex; i <= toIndex; i++) {
if (y[i] < current.y) current = { x: x[i], y: y[i], index: i };
}
return current;
}