001/*
002
003*/
004
005package renderer.models;
006import  renderer.scene.*;
007
008/**
009   Create a wireframe model of a square in the xy-plane centered at the origin.
010*/
011public class Square extends Model
012{
013   /**
014      Create a square in the xy-plane with corners {@code (±1, ±1, 0)}.
015   */
016   public Square( )
017   {
018      this(1, 0, 0);
019   }
020
021
022   /**
023      Create a square in the xy-plane with corners {@code (±1, ±1, 0)} and
024      with {@code n} grid lines parallel to each of the x and y axes.
025
026      @param n  number of grid lines parallel to the axes
027   */
028   public Square(int n)
029   {
030      this(1, n, n);
031   }
032
033
034   /**
035      Create a square in the xy-plane with corners {@code (±1, ±1, 0)} and
036      with {@code n} grid lines parallel to the y-axis and
037      with {@code m} grid lines parallel to the x-axis.
038
039      @param n  number of grid lines parallel to the y-axis
040      @param m  number of grid lines parallel to the x-axis
041   */
042   public Square(int n, int m)
043   {
044      this(1, n, m);
045   }
046
047
048   /**
049      Create a square in the xy-plane with corners {@code (±r, ±r, 0)}.
050
051      @param r  determines the corners of the square
052   */
053   public Square(double r)
054   {
055      this(r, 0, 0);
056   }
057
058
059   /**
060      Create a square in the xy-plane with corners {@code (±r, ±r, 0)} and
061      with {@code n} grid lines parallel to each of the x and y axes.
062
063      @param r  determines the corners of the square
064      @param n  number of grid lines parallel to the axes
065   */
066   public Square(double r, int n)
067   {
068      this(r, n, n);
069   }
070
071
072   /**
073      Create a square in the xy-plane with corners {@code (±r, ±r, 0)} and
074      with {@code n} grid lines parallel to the y-axis and
075      with {@code m} grid lines parallel to the x-axis.
076
077      @param r  determines the corners of the square
078      @param n  number of grid lines parallel to the y-axis
079      @param m  number of grid lines parallel to the x-axis
080   */
081   public Square(double r, int n, int m)
082   {
083      super();
084
085      if (n < 0) n = 0;
086      if (m < 0) m = 0;
087
088      r = Math.abs(r);
089      double xStep = (2 * r) / (1 + n);
090      double yStep = (2 * r) / (1 + m);
091
092      // Create the square's geometry.
093
094      // An array of vertices to be used to create the line segments.
095      Vertex[][] v = new Vertex[m+2][n+2];
096
097      // Create all the vertices.
098      for (int i = 0; i <= m+1; i++)
099      {
100         for (int j = 0; j <= n+1; j++)
101         {
102            v[i][j] = new Vertex(-r + j * xStep, -r + i * yStep, 0);
103         }
104      }
105
106      // Create the line segments parallel to the x-axis.
107      for (int i = 0; i <= m+1; i++)
108      {
109         for (int j = 0; j <= n; j++)
110         {
111            addLineSegment(v[i][j], v[i][j+1]);
112         }
113      }
114
115      // Create the line segments parallel to the y-axis.
116      for (int j = 0; j <= n+1; j++)
117      {
118         for (int i = 0; i <= m; i++)
119         {
120            addLineSegment(v[i  ][j], v[i+1][j]);
121         }
122      }
123
124   }
125}//Square