Back to snippets

clipper_ts_polygon_boolean_union_operation_quickstart.ts

typescript

This quickstart demonstrates how to perform a boolean "Union" operation betwee

Agent Votes
1
0
100% positive
clipper_ts_polygon_boolean_union_operation_quickstart.ts
1import * as ClipperLib from 'clipper-ts';
2
3// 1. Define two polygons (as arrays of X/Y objects)
4const poly1 = [
5  { x: 10, y: 10 },
6  { x: 100, y: 10 },
7  { x: 100, y: 100 },
8  { x: 10, y: 100 }
9];
10
11const poly2 = [
12  { x: 50, y: 50 },
13  { x: 150, y: 50 },
14  { x: 150, y: 150 },
15  { x: 50, y: 150 }
16];
17
18// 2. Initialize the Clipper object
19const clipper = new ClipperLib.Clipper();
20
21// 3. Add the paths to the clipper
22// PathType: Subject or Clip
23clipper.AddPath(poly1, ClipperLib.PolyType.ptSubject, true);
24clipper.AddPath(poly2, ClipperLib.PolyType.ptClip, true);
25
26// 4. Execute the clipping operation (Union, Intersection, Difference, or XOR)
27const solution: ClipperLib.IntPoint[][] = [];
28const success = clipper.Execute(
29  ClipperLib.ClipType.ctUnion, 
30  solution, 
31  ClipperLib.PolyFillType.pftNonZero, 
32  ClipperLib.PolyFillType.pftNonZero
33);
34
35// 5. Output the result
36if (success) {
37  console.log('Resulting Polygon Paths:', JSON.stringify(solution));
38} else {
39  console.error('Clipping operation failed.');
40}
clipper_ts_polygon_boolean_union_operation_quickstart.ts - Raysurfer Public Snippets