001/*
002
003*/
004
005package renderer.models;
006import  renderer.scene.*;
007
008/**
009   Create a wireframe model of a right square pyramid with its
010   base in the xz-plane and its apex on the positive y-axis.
011<p>
012   See <a href="https://en.wikipedia.org/wiki/Pyramid_(geometry)" target="_top">
013                https://en.wikipedia.org/wiki/Pyramid_(geometry)</a>
014
015   @see PyramidFrustum
016*/
017public class Pyramid extends Model
018{
019   /**
020      Create a right square pyramid with its base in the xz-plane,
021      a base side length of 2, height 1, and apex on the positive y-axis.
022   */
023   public Pyramid( )
024   {
025      this(2, 1, 15, 4);
026   }
027
028
029   /**
030      Create a right square pyramid with its base in the xz-plane,
031      a base length of {@code s}, height {@code h}, and apex on the
032      positive y-axis.
033
034      @param s  side length of the base in the xz-plane
035      @param h  height of the apex on the y-axis
036      @param n  number of lines of latitude around the body of the pyramid
037      @param k  number of triangles in the triangle fan at the top of each side
038   */
039   public Pyramid(double s, double h, int n, int k)
040   {
041      super();
042
043      if (n < 1) n = 1;
044      if (k < 1) k = 1;
045
046      // Create the pyramid's geometry.
047
048      // Create all the lines of "longitude" from the apex, down
049      // to the base, across the base, and then back up to the apex.
050      s = s/2;
051      double delta = (2 * s) / k;
052      // lines of "longitude" perpendicular to the x-axis
053      for (int j = 0; j < k + 1; j++)
054      {
055         double d = j * delta;
056         addLineSegment(new Vertex(   0, h,  0), new Vertex(-s+d, 0, -s));
057         addLineSegment(new Vertex(-s+d, 0, -s), new Vertex(-s+d, 0,  s));
058         addLineSegment(new Vertex(-s+d, 0,  s), new Vertex(   0, h,  0));
059      }
060      // lines of "longitude" perpendicular to the z-axis
061      for (int j = 1; j < k; j++)
062      {
063         double d = j * delta;
064         addLineSegment(new Vertex( 0, h,    0), new Vertex( s, 0, -s+d));
065         addLineSegment(new Vertex( s, 0, -s+d), new Vertex(-s, 0, -s+d));
066         addLineSegment(new Vertex(-s, 0, -s+d), new Vertex( 0, h,    0));
067      }
068      // Create all the lines of "latitude" around the pyramid, starting
069      // from the base and working upwards.
070      double deltaH = h / n;
071      double deltaS = s / n;
072      for (int i = 0; i < n; i++)
073      {
074         h = i * deltaH;
075         addLineSegment(new Vertex( s, h,  s), new Vertex( s, h, -s));
076         addLineSegment(new Vertex( s, h, -s), new Vertex(-s, h, -s));
077         addLineSegment(new Vertex(-s, h, -s), new Vertex(-s, h,  s));
078         addLineSegment(new Vertex(-s, h,  s), new Vertex( s, h,  s));
079         s -= deltaS;
080      }
081   }
082}//Pyramid