Posted by Fourth on Mon 17 Nov 14:59
report abuse | View followups from Vectorxx | download | new post
- Cube::Cube(int n) : Shape() {
- double x1,x2,y1,y2,z1,z2;
- x1 = -0.5;
- x2 = 0.5;
- y1 = -0.5;
- y2 = 0.5;
- z1 = .5; // front
- z2 = -.5; // back
- Point3 op3(x2,y2,z1);
- Point3 op2(x1,y2,z1);
- Point3 op1(x1,y1,z1);
- Point3 p3 = op3;
- Point3 p2 = op2;
- Point3 p1 = op1;
- float fx = n-1;
- float fy = n-1;
- float fz = n-1;
- Point3 temp = p3;
- //front and back
- //iterate through the square creating smaller squares made up of 2 triangles
- for( int x = 0; x < 2; x++ ) {
- for( int i = 0; i < n; i++ ) { //rows
- float facty = fy/n;
- float npy = (1-facty)*p1.y + facty*p2.y;
- for( int j = 0; j < n; j++ ) { //cols
- float factx = fx/n;
- float npx = (1-factx)*p2.x + factx*p3.x;
- Point3 topL( npx, temp.y, temp.z);
- Point3 lowR( temp.x, npy, temp.z);
- Point3 midP( npx, npy, temp.z);
- addSquare( temp, topL, midP, lowR );
- temp = topL;
- fx--;
- }
- fx = n-1;
- temp.x = p3.x;
- temp.z = p3.z;
- temp.y = npy;
- fy--;
- }
- fy = n-1;
- //reset for the back
- p2 = op3;
- p3 = op2;
- p1.x = p2.x;
- p3.z = z2;
- p2.z = z2;
- p1.z = z2;
- temp = p3;
- }
- //set up for the top
- p3 = op3;
- p2 = op2;
- p1 = op1;
- p3.z = z2;
- p2.z = z2;
- p1.y = p2.y;
- fx = n-1;
- temp = p3;
- //top and bottom
- for( int x = 0; x < 2; x++ ) {
- for( int i = 0; i < n; i++ ) { //rows
- float factz = fz/n;
- float npz = (1-factz)*p1.z + factz*p2.z;
- for( int j = 0; j < n; j++ ) { //cols
- float factx = fx/n;
- float npx = (1-factx)*p2.x + factx*p3.x;
- Point3 topL( npx, temp.y, temp.z);
- Point3 lowR( temp.x, temp.y, npz);
- Point3 midP( npx, temp.y, npz);
- addSquare( temp, topL, midP, lowR );
- temp = topL;
- fx--;
- }
- fx = n-1;
- temp.x = p3.x;
- temp.z = npz;
- temp.y = p3.y;
- fz--;
- }
- fz = n-1;
- //reset for the bottom
- p3 = op3;
- p2 = op1;
- p1 = op1;
- p1.z = z2;
- p3.y = p1.y;
- temp = p3;
- }
- //set up for the right
- p3 = op3;
- p2 = op3;
- p1.x = p2.x;
- p3.z = z2;
- fz = n-1;
- fy = n-1;
- temp = p3;
- //right and left
- for( int x = 0; x < 2; x++ ) {
- for( int i = 0; i < n; i++ ) { //rows
- float facty = fy/n;
- float npy = (1-facty)*p1.y + facty*p2.y;
- for( int j = 0; j < n; j++ ) { //cols
- float factz = fz/n;
- float npz = (1-factz)*p2.z + factz*p3.z;
- Point3 topL( temp.x, temp.y, npz);
- Point3 lowR( temp.x, npy, temp.z);
- Point3 midP( temp.x, npy, npz);
- addSquare( temp, topL, midP, lowR );
- temp = topL;
- fz--;
- }
- fz = n-1;
- temp.x = p3.x;
- temp.z = p3.z;
- temp.y = npy;
- fy--;
- }
- fy = n-1;
- //reset for the left
- p3 = op2;
- p2 = op2;
- p2.z = z2;
- p1 = op1;
- p1.z = z2;
- temp = p3;
- }
- }
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.