title: Calculate 4 points of a square based on 2 points of a line
body: <p>I am trying to calculate 4 points of a square in three dimensional space that is perpendicular to a line formed by two given points. This square must be:</p>
  <ol>
  <li>Perpendicular to the line defined by two given points</li>
  <li>Of thickness (or width/length) 1</li>
  <li>aligned so that two sides are always perpendicular to the Y axis</li>
  </ol>
  <p>I have tried coding this myself, and i somewhat succeeded, but it is not exactly what i wanted. Here is a code snippet of what i have:</p>
  <p>Note: Vert is a class that stores a coordinate in 3 variables, x, y, and z. FourVert is a class that stores 4 Verts, and the constructor is what i am trying to develop. The two Vert parameters are the given points, and the float t is the thickness or separation required between the output 4 points. Add is a function that add the given Vert's coordinate value as an offset to all 4 points of the FourVert. This is only used to reposition the points on the right place, the first Vert. I might have weird naming conventions, but i write it in a way that i understand it.</p>
  <p><strong>EDIT</strong>: Added comments as requested</p>
  <pre><code>public FourVert(Vert V1, Vert V2, float t)
  {
      float dx = V2.x - V1.x; //Position our points at the origin to convert into
      float dy = V2.y - V1.y; //polar coordinates using Math.atan2();
      float dz = V2.z - V1.z;
      float d = (float) Math.sqrt(dx*dx+dz*dz); //Get the lateral distance to use
      float axz = (float) -Math.atan2(dx, dz);  //in relation with the y value to
      float ady = (float) Math.atan2(d, dy);    //calculate the altitude angle
      float px = (float) (Math.cos(axz) * t);
      float py = (float) (Math.cos(ady + 1.570796327) * t); //add half PI
      float ny = (float) (Math.cos(ady - 1.570796327) * t);
      float pz = (float) (Math.sin(axz) * t);
      v1 = new Vert(px, py, pz);   //Use the calculated values to produce 4 points
      v2 = new Vert(-px, py, -pz); //v1, v2, v3, and v4 are our Vert fields of the
      v3 = new Vert(-px, ny, -pz); //FourVert class
      v4 = new Vert(px, ny, pz);
      add(V1); //take our FourVert back to where we want it, not on the origin.
  }
  </code></pre>
  <p>I already have the rendering taken care of, and i know it works well because i have tested it with givens.</p>
  <p>What works here is the azimuth values, they function perfectly (Lateral movement). The vertical movement, or altitude, or 'pitch' does not function correctly. I have tried many other ways to do this to no avail.</p>
  <p>The problem/part that is not working is here I think:</p>
  <pre><code>    float py = (float) (Math.cos(ady + 1.570796327) * t);
      float ny = (float) (Math.cos(ady - 1.570796327) * t);
  </code></pre>