示例#1
0
void cylinder (Scene& scene, int nseg, const Trafo& P, const ColorB* color)
//
// Adds a unit cylinder (radius 1 in x-y-plane, from -1 to 1 in z-direction)
// to the scene. The cylinder mantle will be approximated by nseg segments.
// The cylinder can be scaled by an arbitrary transformation matrix P.
// Differenc to tube: the front and back ends are closed !
// Optionally a color (precisely a pointer to ColorB) can be given to set the
// color of the surface facets (defaults to NULL).
//
{
    int i;

    double theta = 0,
	   dtheta = 2*M_PI/nseg;

    float *x = 0, *y = 0;

    if ( !(x = new float[nseg]) ||
	 !(y = new float[nseg]) ) Matpack.Error("cylinder: out of memory");
    
    // construct a circular polygonal
    for (i = 0; i < nseg; i++, theta += dtheta) {
	x[i] = cos(theta);
	y[i] = sin(theta);
    }

    // extrude the polygonal 2 units in direction of the negative
    // z-axis, then shift the cylinder 1 unit centering it around
    // the origin
    scene.Extrude(nseg,x,y,2,P*trans(0,0,1),color);

    delete[] y;
    delete[] x;
}