Professional Documents
Culture Documents
5th Week Lab Program-Rotate A Triangle About Origin and Fixed Point
5th Week Lab Program-Rotate A Triangle About Origin and Fixed Point
basic object
(Create and rotate a Triangle about origin and fixed point)
Rotation
Rotation of object by an angle ϴ
Consider a point (x,y) to rotated about the origin, to a new
location (x’,y’) with an angle theta.
The polar form of (x,y) and (x’,y’)
x = r cos y = r sin
x’ = r cos (+) y’ = r sin (+)
x’ = r cos cos - r sin sin
= x cos - y sin
y’ = r cos sin - r sin
cos
= x sin + y cos
x' cos cos
sin y
sin
y ' 49
Two types of 2D rotations
• 1.Rotation about origin
• 2.Rotation about pivot point or fixed point
2D Rotation about the origin.
x r.cos( ) r.cos.cos
Substituting for r :
x r.cos
y r.sin
Gives us :
x x.cos y.sin
2D Rotation about the origin.
x x.cos y.sin
y x.sin y.cos
Rewriting in matrix
form gives us : .
sin cos y
y
x cos
sin
sin x cos ,
Define the matrix R
sin cos P R P
4.Develop a program to demonstrate 2D transformation on basic objects
#define BLACK
#include<stdio.h>
#include<math.h>
#include<GL/glut.h>
GLfloat Triangle[3][3]={{100.0,250.0,175.0},{100.0,100.0,300.0},{1.0,1.0,1.0}};
GLfloat rotatement[3][3]={{0},{0},{0}};
GLfloat Result[3][3]={{0},{0},{0}};
GLfloat m=0;
GLfloat n=0;
float theta;
void Triangle()
{
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(Triangle[0][0],Triangle[1][0]);
glVertex2f(Triangle[0][1],Triangle[1][1]);
glVertex2f(Triangle[0][2],Triangle[1][2]);
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
Triangle();
glTranslatef(m,n,0);
glRotatef(theta,0,0,1);
glTranslatef(-m,-n,0);
Triangle();
glFlush();
}
void myinit()
{
glClearColor(1,1,1,1);
glColor3f(1,0,0);
glPointSize(1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,449,0,499);
}
int main(int argc, char** argv)
{
int ch;
printf("enter your choice \n1: Rotation about origin \n2: Rotation about a Fixed point\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the rotation angle in degree :");
scanf("%f", &rotation_angle);
rotation_angle= (3.14 * rotation_angle) / 180;
rotate();
break;
case 2: printf("Enter the fixed points :");
scanf("%f%f", &arbitrary_x,&arbitrary_y);
printf("Enter rotation angle in degree :");
scanf("%f", &rotation_angle);
rotation_angle= (3.14 * rotation_angle) / 180;
rotate();
break;
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("triangle rotation");
glutDisplayFunc(display);
myinit();
glutMainLoop();
return 0;
}
Enter your choice