Turfjs / turf-line-slice-at-intersection

Segment a LineString by Features
6 stars 3 forks source link

Counter clockwise #3

Open mezgaAladar opened 7 years ago

mezgaAladar commented 7 years ago

Hello guys,

I'm using your module and it is a very great tool! But unfortunately it is not works if I want to slice by counter-clockwise polygons. I rewrote the testLineAndRing method within the module, and it works now, but only with counter-clockwise. Can someone recomment an ultimate solution?

I know I can check it with shoelace formula, and use one of the two methods in the particular cases, but I'm looking for a more simple solution.

My script is: ` var tempSegments = []; var cutPoints = []; var actualSegment; for (var k = 0; k < segments.length; k++) { var curr = []; for (var l = 0; l < segments[k].length - 1; l++) { curr.push(segments[k][l]); actualSegment = segments[k][l];

        //Collecting the intersection points
        for (var m = 0; m < ring.length - 1; m++) {
            if (equal(segments[k][l], ring[m])) {
                tempSegments.push(curr.slice());
                curr = [segments[k][l]];
                continue;
            }

            var is = linesIntersect(
                segments[k][l],
                segments[k][l + 1],
                ring[m],
                ring[m + 1]
            );

            if (is) {
                cutPoints.push(is);
            }
        }
    }

    while (cutPoints.length > 0) {
        //Which is the closest point?
        var minDist = Number.MAX_VALUE;
        var minDistSpot = -1;
        for (var c=0; c < cutPoints.length; c++) {
            if ( getDistanceByEndpoints(actualSegment[0],actualSegment[1],cutPoints[c][0],cutPoints[c][1]) < minDist ) {
                minDist = getDistanceByEndpoints(actualSegment[0],actualSegment[1],cutPoints[c][0],cutPoints[c][1]);
                minDistSpot = c;
            }
        }
        curr.push(cutPoints[minDistSpot]);
        tempSegments.push(curr.slice());
        curr = [cutPoints[minDistSpot]];
        cutPoints.splice(minDistSpot,1);

    }
    curr.push(segments[k][segments[k].length - 1]);
    tempSegments.push(curr.slice());

}

`