flatten-offset icon indicating copy to clipboard operation
flatten-offset copied to clipboard

Offset bug for a 1-offset of unit square

Open pgodman opened this issue 3 years ago • 3 comments
trafficstars

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()
}

pgodman avatar Jul 06 '22 01:07 pgodman

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

danielweck avatar Feb 17 '24 10:02 danielweck

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

danielweck avatar Feb 17 '24 10:02 danielweck

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();
}

danielweck avatar Feb 17 '24 10:02 danielweck