001/*
002
003*/
004
005package renderer.models;
006import  renderer.scene.*;
007
008/**
009   Create a wireframe model of a partial right circular cylinder
010   with its axis along the y-axis.
011<p>
012   By a partial cylinder we mean a cylinder over a circular sector
013   of the cylinder's base.
014<p>
015   See <a href="https://en.wikipedia.org/wiki/Circular_sector" target="_top">
016                https://en.wikipedia.org/wiki/Circular_sector</a>
017
018   @see Cylinder
019*/
020public class CylinderSector extends Model
021{
022   /**
023      Create half of a cylinder with radius 1
024      and its axis along the y-axis from
025      {@code y = -1} to {@code y = 1}.
026   */
027   public CylinderSector( )
028   {
029      this(1, 1, Math.PI/2, 3*Math.PI/2, 15, 8);
030   }
031
032
033   /**
034      Create a part of the cylinder with radius {@code r} and its
035      axis along the y-axis from {@code y = -h} to {@code y = h}.
036   <p>
037      The partial cylinder is a cylinder over the circular sector
038      from angle {@code theta1} to angle {@code theta2}.
039   <p>
040      The last two parameters determine the number of lines of longitude
041      and the number of (partial) circles of latitude in the model.
042   <p>
043      Notice that if there are {@code n} circles of latitude in the model,
044      then each line of longitude will have {@code n-1} line segments.
045      If there are {@code k} lines of longitude, then each (partial)
046      circle of latitude will have {@code k-1} line segments.
047   <p>
048      There must be at least four lines of longitude and at least
049      two circles of latitude.
050
051      @param r       radius of the cylinder
052      @param h       height of the cylinder
053      @param theta1  beginning longitude angle of the sector
054      @param theta2  ending longitude angle of the sector
055      @param n       number of circles of latitude around the cylinder
056      @param k       number lines of longitude
057   */
058   public CylinderSector(double r, double h,
059                         double theta1, double theta2,
060                         int n, int k)
061   {
062      this(r, -h, h, theta1, theta2, n, k);
063   }
064
065
066   /**
067      Create a part of the cylinder with radius {@code r} and its
068      axis along the y-axis from {@code y = -h1} to {@code y = h1}.
069   <p>
070      If {@code theta1 > 0} or {@code theta2 < 2*PI},then the partial
071      cylinder is a cylinder over the circular sector from angle
072      {@code theta1} to angle {@code theta2}. In other words, the
073      (partial) circles of latitude in the model extend from angle
074      {@code theta1} to angle {@code theta2}.
075   <p>
076      The last two parameters determine the number of lines of longitude
077      and the number of (partial) circles of latitude in the model.
078   <p>
079      Notice that if there are {@code n} circles of latitude in the model,
080      then each line of longitude will have {@code n-1} line segments.
081      If there are {@code k} lines of longitude, then each (partial)
082      circle of latitude will have {@code  k-1} line segments.
083   <p>
084      There must be at least four lines of longitude and at least
085      two circles of latitude.
086
087      @param r       radius of the cylinder
088      @param h1      height (on the y-axis) of the base of the cylinder
089      @param h2      height (on the y-axis) of the top of the cylinder
090      @param theta1  beginning longitude angle of the sector
091      @param theta2  ending longitude angle of the sector
092      @param n       number of circles of latitude around the cylinder
093      @param k       number lines of longitude
094   */
095   public CylinderSector(double r,
096                         double h1, double h2,
097                         double theta1, double theta2,
098                         int n, int k)
099   {
100      super();
101
102      if (n < 2) n = 2;
103      if (k < 4) k = 4;
104
105      // Create the cylinder's geometry.
106
107      double deltaH = (h2 - h1) / (n - 1);
108      double deltaTheta = (theta2 - theta1)/ (k - 1);
109
110      // An array of vertices to be used to create triangles.
111      Vertex[][] v = new Vertex[n][k];
112
113      // Create all the vertices.
114      for (int j = 0; j < k; j++) // choose an angle of longitude
115      {
116         double c = Math.cos(theta1 + j*deltaTheta);
117         double s = Math.sin(theta1 + j*deltaTheta);
118         for (int i = 0; i < n; i++) // choose a circle of latitude
119         {
120            v[i][j] = new Vertex( r * c,
121                                  h1 + i * deltaH,
122                                  r * s );
123         }
124      }
125      Vertex topCenter = new Vertex(0, h2, 0);
126      Vertex bottomCenter = new Vertex(0, h1, 0);
127
128      // Create the horizontal (partial) circles of latitude around the cylinder.
129      for (int i = 0; i < n; i++)
130      {
131         for (int j = 0; j < k - 1; j++)
132         {
133            addLineSegment(new LineSegment(new Vertex(v[i][j]),
134                                           new Vertex(v[i][j+1])));
135         }
136      }
137
138      // Create the lines of longitude from the bottom to the top.
139      for (int j = 0; j < k; j++)
140      {
141         addLineSegment(new LineSegment(new Vertex(bottomCenter),
142                                        new Vertex(v[0][j])));
143
144         for (int i = 0; i < n - 1; i++)
145         {
146            addLineSegment(new LineSegment(new Vertex(v[i][j]),
147                                           new Vertex(v[i+1][j])));
148         }
149
150         addLineSegment(new LineSegment(new Vertex(v[n-1][j]),
151                                        new Vertex(topCenter)));
152      }
153   }
154}//CylinderSector