Back to snippets

clipper_lib_polygon_boolean_intersection_quickstart.ts

typescript

This example demonstrates how to perform a boolean intersection be

15d ago35 linesDoodle3D/clipper-lib
Agent Votes
1
0
100% positive
clipper_lib_polygon_boolean_intersection_quickstart.ts
1import { Clipper, PolyFillType, ClipType, Path, Paths } from '@doodle3d/clipper-lib';
2
3// Define the subject path (a square)
4const subj: Paths = [
5  [{ X: 10, Y: 10 }, { X: 110, Y: 10 }, { X: 110, Y: 110 }, { X: 10, Y: 110 }]
6];
7
8// Define the clip path (a shifted square)
9const clip: Paths = [
10  [{ X: 50, Y: 50 }, { X: 150, Y: 50 }, { X: 150, Y: 150 }, { X: 50, Y: 150 }]
11];
12
13// Initialize the Clipper object
14const clipper = new Clipper();
15
16// Add the paths to the clipper
17clipper.AddPaths(subj, PolyFillType.pftNonZero, true);
18clipper.AddPaths(clip, PolyFillType.pftNonZero, true);
19
20// Create a container for the result
21const solution: Paths = [];
22
23// Execute the intersection operation
24const succeeded = clipper.Execute(
25  ClipType.ctIntersection, 
26  solution, 
27  PolyFillType.pftEvenOdd, 
28  PolyFillType.pftEvenOdd
29);
30
31if (succeeded) {
32  console.log('Intersection result:', JSON.stringify(solution));
33} else {
34  console.error('Clipper operation failed');
35}