makeTorus.java
Created with JBuilder
package torus;

public class makeTorus {

  // Create torus by dividing trapezoids each into two triangles,
  // returning an array of k triangles in form xyz[k][3]
  // deals directly with control panel, cp
  public static xyz [][] triangles(controls cp){

    // read parameters off cp sliders
    int n1=cp.n1.getValue();             // # divisions around equator
    int n2=cp.n2.getValue();             // # divisions through hole
    double r1=cp.r1.getValue()/100.0;    // mid-radius   (0 to 1)
    double r2=cp.r2.getValue()/200.0;    // small radius (0 to 0.5)

    cp.r1Label.setText("Mid radius: " + r1);  // display values as cp labels
    cp.r2Label.setText("Small radius: " + r2);
    cp.n1Label.setText("N around: " + n1);
    cp.n2Label.setText("N through hole: " + n2);

    xyz[][] triList = new xyz[2*n1*n2][3];
    for (int i1=0; i1<n1; i1++)
      for (int i2=0; i2<n2; i2++){
        triList[2*(i1*n2+i2)][0]=torusPoint(i1,i2,n1,n2,r1,r2);    // half a trapezoid
        triList[2*(i1*n2+i2)][1]=torusPoint(i1+1,i2,n1,n2,r1,r2);
        triList[2*(i1*n2+i2)][2]=torusPoint(i1+1,i2+1,n1,n2,r1,r2);

        triList[2*(i1*n2+i2)+1][0]=torusPoint(i1,i2,n1,n2,r1,r2);  // 2nd half
        triList[2*(i1*n2+i2)+1][1]=torusPoint(i1+1,i2+1,n1,n2,r1,r2);
        triList[2*(i1*n2+i2)+1][2]=torusPoint(i1,i2+1,n1,n2,r1,r2);
      }
    cp.status.setText(triList.length + " triangles");    // display # of triangles
    return triList;
  }


  // standard parametric generation of torus as n1*n2 trapezoids.
  // This method gives vertex at spot i1 (the big way) by i2 (through hole).
  // Note that all these sin's and cos's are called many times,
  // and each point is generated six times.  So this could be sped
  // up by storing these values in a table when n changes...
  private static xyz torusPoint(int i1, int i2, int n1, int n2, double r1, double r2){
    double theta1 = 2 * Math.PI * i1 / n1;
    double theta2 = 2 * Math.PI * i2 / n2;
    double r = (r1+r2*Math.cos(theta2));
    return new xyz( (float)(r * Math.cos(theta1)),
                    (float)(r * Math.sin(theta1)),
                    (float)(r2*Math.sin(theta2))  );
  }

}

makeTorus.java
Created with JBuilder