alexbol99 / flatten-offset

Offset polygon
MIT License
17 stars 4 forks source link

Offset bug for a 1-offset of unit square #17

Open pgodman opened 2 years ago

pgodman commented 2 years ago

Thanks for sharing this work!

Please see this notebook for a repro: https://observablehq.com/@pgodman/unit-square-offset-bug

offsetting a unit square by one creates a wrong result.

Relevant code (I based this on your tutorial):

{
  let {point, segment, circle, arc, Polygon} = Flatten;
  let offset = FlattenOffset.default;

  let DISTANCE = 50;
  //let DISTANCE = 50.01;
  //let DISTANCE = 49.99;

  // Create new polygon
  let polygon = new Polygon([[50, 50], [50, 100], [100, 100], [100, 50], [50, 50]]);
  let expanded = offset(polygon, DISTANCE);

  let stage = d3.select(DOM.svg(width, height));

  // Add svg element to svg stage container
  stage.html(expanded.svg());

  return stage.node()
}
danielweck commented 8 months ago

Probably related? https://github.com/alexbol99/flatten-js/issues/124

danielweck commented 8 months ago

Possible workaround? https://github.com/alexbol99/flatten-js/issues/125#issuecomment-1781792539

danielweck commented 8 months ago

Ah, what worked for me was to check the Face orientation at (my base Polygon is of course made with Faces with ORIENTATION.CCW ... so your situation may be different than mine):

https://github.com/alexbol99/flatten-offset/blob/317902abd4485ed969be450a781fb6e771fefb83/src/polygonOffset.js#L174

// ...
const face = polygon.addFace([seg_left, cap1, seg_right, cap2]);

if (face.orientation() !== ORIENTATION.CCW) {
    face.reverse();
}