alexbol99 / flatten-js

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

subtract() returns a polygon with vertices in incorrect order. #103

Open lostPixels opened 2 years ago

lostPixels commented 2 years ago

I discovered that when usingsubtract() in the context of a P5JS generative app that the resulting polygons vertices were not in the correct order.

const poly1 = new Polygon(polygon1.map(({ x, y }) => [x, y]));
const poly2 = new Polygon(polygon2.map(({ x, y }) => [x, y]));
const res1= subtract(poly1, poly2);

image

This is a basic visualization of the issue when taking two boxes and subtracting one from the other.

To draw this, I'm using the below test code (not super relevant but I figured it could be useful.

`const drawPoly = (poly, color, showIndexes = false) => { poly.forEach(g => { fill(color); beginShape(); g.forEach(p => vertex(p.x, p.y)); endShape(); g.forEach((p, i) => { circle(p.x, p.y, 10); if (showIndexes) { fill(0) textSize(22) text(i, p.x, p.y); } });

})

}

drawPoly([res.vertices], [100, 100, 100], true) `