alexbol99 / flatten-js

Javascript library for 2d geometry
MIT License
546 stars 56 forks source link

Cannot cut polygon with Line #80

Closed tiansivive closed 1 year ago

tiansivive commented 3 years ago

From looking through the source code, it looks as if you're only able to cut a polygon into two if the "cutting line" is a segment with points on the polygon edges.

Anything else seems to fail, for example:


new F.Polygon([
  F.point(0, 0),
  F.point(100, 0),
  F.point(100, 100),
  F.point(0, 100)
])
const l = new F.Line(new F.Point(50, 0), new F.Vector(1, 0))
const [p1, p2] = polygon.cut(ml)

I'd expect this to split the polygon into 2, but instead p2 is undefined and p1 is just a copy of polygon

alexbol99 commented 3 years ago

Hello, @tiansivive ,

Method polygon.cut() does not support line as input. In fact, it accept multiline, which is sequence of of rays and segments, and indeed, segment should start and end on polygon edges. The code that will work:

  const { polygon, point, vector, line, multiline } = Flatten;

  const poly = polygon([[0, 0], [100, 0], [100, 100], [0, 100]]);
  const l = line(point(50, 0), vector(1, 0));
  const ip = l.intersect(poly);
  const ip_sorted = l.sortPoints(ip);
  const ml = multiline([l]).split(ip_sorted);
  const [p1, p2] = poly.cut(ml);

  return [p1, p2];

See the notebook: https://observablehq.com/d/ca0842f3e3e1b35b

Later I will add support for line too.

Best, Alex

tiansivive commented 3 years ago

Thanks for the example! The docs mention that Multiline could be a Line as well, so if it's not expected to work with Line, maybe we could just update the docs for the time being?

alexbol99 commented 3 years ago

I updated documentation Currently this option is not supported