By | 02.12.2016

# Draw rectangle and perform operations

Write a C Program to draw rectangle and perform operations in Graphics. Here’s simple C Program to draw rectangle and perform operations in Graphics in C Programming Language.

C Program to draw a rectangle and perform the following operations ::

a. Rotation about the origin followed by translation.

b. Rotation about an arbitrary point.

c. Apply X shear and Y shear on the rectangle.

Below is the source code for C Program to draw rectangle and perform operations in Graphics which is successfully compiled and run on Windows System to produce desired output as shown below :

### SOURCE CODE : :

```/*  C Program to draw rectangle and perform operations in Graphics  */

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

void draw (int r[])
{
int i;
setlinestyle (DOTTED_LINE, 0, 1);
line (320, 0, 320, 480);
line (0, 240, 640, 240);

setlinestyle (SOLID_LINE, 0, 1);
line (320+r, 240-r, 320+r, 240-r);
line (320+r, 240-r, 320+r, 240-r);
line (320+r, 240-r, 320+r, 240-r);
line (320+r, 240-r, 320+r, 240-r);
}

void reset (int r[])
{
int i;
int val = {
{ 0, 0 },{ 100, 0 },{ 100, 50 },{ 0, 50 }
};
for (i=0; i<4; i++)
{
r[i] = val[i];
r[i] = val[i];
}
}

void rotate (int r[], int angle)
{
int i;
double ang_rad = (angle * M_PI) / 180;
for (i=0; i<4; i++)
{
double xnew, ynew;
r[i] = xnew;
r[i] = ynew;
}
}

void shear (int r[], int sx, int sy)
{
int i;
for (i=0; i<4; i++)
{
int xnew, ynew;
xnew = r[i] + r[i] * sx;
ynew = r[i] + r[i] * sy;
r[i] = xnew;
r[i] = ynew;
}
}

void translate (int r[], int dx, int dy)
{
int i;
for (i=0; i<4; i++)
{
r[i] += dx;
r[i] += dy;
}
}

void ini()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"..//bgi");
}
void main()
{

int r,angle,dx,dy,x, y,choice;

do
{
clrscr();
printf("1.Rotation about the origin followed by translation\n");
printf("4.Exit\n\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the rotation angle: ");
scanf("%d", &angle);
printf("Enter the x- and y-coordinates for translation: ");
scanf("%d%d",&dx,&dy);
ini();
cleardevice();
reset(r);
draw(r);getch();
rotate(r, angle);
cleardevice();
draw(r);getch();
translate(r,dx,dy);
cleardevice();
draw(r);getch();
closegraph();
break;
case 2: printf("Enter the rotation angle: ");
scanf("%d",&angle);
printf("Enter the x- and y-coordinates of the point: ");
scanf("%d%d",&x,&y);
ini();
cleardevice();
reset(r);
translate(r,x,y);
draw(r);
putpixel(320+x,240-y,WHITE);
getch();
translate(r,-x,-y);
draw(r);getch();
rotate(r,angle);
draw(r);getch();
translate(r,x,y);
cleardevice();
draw(r);
putpixel(320+x,240-y,WHITE);
getch();
closegraph();
break;
case 3: printf("Enter the x- and y-shears: ");
scanf("%d%d",&x,&y);
ini();
reset(r);
draw(r);getch();
shear(r, x, y);
cleardevice();
draw (r);getch();
closegraph();
break;
case 4: closegraph();
}
}while(choice!=4);
}```

Above is the source code for C Program to draw rectangle and perform operations 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. 