summaryrefslogtreecommitdiffstats
path: root/webapp/utils/commons.jsx
blob: 1888869dc84314a503384ae552b3927e3b685986 (plain)
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
export function getDistanceBW2Points(point1, point2, xAttr = 'x', yAttr = 'y') {
    return Math.sqrt(Math.pow(point1[xAttr] - point2[xAttr], 2) + Math.pow(point1[yAttr] - point2[yAttr], 2));
}

/**
  * Funtion to return nearest point of given pivot point.
  * It return two points one nearest and other nearest but having both coorditanes smaller than the given point's coordinates.
  */
export function getNearestPoint(pivotPoint, points, xAttr = 'x', yAttr = 'y') {
    var nearestPoint = {};
    var nearestPointLte = {};  // Nearest point smaller than or equal to point
    for (const point of points) {
        if (typeof nearestPoint[xAttr] === 'undefined' || typeof nearestPoint[yAttr] === 'undefined') {
            nearestPoint = point;
        } else if (getDistanceBW2Points(point, pivotPoint, xAttr, yAttr) < getDistanceBW2Points(nearestPoint, pivotPoint, xAttr, yAttr)) {
        // Check for bestImage
            nearestPoint = point;
        }

        if (typeof nearestPointLte[xAttr] === 'undefined' || typeof nearestPointLte[yAttr] === 'undefined') {
            if (point[xAttr] <= pivotPoint[xAttr] && point[yAttr] <= pivotPoint[yAttr]) {
                nearestPointLte = point;
            }
        } else if (
        // Check for bestImageLte
            getDistanceBW2Points(point, pivotPoint, xAttr, yAttr) < getDistanceBW2Points(nearestPointLte, pivotPoint, xAttr, yAttr) &&
            point[xAttr] <= pivotPoint[xAttr] && point[yAttr] <= pivotPoint[yAttr]
        ) {
            nearestPointLte = point;
        }
    }
    return {
        nearestPoint,
        nearestPointLte
    };
}