alexbol99 / flatten-js

Javascript library for 2d geometry
MIT License
553 stars 58 forks source link

Add support for clipping a multi-line by a polygon #91

Open lostPixels opened 3 years ago

lostPixels commented 3 years ago

Hello,

This is a feature request for adding new functionality to multilines. I'd like to be able to "trim" a multi-line down to only segments that exist inside of a polygon. Much like how a "difference" boolean operation between polygons work, but I would like to use a line instead of a polygon.

Hopefully this poorly drawn graphic helps visualize the ask.

image

There's not any existing library in JS that does this, and my SO question hasnt received a ton of action: https://stackoverflow.com/questions/68382197/clip-a-line-by-a-polygon-in-javascript

For single segments I can write my own version of this algorithm, but introducing multilines is a complexity that I can't seem to conquer.

alexbol99 commented 3 years ago

Hello, James,

The idea of algorithm is: 1) Find intersection point between multiline and polygon 2) Insert intersection points into multiline 3) Return edges of split multiline that lay inside polygon

All these tools implemented in the library. If you send me a real data I will show you how to do that.

Do you need that parts of multiline inside polygon will be ordered in a specific order?

Best regards, Alex