C Program For Rotation Of An Object
We have already described what is
Rotation in previous posts. Here is the c code using graphics.h library functions.
Follow The Instructions To Successfully Run The Program:
Whenever you #include <graphics.h> in a program, you must instruct the linker to
link in certain libraries. The command to do so from Dev-C++ is Alt-P. Choose the
Parameters tab from the pop-up window and type the following into the Linker area:
-lbgi
-lgdi32
-lcomdlg32
-luuid
-loleaut32
-lole32
Note: for loops in the program is written using -std=c99 or -std=gnu99 syntex.
Code
#include <stdio.h>
#include <math.h>
#include <graphics.h>
void print(int a[][3])
{
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
}
void multiply(int a[][3], int b[][3], int c[][3])
{
int sum = 0;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 3; ++k) {
sum += (a[i][k] * b[k][j]);
}
c[i][j] = sum;
sum = 0;
}
}
}
void rotation(int a[][3])
{
int deg;
float c[3][3], sum = 0.0;
float t[3][3];
printf("Enter The Angel: ");
scanf("%d",°);
t[0][0] = t[1][1] = cos(deg*3.14/180);
t[0][1] = sin(deg*3.14/180);
t[1][0] = -t[0][1];
t[2][2] = 1; t[0][2] = t[2][0] = 0;
t[2][1] = t[1][2] = 0;
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
printf("%g ", t[i][j]);
}
printf("\n");
}
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 3; ++k) {
sum += (t[i][k] * a[k][j]);
}
c[i][j] = round(sum);
sum = 0;
}
}
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
printf("%g ", c[i][j]);
}
printf("\n");
}
line(c[0][0], c[1][0], c[0][1], c[1][1]);
line(c[0][1], c[1][1], c[0][2], c[1][2]);
line(c[0][2], c[1][2], c[0][0], c[1][0]);
}
int main()
{
initwindow(600, 600);
int a[3][3];
int x1, y1, x2, y2, x3, y3;
int ch;
printf("Enter The Initital Points: \n");
printf("Enter X1, Y1: \n");
scanf("%d%d",&x1,&y1);
printf("Enter X2, Y2: \n");
scanf("%d%d",&x2,&y2);
printf("Enter X3, Y3: \n");
scanf("%d%d",&x3,&y3);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
a[0][0] = x1;
a[1][0] = y1;
a[0][1] = x2;
a[1][1] = y2;
a[0][2] = x3;
a[1][2] = y3;
a[2][0] = a[2][1] = a[2][2] = 1;
print(a);
rotation(a);
while(!kbhit());
return 0;
}
Output