Posted by Fourth on Mon 17 Nov 15:08
report abuse | download | new post
- Cone::Cone(int n, int m) : Shape() {
- if (n < 3)
- n = 3;
- double x1,y1,z1,x2,y2,z2;
- x1 = -0.5;
- x2 = 0.5;
- y1 = -0.5;
- y2 = 0.5;
- z1 = .5;
- z2 = -.5;
- Point3 fixedPB( 0, y1, 0 ); //fixed point at the bottom
- Point3 fixedPT( 0, y2, 0 ); //fixed point at the top
- vector<Point3> circumPoints; //vector to hold circumference points
- //do the bottom first
- //begin by solving the circumference points
- 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 create the bottom of the cone
- 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);
- p1.y = y1;
- p2.y = y1;
- addTriangle( p1, p2, fixedPB );
- }
- /*
- * to make the cone part, we start from the fixed point at the top
- * and move down iterations of fy. the first iteration will not
- * make a square, but all others will.
- */
- float f = m-1;
- for( int i = 0; i < n; i++ ) {
- int t = i;
- //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);
- p1.y = y1;
- p2.y = y1;
- Point3 tempr = p1;
- Point3 templ = p2;
- for( int i = 0; i < m; i++ ) {
- float fact = f/m;
- float npy = (1-fact)*p1.y + fact*fixedPT.y;
- float npzr = (1-fact)*p1.z + fact*fixedPT.z;
- float npzl = (1-fact)*p2.z + fact*fixedPT.z;
- float npxr = (1-fact)*p1.x + fact*fixedPT.x;
- float npxl = (1-fact)*p2.x + fact*fixedPT.x;
- Point3 uppR( npxr, npy, (-1)*npzr );
- Point3 uppL( npxl, npy, (-1)*npzl );
- if( i == 0 ) { //first iteration, so build a triangle at the top
- addTriangle( uppL, fixedPT, uppR );
- } else { //all other iterations make squares
- addSquare( uppR, uppL, templ, tempr );
- }
- tempr = uppR;
- templ = uppL;
- f--;
- }
- f = 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.