By | 02.12.2016

# Implement 3-D rotation in Graphics

Write a C Program to implement 3-D rotation in Graphics. C Program to implement 3-D rotation with respect to x-axis, y-axis and z-axis (wire frame model of a cube). Use appropriate data structures to manipulate the wire frame model.

Below is the source code for C Program to implement 3-D rotation in Graphics which is successfully compiled and run on Windows System to produce desired output as shown below :

### SOURCE CODE : :

```/*  C Program to implement 3-D rotation in Graphics  */

#include <stdio.h>
#include <graphics.h>
#include <math.h>
#include <stdlib.h>
#include <dos.h>
#include <conio.h>

#define ORG -50

double face1 = {
{ 250, 125 },
{ 350, 125 },
{ 350, 225 },
{ 250, 225 },
{ 250, 125 }
};

double face2 = {
{ 250+ORG, 125-ORG },
{ 350+ORG, 125-ORG },
{ 350+ORG, 225-ORG },
{ 250+ORG, 225-ORG },
{ 250+ORG, 125-ORG }
};

double angle = 5.0 * M_PI / 180;
double midx1, midy1, midx2, midy2;

void rotate (void)
{
int i;
for (i=0; i<5; i++)
{
double xnew, ynew;

xnew = midx1 + (face1[i] - midx1) * cos (angle) -
(face1[i] - midy1) * sin (angle);
ynew = midy1 + (face1[i] - midx1) * sin (angle) +
(face1[i] - midy1) * cos (angle);

face1[i] = xnew;
face1[i] = ynew;

xnew = midx2 + (face2[i] - midx2) * cos (angle) -
(face2[i] - midy2) * sin (angle);
ynew = midy2 + (face2[i] - midx2) * sin (angle) +
(face2[i] - midy2) * cos (angle);

face2[i] = xnew;
face2[i] = ynew;
}

cleardevice();

for (i=0; i<4; i++)
{
setcolor(7);
line (face1[i], face1[i], face1[i+1], face1[i+1]);
setcolor(8);
line (face2[i], face2[i], face2[i+1], face2[i+1]);
setcolor(9);
line (face1[i], face1[i], face2[i], face2[i]);
}

delay (125);
}

void main()
{
int gd = DETECT, gm;

midx1 = (face1 + face1) / 2.0;
midy1 = (face1 + face1) / 2.0;
midx2 = (face2 + face2) / 2.0;
midy2 = (face2 + face2) / 2.0;

initgraph (&gd, &gm, "..\\bgi");

while (!kbhit())
rotate();

closegraph();
}```

Above is the source code for C Program to implement 3-D rotation in Graphics which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.

5 1 vote
Article Rating
Category: C Programming Graphic Programs Tags:  