97b18d80679ea34cce2e69a2ba0c5eeb63666a8a
[sixth-3d.git] /
1 /*
2  * Sixth 3D engine. Author: Svjatoslav Agejenko.
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.solid;
6
7 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
8 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
9 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.solidpolygon.SolidPolygon;
10 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.base.AbstractCompositeShape;
11
12 /**
13  * A solid square-based pyramid with the base centered at a given point.
14  *
15  * <p>The pyramid has a square base and four triangular faces meeting at an apex.
16  * The base has side length of {@code 2 * baseSize} and the height extends
17  * {@code height} units above the base center to the apex.</p>
18  *
19  * <p><b>Usage example:</b></p>
20  * <pre>{@code
21  * SolidPolygonPyramid pyramid = new SolidPolygonPyramid(
22  *         new Point3D(0, 0, 300), 50, 100, Color.BLUE);
23  * shapeCollection.addShape(pyramid);
24  * }</pre>
25  *
26  * @see SolidPolygonCube
27  * @see SolidPolygonSphere
28  * @see SolidPolygon
29  */
30 public class SolidPolygonPyramid extends AbstractCompositeShape {
31
32     /**
33      * Constructs a solid square-based pyramid with base centered at the given point.
34      *
35      * @param baseCenter the center point of the pyramid's base in 3D space
36      * @param baseSize   the half-width of the square base; the base extends
37      *                   this distance from the center along X and Z axes,
38      *                   giving a total base edge length of {@code 2 * baseSize}
39      * @param height     the height of the pyramid from base center to apex
40      * @param color      the fill color applied to all faces of the pyramid
41      */
42     public SolidPolygonPyramid(final Point3D baseCenter, final double baseSize,
43                                final double height, final Color color) {
44         super();
45
46         final double halfBase = baseSize;
47         final double apexY = baseCenter.y - height;
48         final double baseY = baseCenter.y;
49
50         Point3D frontLeft = new Point3D(baseCenter.x - halfBase, baseY, baseCenter.z - halfBase);
51         Point3D frontRight = new Point3D(baseCenter.x + halfBase, baseY, baseCenter.z - halfBase);
52         Point3D backRight = new Point3D(baseCenter.x + halfBase, baseY, baseCenter.z + halfBase);
53         Point3D backLeft = new Point3D(baseCenter.x - halfBase, baseY, baseCenter.z + halfBase);
54         Point3D apex = new Point3D(baseCenter.x, apexY, baseCenter.z);
55
56         // Polygons that touch apex
57         addShape(new SolidPolygon(frontLeft, frontRight, apex, color));
58         addShape(new SolidPolygon(frontRight, backRight, apex, color));
59         addShape(new SolidPolygon(backRight, backLeft, apex, color));
60         addShape(new SolidPolygon(backLeft, frontLeft, apex, color));
61
62         // Pyramid bottom
63         addShape(new SolidPolygon( backLeft, backRight, frontLeft, color));
64         addShape(new SolidPolygon( frontRight, frontLeft, backRight, color));
65
66         setBackfaceCulling(true);
67     }
68 }