001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.geometry.examples.tutorials.bsp; 018 019import java.io.File; 020import java.util.Arrays; 021 022import org.apache.commons.geometry.euclidean.twod.Bounds2D; 023import org.apache.commons.geometry.euclidean.twod.RegionBSPTree2D; 024import org.apache.commons.geometry.euclidean.twod.Vector2D; 025import org.apache.commons.geometry.euclidean.twod.path.LinePath; 026import org.apache.commons.numbers.core.Precision; 027 028/** Class containing tutorial code for constructing a balanced tree using a 029 * partitioned region builder. 030 */ 031public final class HexagonPartitionedRegion { 032 033 /** No instantiation. */ 034 private HexagonPartitionedRegion() {} 035 036 /** Tutorial code entry point. 037 * @param args command arguments; if given, the first argument is used as the location of 038 * output folder 039 */ 040 public static void main(final String[] args) { 041 final File outputFolder = new File(args.length > 0 ? args[0] : "."); 042 final BSPTreeSVGWriter svgWriter = new BSPTreeSVGWriter(Bounds2D.from(Vector2D.of(-8, -8), Vector2D.of(8, 8))); 043 044 final Precision.DoubleEquivalence precision = Precision.doubleEquivalenceOfEpsilon(1e-6); 045 046 final LinePath path = LinePath.fromVertexLoop(Arrays.asList( 047 Vector2D.of(-4, 0), 048 Vector2D.of(-2, -3), 049 Vector2D.of(2, -3), 050 Vector2D.of(4, 0), 051 Vector2D.of(2, 3), 052 Vector2D.of(-2, 3) 053 ), precision); 054 055 final RegionBSPTree2D tree = RegionBSPTree2D.partitionedRegionBuilder() 056 .insertAxisAlignedGrid(path.getBounds(), 1, precision) 057 .insertBoundaries(path) 058 .build(); 059 060 svgWriter.write(tree, new File(outputFolder, "hex-partitioned.svg")); 061 } 062}