diff --git a/src/actions/CutShapeAction.js b/src/actions/CutShapeAction.js index 534b941..59688e6 100644 --- a/src/actions/CutShapeAction.js +++ b/src/actions/CutShapeAction.js @@ -4,27 +4,25 @@ import Action from "./Action"; import { addPolygonDifferenceToShapes, addPolygonIntersectionToShapes, + shapeToGeometry, } from "../helpers/actions"; class CutShapeAction extends Action { constructor(shapes) { super(); this.update = (shapesById) => { - const actionGeom = shapes.map((actionShape) => [ - actionShape.data.points.map(({ x, y }) => [x, y]), - ]); + let actionGeom = shapes.map(shapeToGeometry); let cutShapes = {}; for (let shape of Object.values(shapesById)) { - const shapePoints = shape.data.points.map(({ x, y }) => [x, y]); - const shapeHoles = shape.data.holes.map((hole) => - hole.map(({ x, y }) => [x, y]) - ); - let shapeGeom = [[shapePoints, ...shapeHoles]]; + const shapeGeom = shapeToGeometry(shape); try { - const difference = polygonClipping.difference(shapeGeom, actionGeom); + const difference = polygonClipping.difference( + shapeGeom, + ...actionGeom + ); const intersection = polygonClipping.intersection( shapeGeom, - actionGeom + ...actionGeom ); addPolygonDifferenceToShapes(shape, difference, cutShapes); addPolygonIntersectionToShapes(shape, intersection, cutShapes); diff --git a/src/actions/SubtractShapeAction.js b/src/actions/SubtractShapeAction.js index 712bbff..13f915a 100644 --- a/src/actions/SubtractShapeAction.js +++ b/src/actions/SubtractShapeAction.js @@ -1,24 +1,24 @@ import polygonClipping from "polygon-clipping"; import Action from "./Action"; -import { addPolygonDifferenceToShapes } from "../helpers/actions"; +import { + addPolygonDifferenceToShapes, + shapeToGeometry, +} from "../helpers/actions"; class SubtractShapeAction extends Action { constructor(shapes) { super(); this.update = (shapesById) => { - const actionGeom = shapes.map((actionShape) => [ - actionShape.data.points.map(({ x, y }) => [x, y]), - ]); + const actionGeom = shapes.map(shapeToGeometry); let subtractedShapes = {}; for (let shape of Object.values(shapesById)) { - const shapePoints = shape.data.points.map(({ x, y }) => [x, y]); - const shapeHoles = shape.data.holes.map((hole) => - hole.map(({ x, y }) => [x, y]) - ); - let shapeGeom = [[shapePoints, ...shapeHoles]]; + const shapeGeom = shapeToGeometry(shape); try { - const difference = polygonClipping.difference(shapeGeom, actionGeom); + const difference = polygonClipping.difference( + shapeGeom, + ...actionGeom + ); addPolygonDifferenceToShapes(shape, difference, subtractedShapes); } catch { console.error("Unable to find difference for shapes"); diff --git a/src/helpers/actions.js b/src/helpers/actions.js index 44c51b7..90d882b 100644 --- a/src/helpers/actions.js +++ b/src/helpers/actions.js @@ -42,3 +42,11 @@ export function addPolygonIntersectionToShapes(shape, intersection, shapes) { }; } } + +export function shapeToGeometry(shape) { + const shapePoints = shape.data.points.map(({ x, y }) => [x, y]); + const shapeHoles = shape.data.holes.map((hole) => + hole.map(({ x, y }) => [x, y]) + ); + return [[shapePoints, ...shapeHoles]]; +}