alexbol99 / flatten-js

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

Cutting polygons from each other #100

Closed henpanta closed 2 years ago

henpanta commented 2 years ago

Hello,

I'd like to get some advice here. The real world issue I'm trying to solve is to cut rectangles from ortogonal metal plates, and store the remaining material for later use.

 const plate = new Flatten.Polygon([
      [10, 10],
      [100, 10],
      [100, 100],
      [200, 100],
      [200, 200],
      [10, 200],
    ]);

    const toBeCut = new Flatten.Polygon([
      [10, 10],
      [70, 10],
      [70, 120],
      [10, 120],
    ]);

I'd like to cut toBeCut's dimensions from plate, expecting 2 polygons as the result, one is equivalent to toBeCut, and other is the remaining from plate. On the longer run I'd like to offer the user all possible positions, finding all possible angles by rotating toBeCut around the corners of plate (always snapping to the corners, and checking if it contains the actual variation a.k.a. fits into it), but I'm stucked at the first step: how to substract the smaller from the other.

thanks

henpanta commented 2 years ago

Sorry, I just found BooleanOperations, and it's working.

const p3 = Flatten.BooleanOperations.subtract(p1, p2);