Your IP : 216.73.216.40


Current Path : /var/www/html/venkat/check3/file/cg2013/pawan/
Upload File :
Current File : /var/www/html/venkat/check3/file/cg2013/pawan/ass_v3.cpp

#include <iostream>
#include <GL/freeglut.h>
#include <GL/gl.h>
#include <bits/stdc++.h>
#include <bits/stdc++.h>
#include <cmath>

#define loop(a, b, c) for(int li = a; li <= b; li+=c)
#define loop_l(a, b, c) for(int li = a; li < b; li+=c)
#define rloop(b, a, c) for(int li = b; li >= a; li-=c)
#define rloop_l(b, a, c) for(int li = b; li > a; li-=c) 
#define p(a, b) push(make_pair(a, b))
#define pb(a) push_back(a)
#define sp <<" "<<
using namespace std;

typedef struct coordinate {
	double x;
	double y;
	double z;
}coordinate;

typedef struct coordinate4 {
	double arr4[4];
}coordinate4;


void convert_3to4(coordinate &a1, coordinate4 &a2) {
	a2.arr4[0] = a1.x;
	a2.arr4[1] = a1.y;
	a2.arr4[2] = a1.z;
	a2.arr4[3] = 1.0;
}

void convert_4to3(coordinate4 &a2, coordinate &a1) {
	a1.x = a2.arr4[0];
	a1. y = a2.arr4[1];
	a1.z = a2.arr4[2];
}




void * multiply(double transform[][4], double arr_basic[]) {
	int i, j;
	double sum = 0.0;

	double C[4];
	/*double *A = transform;*/
	double *B = arr_basic;

	//double C[4] = {0, 0, 0, 0};

	for (i=0;i<4;i++) {
		sum = 0;
        for (j=0;j<4;j++) {
        	sum += transform[i][j] * *(B + j); 
        }
            C[i]=sum;
	}

	for(i = 0; i < 4; i++) {
		arr_basic[i] = C[i];
	}
    
}

int * round_vector(double arr_f[], int n, int arr_i[]) {
	loop_l(0,n,1) {
		arr_i[li] = (int)round(arr_f[li]);
	}

	return arr_i;

}


double * todouble_vector(double arr_f[], int n, int arr_i[]) {
	loop_l(0,n,1) {
		arr_f[li] = (double)(arr_i[li]);
	}

	return arr_f;

}

/*
void draw_circle(double radius) {
	for (double i = -3.14; i <= 3.14; i += 0.0001) {
		glVertex2f(r * cos(i), r * sin(i));
	}
}

*/


void translate(double *arr_basic, double *arr_inp) {
	//arr_basic    SHOULD BE   {x, y, z, 1}
	//arr_inp should be the translationtions i respective axis   i.e. X - axis = arr_inp[0] and so on
	double translate[4][4] = {{1,0,0,arr_inp[0]}, {0,1,0,arr_inp[1]}, {0,0,1,arr_inp[2]}, {0,0,0,1}};
	double (*p_stuff)[4] = translate;

	multiply(p_stuff, arr_basic);
}

void scale(double *arr_basic, double *arr_inp) {
	double translate[4][4] = {{arr_inp[0],0,0,0}, {0,arr_inp[1],0,0}, {0,0,arr_inp[2],1}, {0,0,0,1}};
	double (*p_stuff)[4] = translate;

	multiply(p_stuff, arr_basic);
}

void rotateZ(double *arr_basic, double angle) {
	double translate[4][4] = {{cos(angle),-sin(angle),0,0}, {sin(angle),cos(angle),0,0}, {0,0,1,0}, {0,0,0,1}};
	double (*p_stuff)[4] = translate;

	multiply(p_stuff, arr_basic);
}


void rotateY(double *arr_basic, double angle) {
	double translate[4][4] = {{cos(angle),0,sin(angle),0}, {0,1,0,0}, {-sin(angle),0,cos(angle),0}, {0,0,0,1}};
	double (*p_stuff)[4] = translate;

	multiply(p_stuff, arr_basic);
}


void rotateX(double *arr_basic, double angle) {
	double translate[4][4] = {{1,0,0,0}, {0,cos(angle),-sin(angle),0}, {0,sin(angle),cos(angle),0}, {0,0,0,1}};
	double (*p_stuff)[4] = translate;

	multiply(p_stuff, arr_basic);
}


int find_leftmost(int x[], int y[], int n) {
	int leftmost = 0, leftmost_x = 0;

	loop_l(0, n, 1) {
		if (li == 0)
			leftmost_x = x[li];

		if (x[li] < leftmost_x) {
			leftmost_x = x[li];
			leftmost = li;
		}
	}

	 

	return leftmost;
}


void reflectYZ(double *arr_basic) {
	double translate[4][4] = {{-1,0,0,0}, {0,1,0,0}, {0,0,1,0}, {0,0,0,1}};
	double (*p_stuff)[4] = translate;

	multiply(p_stuff, arr_basic);
}


void reflectXZ(double *arr_basic) {
	double translate[4][4] = {{1,0,0,0}, {0,-1,0,0}, {0,0,1,0}, {0,0,0,1}};
	double (*p_stuff)[4] = translate;

	multiply(p_stuff, arr_basic);
}

void reflectXY(double *arr_basic) {
	double translate[4][4] = {{1,0,0,0}, {0,1,0,0}, {0,0,-1,0}, {0,0,0,1}};
	double (*p_stuff)[4] = translate;

	multiply(p_stuff, arr_basic);
}





//----------- above transformations
int n;
int k_operations;
int d;

double max_x, min_x;
double max_y, min_y;



void draw(coordinate list[])
{
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0.0, 1.0, 0.0);
	glOrtho(min_x - 500, max_x + 500 , min_y - 500, max_y + 500, -1400, 1400);
	glBegin(GL_LINES);
	
	loop_l(n * k_operations,n *(k_operations + 1),1) {
		cout << (int)list[li].x sp (int)list[li].y sp (int)list[li].z << endl;
		glVertex3f(list[li].x, list[li].y, list[li].z);
	}
	//glEnd();
	//glFlush();
}



void draw1()
{
	//glClearColor(0.0, 0.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0, 1.0, 0.0);
	//glOrtho(-1000.0, 10.0, -10.0, 10.0, -0.1, 0.1);
	glEnd();
	glFlush();
}

void plot(coordinate list[]) {

	draw(list);
	
}


void force_display() {
	glutDisplayFunc(draw1);
	glutMainLoop();
}


void initiate() {
	glutInitDisplayMode(GLUT_SINGLE);
	glutInitWindowSize(800, 800);
	glutInitWindowPosition(100, 100);
	glutCreateWindow("Rectangle");
}



void reflect(coordinate list[], coordinate list_bck[], int operation_num) {
	int operation_upper_bound = operation_num + 1;
	coordinate4 temp;

	cout << "REFLECT" << endl;

	loop_l(n * operation_num, n * operation_upper_bound, 1) {
		convert_3to4(list[li - n * operation_num], temp);
		//cout << temp.arr4[0] sp temp.arr4[1] sp temp.arr4[2] sp temp.arr4[3] << endl;
		reflectYZ(temp.arr4);
		convert_4to3(temp, list[li]);
		//cout << list[li].x << "  " << list[li].y <<  " " <<  list[li].z << endl;


		if (max_x < list[li].x) {
			max_x = list[li].x;
		}

		if (min_x > list[li].x) {
			min_x = list[li].x;
		}

		if (max_y < list[li].y) {
			max_y = list[li].y;
		}

		if (min_y > list[li].y) {
			min_y = list[li].y;
		}
	}

}


void translate(coordinate list[], coordinate list_bck[], double x, double y, double z, int operation_num) {
	int operation_upper_bound = operation_num + 1;
	double arr_vertex[] = {x, y, z};
	coordinate4 temp;

	//cout << "TRANSALTE" << endl;

	int a = n * operation_num;
	int b = n * operation_upper_bound;
	//cout << a sp b;

	loop_l(n * operation_num, n * operation_upper_bound, 1) {
		convert_3to4(list[li - n], temp);
		//cout << temp.arr4[0] sp temp.arr4[1] sp temp.arr4[2] sp temp.arr4[3] << endl;
		translate(temp.arr4, arr_vertex);
		convert_4to3(temp, list[li]);
		//cout << list[li].x << "  " << list[li].y <<  " " <<  list[li].z << endl;

		
	if (max_x < list[li].x) {
			max_x = list[li].x;
		}

		if (min_x > list[li].x) {
			min_x = list[li].x;
		}

		if (max_y < list[li].y) {
			max_y = list[li].y;
		}

		if (min_y > list[li].y) {
			min_y = list[li].y;
		}
	}

}




void rotate(coordinate list[], coordinate list_bck[], double angle, int operation_num) {
	int operation_upper_bound = operation_num + 1;

	coordinate4 temp;

	//cout << "ROTATE" << endl;

	loop_l(n * operation_num, n * operation_upper_bound, 1) {
		convert_3to4(list[li -  n], temp);
		//cout << temp.arr4[0] sp temp.arr4[1] sp temp.arr4[2] sp temp.arr4[3] << endl;
		rotateX(temp.arr4, angle);
		convert_4to3(temp, list[li]);
		/*list[li].x = list[li].x / list[li].z * 200;
		list[li].y = list[li].y / list[li].z * 200;
		list[li].z = 200;*/
		//cout << list[li].x << "  " << list[li].y <<  " " <<  list[li].z << endl;


		if (max_x < list[li].x) {
			max_x = list[li].x;
		}

		if (min_x > list[li].x) {
			min_x = list[li].x;
		}

		if (max_y < list[li].y) {
			max_y = list[li].y;
		}

		if (min_y > list[li].y) {
			min_y = list[li].y;
		}
	}

}


int main(int argc, char** argv)
{	
	//freopen("input2.txt", "r", stdin);
	int i;
	int n1 = 18;
	n = n1 + 10;
	k_operations = 2;
	coordinate4 temp;
	d = 10;
	//cin >> n;
	//cin >> k_operations;

	int arr[]= {-100,700,
-100,500,
-100,500,
100,500,
100,500,
100,700,
100,700,
-100,700,
-50,0,
-50,500,
-150,0,
-500,400,
-500,400,
-100,700,
-50,0,
-400,400,
-400,400,
-100,600};



	coordinate list[(k_operations + 1) * n];
	coordinate list_bck[n];

	double PI = acos(-1);


	loop_l(0,n1 - 10,1) {
		//cin >> list[li].x >> list[li].y;

		list[li].x = arr[li * 2 + 0];
		list[li].y = arr[li * 2 + 1];
		list[li].z = 0.0;
		list_bck[li].x = list[li].x;
		list_bck[li].y = list[li].y;
		list_bck[li].z = list[li].z;
		
		


		if (max_x < list_bck[li].x) {
			max_x = list_bck[li].x;
		}

		if (min_x > list_bck[li].x) {
			min_x = list_bck[li].x;
		}

		if (max_y < list_bck[li].y) {
			max_y = list_bck[li].y;
		}

		if (min_y > list_bck[li].y) {
			min_y = list_bck[li].y;
		}



		

		//cout << list[li].x << "  " << list[li].y <<  " " <<  list[li].z << endl;
	}

	loop_l(n1 - 10,n1,1) {
		//cin >> list[li].x >> list[li].y;
		
		list[li].x = arr[li * 2 + 0];
		list[li].y = arr[li * 2 + 1];
		list[li].z = 0.0;
		list_bck[li].x = list[li].x;
		list_bck[li].y = list[li].y;
		list_bck[li].z = list[li].z;


		convert_3to4(list[li], temp);
		//cout << temp.arr4[0] sp temp.arr4[1] sp temp.arr4[2] sp temp.arr4[3] << endl;
		reflectYZ(temp.arr4);
		convert_4to3(temp, list[li + 10]);
		//cout << list[li].x << "  " << list[li].y <<  " " <<  list[li].z << endl;


		if (li == 0) {
			max_x = min_x = list_bck[0].x;
			max_y = min_y = list_bck[0].y;
		} 

		if (max_x < list_bck[li].x) {
			max_x = list_bck[li].x;
		}

		if (min_x > list_bck[li].x) {
			min_x = list_bck[li].x;
		}

		if (max_y < list_bck[li].y) {
			max_y = list_bck[li].y;
		}

		if (min_y > list_bck[li].y) {
			min_y = list_bck[li].y;
		}

		//cout << list[li ].x << "  " << list[li].y <<  " " <<  list[li].z << endl;

		if (max_x < list_bck[li + 10].x) {
			max_x = list_bck[li + 10].x;
		}

		if (min_x > list_bck[li + 10].x) {
			min_x = list_bck[li + 10].x;
		}

		if (max_y < list_bck[li + 10].y) {
			max_y = list_bck[li + 10].y;
		}

		if (min_y > list_bck[li + 10].y) {
			min_y = list_bck[li + 10].y;
		}

	}

	/*loop_l(n1,n,1) {
		cout << list[li ].x << "  " << list[li].y <<  " " <<  list[li].z << endl;
	}*/

	
	//reflect(list, list_bck, 1);

	//translate(list, list_bck, -100, 10, sqrt(100 * 100 + 100), 1);
	

	translate(list, list_bck, 0, -100, 10, 1);

	double angle = PI / 2 - atan(10.0 / 100);

	rotate(list, list_bck, angle, 2);

	//cout << max_x sp max_y sp min_x sp min_y ; 
	glutInit(&argc, argv);
	initiate();
	plot(list);
	force_display();

	return 0;
}