| Current Path : /var/www/html/venkat/check3/file/cg2013/pawan/ |
| Current File : /var/www/html/venkat/check3/file/cg2013/pawan/rit2013060.cpp |
#include<iostream>
#include<cstdlib>
#include<cmath>
#define A (80 * M_PI)/180
using namespace std;
struct point {
float x;
float y;
float z;
};
struct triangle{
struct point p1;
struct point p2;
struct point p3;
};
int round(float x) {
if(x > 0)
return (int)(x+0.5);
else
return (int)(x-0.5);
}
void printPoint(point p) {
cout << round(p.x) << " " << round(p.y) << " " << round(p.z) << endl;
}
void print(triangle t1) {
printPoint(t1.p1);
printPoint(t1.p2);
printPoint(t1.p3);
cout << endl;
}
void rotateX(triangle *t1) {
triangle t;
point p;
p.x = (float)t1->p1.x;
p.y = (float)(t1->p1.y)* (float)cos(A) - (float)(t1->p1.z) *(float) sin(A);
p.z = (float)(t1->p1.y)*(float)sin(A) + (float)(t1->p1.z) * (float)cos(A);
t1->p1 = p;
p.x = (float)t1->p2.x;
p.y = (float)(t1->p2.y)*(float)cos(A) - (float)(t1->p2.z) * (float)sin(A);
p.z = (float) (t1->p2.y)*(float)sin(A) + (float) (t1->p2.z) * (float)cos(A);
t1->p2 = p;
p.x = (float)t1->p3.x;
p.y = (float)(t1->p3.y)*(float)cos(A) -(float) (t1->p3.z) *(float) sin(A);
p.z = (float)(t1->p3.y)*(float) sin(A) + (float)(t1->p3.z) *(float) cos(A);
t1->p3 = p;
}
void perspective(triangle *t1) {
t1->p1.z = t1->p1.z/2;
t1->p2.z = t1->p2.z/2;
t1->p3.z = t1->p3.z/2;
t1->p1.x =(t1->p1.x)/t1->p1.z;
t1->p2.x =(t1->p1.x)/t1->p2.z;
t1->p3.x =(t1->p1.x)/t1->p3.z;
t1->p1.y = (t1->p1.y)/t1->p1.z;
t1->p2.y = (t1->p1.y)/t1->p2.z;
t1->p3.y = (t1->p1.y)/t1->p3.z;
}
int main() {
struct point p1,p2,p3;
struct triangle outerT, innerT1,innerT2;
p1.x = -25.0, p1.y = -20.0, p1.z = 0.0;
p2.x = -25.0, p2.y = 20.0, p2.z = 0.0;
p3.x = 0.0, p3.y = -40.0, p3.z = 0.0;
outerT.p1 = p1, outerT.p2 = p2, outerT.p3 = p3;
p1.x = -10.0, p1.y = 10.0, p1.z = 0.0;
p2.x = -6.0, p2.y = 10.0, p2.z = 0.0;
p3.x = -6.0, p3.y = -20.0, p3.z = 0.0;
innerT1.p1 = p1, innerT1.p2 = p2, innerT1.p3 = p3;
p1.x = 6.0, p1.y = 10.0, p1.z = 0.0;
p2.x = 10.0, p2.y = 10.0, p2.z = 0.0;
p3.x = 6.0, p3.y = -20.0, p3.z = 0.0;
innerT2.p1 = p1, innerT2.p2 = p2, innerT2.p3 = p3;
cout << "Original Points" << endl;
print(outerT);
print(innerT1);
print(innerT2);
rotateX(&outerT);
rotateX(&innerT1);
rotateX(&innerT2);
cout << endl;
cout << "After Rotation " << endl;
print(outerT);
print(innerT1);
print(innerT2);
perspective(&outerT);
perspective(&innerT1);
perspective(&innerT2);
cout << endl;
cout << "After Perspective " << endl;
print(outerT);
print(innerT1);
print(innerT2);
return 0;
}