Posted by Fourth on Mon 17 Nov 15:05
report abuse | download | new post
- Cylinder::Cylinder(int n, int m) : Shape() {
- if (n < 3)
- n = 3;
- //n is the tessellation factor of the cylinder itself
- //m is the tessellation factor on the sides
- double x1,y1,z1,x2,y2,z2;
- x1 = -0.5;
- x2 = 0.5;
- y1 = -0.5;
- y2 = 0.5;
- z1 = .5;
- z2 = -.5;
- Point3 fixedPT( 0, y2, 0 );
- Point3 fixedPB( 0, y1, 0 );
- vector<Point3> circumPoints; //vector to hold circumference points
- //begin with the top and bottom
- //while n > 0, add points to the circumference and try triangles
- float angle = 360/n; //get the angle interval
- for( double i = 0; i < 360; i+=angle ) {
- double px = .5*cos( i * (PI/180) );
- double pz = .5*sin( i * (PI/180) );
- Point3 circP( px, y2, pz );
- circumPoints.push_back(circP);
- }
- //iterate through to make the top and the bottom
- for( int i = 0; i < n; i++ ) {
- int t = i;
- Point3 p1 = circumPoints.at(t);
- if( t+1 == n )
- t = -1;
- Point3 p2 = circumPoints.at(t+1);
- addTriangle( p2, p1, fixedPT );
- p1.y = y1;
- p2.y = y1;
- addTriangle( p1, p2, fixedPB );
- }
- //for each circumference point and the next circumference point
- //draw squares all the way down
- float fy = m-1;
- for( int x = 0; x < n; x++ ) { //number of faces
- int t = x;
- //grab the first 2 circumference points
- Point3 p1 = circumPoints.at(t);
- if(t+1 == n) {
- t = -1; //go back to the beginning of the vector
- }
- Point3 p2 = circumPoints.at(t+1);
- Point3 p3 = p2;
- Point3 temp = p2;
- p3.y = y1;
- for( int i = 0; i < m; i++ ) {
- float facty = fy/m;
- float npy = (1-facty)*p3.y + facty*p2.y;
- Point3 lowR( p1.x, npy, p1.z );
- Point3 midP( temp.x, npy, temp.z );
- addSquare( p1, temp, midP, lowR );
- p1 = lowR;
- temp = midP;
- fy--;
- }
- fy = m-1;
- }
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.