2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.solid;
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;
13 * A solid square-based pyramid with the base centered at a given point.
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>
19 * <p><b>Usage example:</b></p>
21 * SolidPolygonPyramid pyramid = new SolidPolygonPyramid(
22 * new Point3D(0, 0, 300), 50, 100, Color.BLUE);
23 * shapeCollection.addShape(pyramid);
26 * @see SolidPolygonCube
27 * @see SolidPolygonSphere
30 public class SolidPolygonPyramid extends AbstractCompositeShape {
33 * Constructs a solid square-based pyramid with base centered at the given point.
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
42 public SolidPolygonPyramid(final Point3D baseCenter, final double baseSize,
43 final double height, final Color color) {
46 final double halfBase = baseSize;
47 final double apexY = baseCenter.y - height;
48 final double baseY = baseCenter.y;
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);
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));
63 addShape(new SolidPolygon( backLeft, backRight, frontLeft, color));
64 addShape(new SolidPolygon( frontRight, frontLeft, backRight, color));
66 setBackfaceCulling(true);