1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.geometry.examples.tutorials.bsp;
18
19 import java.io.File;
20 import java.util.Arrays;
21
22 import org.apache.commons.geometry.core.precision.DoublePrecisionContext;
23 import org.apache.commons.geometry.core.precision.EpsilonDoublePrecisionContext;
24 import org.apache.commons.geometry.euclidean.twod.Bounds2D;
25 import org.apache.commons.geometry.euclidean.twod.RegionBSPTree2D;
26 import org.apache.commons.geometry.euclidean.twod.Vector2D;
27 import org.apache.commons.geometry.euclidean.twod.path.LinePath;
28
29
30
31
32 public final class HexagonPartitionedRegion {
33
34
35 private HexagonPartitionedRegion() {}
36
37
38
39
40
41 public static void main(String[] args) {
42 File outputFolder = new File(args.length > 0 ? args[0] : ".");
43 BSPTreeSVGWriter svgWriter = new BSPTreeSVGWriter(Bounds2D.from(Vector2D.of(-8, -8), Vector2D.of(8, 8)));
44
45 DoublePrecisionContext precision = new EpsilonDoublePrecisionContext(1e-6);
46
47 LinePath path = LinePath.fromVertexLoop(Arrays.asList(
48 Vector2D.of(-4, 0),
49 Vector2D.of(-2, -3),
50 Vector2D.of(2, -3),
51 Vector2D.of(4, 0),
52 Vector2D.of(2, 3),
53 Vector2D.of(-2, 3)
54 ), precision);
55
56 RegionBSPTree2D tree = RegionBSPTree2D.partitionedRegionBuilder()
57 .insertAxisAlignedGrid(path.getBounds(), 1, precision)
58 .insertBoundaries(path)
59 .build();
60
61 svgWriter.write(tree, new File(outputFolder, "hex-partitioned.svg"));
62 }
63 }