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 an unbalanced BSP tree for a hexagon.
029 */
030public final class HexagonUnbalanced {
031
032    /** No instantiation. */
033    private HexagonUnbalanced() {}
034
035    /** Tutorial code entry point.
036     * @param args command arguments; if given, the first argument is used as the location of
037     *      output folder
038     */
039    public static void main(final String[] args) {
040        final File outputFolder = new File(args.length > 0 ? args[0] : ".");
041        final BSPTreeSVGWriter svgWriter = new BSPTreeSVGWriter(Bounds2D.from(Vector2D.of(-8, -8), Vector2D.of(8, 8)));
042        svgWriter.setTreeParentOffsetFactor(0);
043        svgWriter.setTreeParentXOffsetMin(20);
044
045        final Precision.DoubleEquivalence precision = Precision.doubleEquivalenceOfEpsilon(1e-6);
046
047        final RegionBSPTree2D tree = RegionBSPTree2D.empty();
048
049        final LinePath path = LinePath.fromVertexLoop(Arrays.asList(
050                    Vector2D.of(-4, 0),
051                    Vector2D.of(-2, -3),
052                    Vector2D.of(2, -3),
053                    Vector2D.of(4, 0),
054                    Vector2D.of(2, 3),
055                    Vector2D.of(-2, 3)
056                ), precision);
057        tree.insert(path);
058
059        svgWriter.write(tree, new File(outputFolder, "hex-unbalanced.svg"));
060    }
061}