Program to perform 2D rotation without using OpenGL function for rotation
Written by Munia Balayil
#include<stdio.h>
#include<math.h>
#include<GL/glut.h>
int x, y, p, q, xa, ya, ra, i, a[10], b[10], da, db;
float dx, dy, theta;
void line(int x, int y, int a, int b)
{
glVertex2d(x, y);
glVertex2d(a, b);
}
void display_rectangle()
{
for(i = 0; i < 4; i++)
{
if(i != 3)
line(a[i], b[i], a[i+1], b[i+1]);
else
line(a[i], b[i], a[0], b[0]);
}
}
void Init()
{
/* Set clear color to white */
glClearColor(1.0, 1.0, 1.0, 0);
/* Set fill color to black */
glColor3f(0.0, 0.0, 0.0);
gluOrtho2D(0 , 640 , 0 , 480);
}
void rotate()
{
// Find the vertices of the rotated rectangle
theta = (float)(ra*(3.14/180));
for(i = 0;i<4;i++)
{
a[i] = (xa + ((a[i] - xa)*cos(theta) - (b[i] - ya)*sin(theta)));
b[i] = (ya + ((a[i] - xa)*sin(theta) + (b[i] - ya)*cos(theta)));
}
}
void transform()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
// Find the original rectangle vertices
da = p - x; db = q - y;
a[0] = x; b[0] = y;
a[1] = x + da; b[1] = y;
a[2] = x + da; b[2] = y + db;
a[3] = x; b[3] = y + db;
// Draw original rectangle
display_rectangle();
rotate();
// Draw rotated rectangle
display_rectangle();
glEnd();
glFlush();
}
void main(int argc, char **argv)
{
printf("\n**********************************************\n");
printf("Enter the upper left corner of the rectangle:\n");
scanf("%d%d", &x, &y);
printf("\n**********************************************\n");
printf("Enter the lower right corner of the rectangle:\n");
scanf("%d%d", &p, &q);
printf("\n********************Rotation********************\n");
printf("Enter the value of fixed point and angle of rotation:\n");
scanf("%d%d%d", &xa, &ya, &ra);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(0, 0);
glutInitWindowSize(640, 480);
glutCreateWindow("Rotation");
Init();
glutDisplayFunc(transform);
glutMainLoop();
}
Written by Munia Balayil
No comments:
Post a Comment