001/*
002
003*/
004
005package renderer.models;
006import  renderer.scene.*;
007
008/**
009   Create a wireframe model of a cube with its center
010   at the origin, having edge length 2, and with its
011   corners at {@code (±1, ±1, ±1)}.
012<p>
013   This version of the cube model has each face of
014   the cube cut up by an n by m grid of lines.
015<p>
016   Here is a picture showing how the cube's eight corners
017   are labeled.
018<pre>{@code
019                  v[4]
020                   +-----------------+ v[5]
021                  /|                /|
022                /  |              /  |
023              /    |            /    |
024            /      |          /      |
025      v[7] +-----------------+ v[6]  |
026           |       |         |       |               y
027           |       |         |       |               |
028           |       |         |       |               |
029           |  v[0] +---------|-------+ v[1]          |
030           |      /          |      /                |
031           |    /            |    /                  +----. x
032           |  /              |  /                   /
033           |/                |/                    /
034           +-----------------+                    /
035          v[3]              v[2]                 z
036}</pre>
037
038   @see Cube
039   @see Cube3
040   @see Cube4
041*/
042public class Cube2 extends Cube
043{
044   /**
045      Create a cube with its center at the origin, having edge
046      length 2, with its corners at {@code (±1, ±1, ±1)}. and
047      with two perpendicular grid lines going across the middle
048      of each of the cube's faces.
049   */
050   public Cube2( )
051   {
052      this(1, 1, 1);
053   }
054
055
056   /**
057      Create a cube with its center at the origin, having edge
058      length 2, with its corners at {@code (±1, ±1, ±1)}, and
059      with each of the cube's faces containing the given number
060      of grid lines parallel to the x, y, and z directions.
061
062      @param xGrid  number of grid lines perpendicular to the x-axis
063      @param yGrid  number of grid lines perpendicular to the y-axis
064      @param zGrid  number of grid lines perpendicular to the z-axis
065   */
066   public Cube2(int xGrid, int yGrid, int zGrid)
067   {
068      super();  // create the basic cube with 12 edges
069
070      if (xGrid < 0) xGrid = 0;
071      if (yGrid < 0) yGrid = 0;
072      if (zGrid < 0) zGrid = 0;
073
074      double xStep = 2.0 / (1 + xGrid);
075      double yStep = 2.0 / (1 + yGrid);
076      double zStep = 2.0 / (1 + zGrid);
077
078      // Grid lines perpendicular to the x-axis.
079      double x = -1.0;
080      for (int i = 0; i < xGrid; i++)
081      {
082         x += xStep;
083         // Start at the top, front edge, go down the front face, and around the cube.
084         addLineSegment(new Vertex(x,  1,  1), new Vertex(x, -1,  1));
085         addLineSegment(new Vertex(x, -1,  1), new Vertex(x, -1, -1));
086         addLineSegment(new Vertex(x, -1, -1), new Vertex(x,  1, -1));
087         addLineSegment(new Vertex(x,  1, -1), new Vertex(x,  1,  1));
088      }
089
090      // Grid lines perpendicular to the y-axis.
091      double y = -1.0;
092      for (int i = 0; i < yGrid; i++)
093      {
094         y += yStep;
095         // Start at the front, right edge, go left across the front face, and around the cube.
096         addLineSegment(new Vertex( 1, y,  1), new Vertex(-1, y,  1));
097         addLineSegment(new Vertex(-1, y,  1), new Vertex(-1, y, -1));
098         addLineSegment(new Vertex(-1, y, -1), new Vertex( 1, y, -1));
099         addLineSegment(new Vertex( 1, y, -1), new Vertex( 1, y,  1));
100      }
101
102      // Grid lines perpendicular to the z-axis.
103      double z = -1.0;
104      for (int i = 0; i < zGrid; i++)
105      {
106         z += zStep;
107         // Start at the top, right edge, go left across the top face, and around the cube.
108         addLineSegment(new Vertex( 1,  1, z), new Vertex(-1,  1, z));
109         addLineSegment(new Vertex(-1,  1, z), new Vertex(-1, -1, z));
110         addLineSegment(new Vertex(-1, -1, z), new Vertex( 1, -1, z));
111         addLineSegment(new Vertex( 1, -1, z), new Vertex( 1,  1, z));
112      }
113   }
114}//Cube2