C Program to draw a rectangle and perform the operations.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <math.h>
 
void draw (int r[][2])
{
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[0][0], 240-r[0][1], 320+r[1][0], 240-r[1][1]);
line (320+r[0][0], 240-r[0][1], 320+r[3][0], 240-r[3][1]);
line (320+r[1][0], 240-r[1][1], 320+r[2][0], 240-r[2][1]);
line (320+r[2][0], 240-r[2][1], 320+r[3][0], 240-r[3][1]);
}
 
void reset (int r[][2])
{
int i;
int val[4][2] = {
{ 0, 0 },{ 100, 0 },{ 100, 50 },{ 0, 50 }
};
for (i=0; i<4; i++)
{
r[i][0] = val[i][0];
r[i][1] = val[i][1];
}
}
 
void rotate (int r[][2], int angle)
{
int i;
double ang_rad = (angle * M_PI) / 180;
for (i=0; i<4; i++)
{
double xnew, ynew;
xnew = r[i][0] * cos (ang_rad) - r[i][1] * sin (ang_rad);
ynew = r[i][0] * sin (ang_rad) + r[i][1] * cos (ang_rad);
r[i][0] = xnew;
r[i][1] = ynew;
}
}
 
void shear (int r[][2], int sx, int sy)
{
int i;
for (i=0; i<4; i++)
{
int xnew, ynew;
xnew = r[i][0] + r[i][1] * sx;
ynew = r[i][1] + r[i][0] * sy;
r[i][0] = xnew;
r[i][1] = ynew;
}
}
 
void translate (int r[][2], int dx, int dy)
{
int i;
for (i=0; i<4; i++)
{
r[i][0] += dx;
r[i][1] += dy;
}
}
 
void ini()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"..//bgi");
}
void main()
{
 
int r[4][2],angle,dx,dy,x, y,choice;
 
do
{
clrscr();
printf("1.Rotation about the origin followed by translation\n");
printf("2.Rotation about an arbitrary point\n");
printf("3.Shear about the origin\n");
printf("4.Exit\n\n");
printf("Enter your choice: ");
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);
}
Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

One thought on “C Program to draw a rectangle and perform the operations.

  1. I want to create a database 12 coloumns and rows according to entry with a permanet which can be derived after closing the program

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in