001/*
002
003*/
004
005package renderer.models;
006import  renderer.scene.*;
007
008/**
009   Create a wireframe model of a sector of a disk
010   in the xy-plane centered at the origin.
011<p>
012   See <a href="https://en.wikipedia.org/wiki/Circular_sector" target="_top">
013                https://en.wikipedia.org/wiki/Circular_sector</a>
014
015   @see Disk
016*/
017public class DiskSector extends Model
018{
019   /**
020      Create half a disk in the xy-plane with radius 1,
021      with 7 spokes coming out of the center, and
022      with 6 concentric circles around the disk.
023   */
024   public DiskSector( )
025   {
026      this(1, 0, Math.PI, 6, 7);
027   }
028
029
030   /**
031      Create a sector of a disk in the xy-plane with radius
032      {@code r}, with {@code k} spokes coming out of the center,
033      and with {@code n} concentric circles around the disk.
034   <p>
035      Notice that if there are {@code k} spokes, then each (partial)
036      circle around the center will have {@code k-1} line segments.
037      If there are {@code n} concentric circles around the center,
038      then each spoke will have {@code n} line segments.
039   <p>
040      There must be at least four spokes and at least one concentric circle.
041
042      @param r       radius of the disk
043      @param theta1  beginning angle of the sector
044      @param theta2  ending angle of the sector
045      @param n       number of concentric circles
046      @param k       number of spokes in the disk
047   */
048   public DiskSector(double r, double theta1, double theta2, int n, int k)
049   {
050      super();
051
052      if (n < 1) n = 1;
053      if (k < 4) k = 4;
054
055      // Create the disk's geometry.
056
057      double deltaR = r / n;
058      double deltaTheta = (theta2 - theta1) / (k - 1);
059
060      // An array of vertices to be used to create line segments.
061      Vertex[][] v = new Vertex[n][k];
062
063      // Create all the vertices.
064      for (int j = 0; j < k; j++) // choose a spoke (an angle)
065      {
066         double c = Math.cos(theta1 + j * deltaTheta);
067         double s = Math.sin(theta1 + j * deltaTheta);
068         for (int i = 0; i < n; i++) // move along the spoke
069         {
070            double ri = (i + 1) * deltaR;
071            v[i][j] = new Vertex( ri * c,
072                                  ri * s,
073                                  0 );
074         }
075      }
076      Vertex center = new Vertex(0,0,0);
077
078      // Create the line segments around each (partial) concentric circle.
079      for (int i = 0; i < n; i++)  // choose a circle
080      {
081         for (int j = 0; j < k - 1; j++)
082         {
083            addLineSegment(new LineSegment(new Vertex(v[i][j]),
084                                           new Vertex(v[i][j+1])));
085         }
086      }
087
088      // Create the spokes connecting the center to the outer circle.
089      for (int j = 0; j < k; j++) // choose a spoke
090      {
091         addLineSegment(new LineSegment(new Vertex(center),
092                                        new Vertex(v[0][j])));
093
094         for (int i = 0; i < n - 1; i++)
095         {
096            addLineSegment(new LineSegment(new Vertex(v[i][j]),
097                                           new Vertex(v[i+1][j])));
098         }
099      }
100   }
101}//DiskSector