diff --git a/fatline/Intersect.js b/fatline/Intersect.js index 3f9ba9ef..c1edd051 100644 --- a/fatline/Intersect.js +++ b/fatline/Intersect.js @@ -288,75 +288,70 @@ function clipFatLine(v1, v2, range2) { */ function getConvexHull(dq0, dq1, dq2, dq3) { var distq1 = getSignedDistance(0, dq0, 1, dq3, 1 / 3, dq1), - distq2 = getSignedDistance(0, dq0, 1, dq3, 2 / 3, dq2), - hull; + distq2 = getSignedDistance(0, dq0, 1, dq3, 2 / 3, dq2); // Check if [1/3, dq1] and [2/3, dq2] are on the same side of line // [0,dq0, 1,dq3] if (distq1 * distq2 < 0) { // dq1 and dq2 lie on different sides on [0, q0, 1, q3]. The hull is a // quadrilateral and line [0, q0, 1, q3] is NOT part of the hull so we // are pretty much done here. - hull = [ + return [ [ 0, dq0, 1 / 3, dq1 ], [ 1 / 3, dq1, 1, dq3 ], [ 2 / 3, dq2, 0, dq0 ], [ 1, dq3, 2 / 3, dq2 ] ]; + } + // dq1 and dq2 lie on the same sides on [0, q0, 1, q3]. The hull can be + // a triangle or a quadrilateral and line [0, q0, 1, q3] is part of the + // hull. Check if the hull is a triangle or a quadrilateral. + var dqMaxX, dqMaxY, vqa1a2X, vqa1a2Y, vqa1MaxX, vqa1MaxY, vqa1MinX, vqa1MinY; + if (Math.abs(distq1) > Math.abs(distq2)) { + dqMaxX = 1 / 3; + dqMaxY = dq1; + // apex is dq3 and the other apex point is dq0 vector + // dqapex->dqapex2 or base vector which is already part of the hull. + vqa1a2X = 1; + vqa1a2Y = dq3 - dq0; + // vector dqapex->dqMax + vqa1MaxX = 2 / 3; + vqa1MaxY = dq3 - dq1; + // vector dqapex->dqmin + vqa1MinX = 1 / 3; + vqa1MinY = dq3 - dq2; } else { - // dq1 and dq2 lie on the same sides on [0, q0, 1, q3]. The hull can be - // a triangle or a quadrilateral and line [0, q0, 1, q3] is part of the - // hull. Check if the hull is a triangle or a quadrilateral. - distq1 = Math.abs(distq1); - distq2 = Math.abs(distq2); - var dqmax, vqa1a2x, vqa1a2y, vqa1Maxx, vqa1Maxy, vqa1Minx, vqa1Miny; - if (distq1 > distq2) { - dqmax = [ 1 / 3, dq1 ]; - // apex is dq3 and the other apex point is dq0 vector - // dqapex->dqapex2 or base vector which is already part of the hull. - vqa1a2x = 1; - vqa1a2y = dq3 - dq0; - // vector dqapex->dqmax - vqa1Maxx = 2 / 3; - vqa1Maxy = dq3 - dq1; - // vector dqapex->dqmin - vqa1Minx = 1 / 3; - vqa1Miny = dq3 - dq2; - } else { - dqmax = [ 2 / 3, dq2 ]; - // apex is dq0 in this case, and the other apex point is dq3 vector - // dqapex->dqapex2 or base vector which is already part of the hull. - vqa1a2x = -1; - vqa1a2y = dq0 - dq3; - // vector dqapex->dqmax - vqa1Maxx = -2 / 3; - vqa1Maxy = dq0 - dq2; - // vector dqapex->dqmin - vqa1Minx = -1 / 3; - vqa1Miny = dq0 - dq1; - } - // Compare cross products of these vectors to determine, if - // point is in triangles [ dq3, dqMax, dq0 ] or [ dq0, dqMax, dq3 ] - var vcrossa1a2_a1Min = vqa1a2x * vqa1Miny - vqa1a2y * vqa1Minx; - var vcrossa1Max_a1Min = vqa1Maxx * vqa1Miny - vqa1Maxy * vqa1Minx; - if (vcrossa1Max_a1Min * vcrossa1a2_a1Min < 0) { + dqMaxX = 2 / 3; + dqMaxY = dq2; + // apex is dq0 in this case, and the other apex point is dq3 vector + // dqapex->dqapex2 or base vector which is already part of the hull. + vqa1a2X = -1; + vqa1a2Y = dq0 - dq3; + // vector dqapex->dqMax + vqa1MaxX = -2 / 3; + vqa1MaxY = dq0 - dq2; + // vector dqapex->dqmin + vqa1MinX = -1 / 3; + vqa1MinY = dq0 - dq1; + } + // Compare cross products of these vectors to determine, if + // point is in triangles [ dq3, dqMax, dq0 ] or [ dq0, dqMax, dq3 ] + var a1a2_a1Min = vqa1a2X * vqa1MinY - vqa1a2Y * vqa1MinX, + a1Max_a1Min = vqa1MaxX * vqa1MinY - vqa1MaxY * vqa1MinX; + return a1a2_a1Min * a1Max_a1Min < 0 // Point [2/3, dq2] is inside the triangle, the hull is a triangle. - hull = [ - [ 0, dq0, dqmax[0], dqmax[1] ], - [ dqmax[0], dqmax[1], 1, dq3 ], + ? [ + [ 0, dq0, dqMaxX, dqMaxY ], + [ dqMaxX, dqMaxY, 1, dq3 ], [ 1, dq3, 0, dq0 ] - ]; - } else { + ] // Convexhull is a quadrilateral and we need all lines in the // correct order where line [0, q0, 1, q3] is part of the hull. - hull = [ + : [ [ 0, dq0, 1 / 3, dq1 ], [ 1 / 3, dq1, 2 / 3, dq2 ], [ 2 / 3, dq2, 1, dq3 ], [ 1, dq3, 0, dq0 ] ]; - } - } - return hull; } // This is basically an "unrolled" version of #Line.getDistance() with sign