Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Parth1906
GitHub Repository: Parth1906/SPPU-2019-Pattern-SE-COMP-Computer-Graphics-Practicals
Path: blob/main/5. a) Pattern - Diamond in Rectangle.cpp
724 views
1
/*
2
5. A) Write a C++ program for drawing a
3
following pattern(diamond in rectangle)
4
*/
5
CODE:
6
#include<iostream.h>
7
#include<conio.h>
8
#include<graphics.h>
9
#include<math.h>
10
int sign(int x)
11
{
12
if(x<0)
13
return -1;
14
else if(x>0)
15
return 1;
16
else
17
return 0;
18
}
19
void bline(int x1,int y1,int x2,int y2,int col)
20
{
21
int dx,dy,e,x,y,i=1;
22
dx=x2-x1;
23
dy=y2-y1;
24
x=x1;
25
y=y1;
26
e=2*dy-dx;
27
while(i<=dx)
28
{
29
while(e>=0)
30
{
31
y++;
32
e=e-2*dx;
33
}
34
x++;
35
36
e=e+2*dy;
37
putpixel(x,y,col);
38
i++;
39
}
40
}
41
void ddaline(int x1,int y1,int x2,int y2,int
42
col)
43
{
44
int x,y,len,i;
45
float dx,dy;
46
if(x1==x2 && y1==y2)
47
putpixel(x1,y1,col);
48
else
49
{
50
dx=x2-x1;
51
dy=y2-y1;
52
if(dx>dy)
53
len=dx;
54
else
55
len=dy;
56
dx=(x2-x1)/len;
57
dy=(y2-y1)/len;
58
x=x1+0.5*sign(dx);
59
y=y1+0.5*sign(dy);
60
i=1;
61
while(i<len)
62
{
63
putpixel(x,y,col);
64
x=x+dx;
65
y=y+dy;
66
i++;
67
}
68
}
69
}
70
int main()
71
{
72
73
int ch,col,x1,x2,y1,y2;
74
int gd=DETECT,gm;
75
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
76
setbkcolor(WHITE);
77
ddaline(50,50,50,200,2); //left vert
78
ddaline(50,50,350,50,4); //up horizontal
79
ddaline(350,50,350,200,6); //right vert
80
ddaline(50,200,350,200,7); //down horizontal
81
ddaline(200,50,50,125,9); //diamond up left
82
bline(50,125,200,200,12); //diamond left,down
83
ddaline(350,125,200,200,14);//diamond down,right
84
bline(200,50,350,125,3); //diamond right,up
85
ddaline(275,87,275,163,4);//in right
86
ddaline(125,87,275,87,5);//in up
87
ddaline(125,87,125,163,6);//in left
88
ddaline(125,163,275,163,2);//in down
89
getch();
90
closegraph();
91
return 0;
92
}
93
94