PageSourceSearch

https://phylogeographer.com/scripts/pathing.js

js phylogeographer.com collected 2026-10-02 14:54:47 UTC 2,572 bytes, 95 lines download raw bytes

1function getDist2(p1, p2) {
2    return Math.sqrt((p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]));
3}
4
5function refineParentFromGrandParentAndChildren(grandparent, children) {
6    var pointDists = []
7    for (var i = 0; i < children.length; i++) {
8        pointDists.push([children[i], getDist2(children[i],grandparent)])
9    }
10    var mindex = 0
11    var mindist = pointDists[0][1]
12    for (var i = 0; i < pointDists.length; i++) {
13        if (pointDists[i][1] < mindist) {
14            mindex = i;
15            mindist = pointDists[i][1]
16        }
17    }
18    return pointDists[mindex][0]
19}
20
21function pDistancePoint(x, y, x1, y1, x2, y2) {
22
23    var A = x - x1;
24    var B = y - y1;
25    var C = x2 - x1;
26    var D = y2 - y1;
27  
28    var dot = A * C + B * D;
29    var len_sq = C * C + D * D;
30    var param = -1;
31    if (len_sq != 0) //in case of 0 length line
32        param = dot / len_sq;
33  
34    var xx, yy;
35  
36    if (param < 0) {
37      xx = x1;
38      yy = y1;
39    }
40    else if (param > 1) {
41      xx = x2;
42      yy = y2;
43    }
44    else {
45      xx = x1 + param * C;
46      yy = y1 + param * D;
47    }
48  
49    var dx = x - xx;
50    var dy = y - yy;
51    return [yy,xx]//Math.sqrt(dx * dx + dy * dy);
52  }
53
54function segmentDistToPoint(segA, segB, p) {
55    var p2 = [segB[1] - segA[1], segB[0] - segA[0]];
56    var something = p2[1] * p2[1] + p2[0]*p2[0];
57    var u = ((p[1] - segA[1]) * p2[1] + (p[0] - segA[0]) * p2[0]) / something;
58
59    if (u > 1) {
60        u = 1;
61    } else if (u < 0) {
62        u = 0;
63    }
64    
65    var x = segA[1] + u * p2[1];
66    var y = segA[0] + u * p2[0];
67
68    var dx = x - p[1];
69    var dy = y - p[0];
70    return [y, x]
71    //var dist = math.sqrt((dx*dx + dy*dy);
72
73    //return dist;
74}
75
76function refineParentFromGrandParentAndChildrenSegments(grandparent, children) {
77    var pointDists = []
78    for (var i = 0; i < children.length; i++) {
79        var endindex = i + 1
80        if (endindex == children.length) {
81            endindex = 0
82        }
83        closestSegment = pDistancePoint(grandparent[1], grandparent[0], children[i][1], children[i][0], children[endindex][1], children[endindex][0]);
84        pointDists.push([closestSegment, getDist2(closestSegment,grandparent)])
85    }
86    var mindex = 0
87    var mindist = pointDists[0][1]
88    for (var i = 0; i < pointDists.length; i++) {
89        if (pointDists[i][1] < mindist) {
90            mindex = i;
91            mindist = pointDists[i][1]
92        }
93    }
94    return pointDists[mindex][0]
95}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.