Code cleanup. Copyright update.
[sixth-3d-demos.git] / src / main / java / eu / svjatoslav / sixth / e3d / examples / RandomPolygonsDemo.java
diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/examples/RandomPolygonsDemo.java b/src/main/java/eu/svjatoslav/sixth/e3d/examples/RandomPolygonsDemo.java
new file mode 100755 (executable)
index 0000000..3640e4b
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * Sixth 3D engine demos. Copyright ©2012-2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 3 of the GNU Lesser General Public License
+ * or later as published by the Free Software Foundation.
+ */
+
+package eu.svjatoslav.sixth.e3d.examples;
+
+import eu.svjatoslav.sixth.e3d.geometry.Point3D;
+import eu.svjatoslav.sixth.e3d.gui.ViewFrame;
+import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
+import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection;
+import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.line.LineAppearance;
+import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.solidpolygon.SolidPolygon;
+import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.wireframe.Grid3D;
+
+public class RandomPolygonsDemo {
+
+    private static void addRandomPolygon(final ShapeCollection geometryCollection) {
+        final Point3D polygonLocation = getRandomPoint(1000);
+
+        final double polygonAverageSize = 30;
+
+        final Point3D point1 = new Point3D(polygonLocation);
+        point1.add(getRandomPoint(polygonAverageSize));
+
+        final Point3D point2 = new Point3D(polygonLocation);
+        point2.add(getRandomPoint(polygonAverageSize));
+
+        final Point3D point3 = new Point3D(polygonLocation);
+        point3.add(getRandomPoint(polygonAverageSize));
+
+        final Color color = new Color(Math.random(), Math.random(),
+                Math.random(), 0.5d);
+
+        final SolidPolygon polygon = new SolidPolygon(point1, point2, point3,
+                color);
+        geometryCollection.addShape(polygon);
+    }
+
+    private static Point3D getRandomPoint(final double amplitude) {
+        return new Point3D((Math.random() * amplitude * 2d) - amplitude,
+                (Math.random() * amplitude * 2d) - amplitude, (Math.random()
+                * amplitude * 2d)
+                - amplitude);
+    }
+
+    public static void main(final String[] args) {
+
+        final ViewFrame viewFrame = new ViewFrame();
+
+        final ShapeCollection shapeCollection = viewFrame.getView()
+                .getContext().getRootShapeCollection();
+
+        // add grid
+        final LineAppearance appearance = new LineAppearance(5, new Color(100,
+                100, 255, 60));
+
+        shapeCollection.addShape(new Grid3D(new Point3D(1000, -1000, -1000),
+                new Point3D(-1000, 1000, 1000), 300, appearance));
+
+        // add random polygons
+        for (int i = 0; i < 3000; i++)
+            addRandomPolygon(shapeCollection);
+
+    }
+}