// from https://codepen.io/tculda/pen/pogwpOw // (https://stackoverflow.com/questions/22356012/multiple-points-colors-gradient-on-html5-canvas) static double[] getProjectionDistance(DoublePt a, DoublePt b, DoublePt c){ var k2 = b.x*b.x - b.x*a.x + b.y*b.y -b.y*a.y; var k1 = a.x*a.x - b.x*a.x + a.y*a.y -b.y*a.y; var ab2 = (a.x - b.x)*(a.x - b.x) + (a.y - b.y) * (a.y - b.y); var kcom = (c.x*(a.x - b.x) + c.y*(a.y-b.y)); var d1 = (k1 - kcom) / ab2; var d2 = (k2 + kcom) / ab2; ret new double[] {d1, d2}; }