C++ Programming Code Examples
C++ > Computer Graphics Code Examples
A Graphics Illustration Program
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/* A Graphics Illustration Program */
#include<stdio.h>
#include<iostream.h>
#include<dos.h>
#include<process.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
//void render(float,float,float, float,float,float, float,float,float,
float,float,float);
void initialize(void);
void firstpage(void);
void call_first(void);
float intensity,alpha,thita,tempy,tempz,tempx;
char ch='4';
char ch1='1';
char ch2='1';
int pts1[5][3];
float tx,ty,tz,d=.5;
void assign(float,float,float,float,float,float,float,float,float);
void scan_line(float,float,float,float,float,float,float,float,float);
void drawpyramid(float,float,float,float,float,float);
void call_assign(void);
void display(void);
void tranform(void);
void draw(void);
void drawscale(void);
float pts[5][3]={-100,0,0, 0,0,45, 100,0,0, 0,0,-45, 0,130,0};
float pts2[5][3]={228,273,0, 305,295,0, 428,273,0, 350,250,0 ,328,143,0};
float pt[5][3]={-100,0,0, 0,0,45,100,0,0,0,0-45,0,130,0};
void main()
{
int i;
float sx,sy,sz=1;
struct palettetype pal;
int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"d:\backups\borlandc\bgi");
getpalette(&pal);
firstpage();
for(i=16;i>0;i--)
setrgbpalette(pal.colors[i],0,4*i,0);
L1: display();
while(ch1!='4')
{
ch='2';
L2: call_assign();
clearviewport();
gotoxy(1,2);
cout<<"1. Translation
";
cout<<"2. Rotation
";
cout<<"3. Scaling
";
cout<<"4. Back
";
ch1=getch();
if(ch1=='4')
{
clearviewport();
goto L1;
}
if(ch1=='1')
{
clearviewport();
while(ch1!='4')
{
gotoxy(2,2);
cout<<"a. X+
"; cout<<" b. X-
";
cout<<" c. Y+
"; cout<<" d. Y-
";
cout<<" e. Z+
"; cout<<" f. Z-
";
cout<<" g. Back";
call_assign();
ch1=getch();
clearviewport();
if(ch1=='g')
goto L2;
if(ch1=='a')
tx=5;
if(ch1=='b')
tx=-5;
if(ch1=='c')
ty=5;
if(ch1=='d')
ty=-5;
if(ch1=='e')
tz=10;
if(ch1=='f')
tz=-10;
for(i=0;i<5;i++)
{
pts[i][0]+=tx;
pts[i][1]+=ty;
pts[i][1]+=tz;
}
}
}
if(ch1=='3')
{
clearviewport();
cout<<"Enter sx:";
cin>>sx;
cout<<"Enter sy:";
cin>>sy;
for(i=0;i<5;i++)
{
pts2[i][0]=abs(pts2[i][0]*sx+200*(1-sx));
pts2[i][1]=abs(pts2[i][1]*sy+200*(1-sy));
}
drawscale();
getch();
}
if(ch1=='2')
{
while(ch2!='4')
{
clearviewport();
gotoxy(1,2);
cout<<"1.X-axis rotation
";
gotoxy(1,3);
cout<<"2.Y-axis rotation
";
gotoxy(1,4);
cout<<"3.Z-axis rotation
";
gotoxy(1,5);
cout<<"4.Back";
ch2=getch();
if(ch2=='4')
break;
if(ch2=='1')
{
alpha=0;
while(alpha<360)
{
alpha=alpha+10;
thita=(alpha*3.142)/180;
initialize();
for(i=0;i<5;i++)
{
tempy=(pts1[i][1]*cos(thita)+pts1[i][2]*sin(thita));
pts1[i][2]=(pts1[i][1]*sin(thita)-pts1[i][2]*cos(thita));
pts1[i][1]=tempy;
}
clearviewport();
draw();
delay(100);
}
}
if(ch2=='2')
{
alpha=0;
while(alpha<360)
{
alpha=alpha+10;
thita=(alpha*3.142)/180;
initialize();
for(i=0;i<5;i++)
{
tempz=(pts1[i][2]*cos(thita)+pts1[i][0]*sin(thita));
pts1[i][0]=(pts1[i][2]*sin(thita)-pts1[i][0]*cos(thita));
pts1[i][2]=tempz;
}
clearviewport();
draw();
delay(100);
}
}
if(ch2=='3')
{
alpha=0;
while(alpha<360)
{
alpha=alpha+10;
thita=(alpha*3.142)/180;
initialize();
for(i=0;i<5;i++)
{
tempx=(pts1[i][0]*cos(thita)-pts1[i][1]*sin(thita));
pts1[i][1]=(pts1[i][0]*sin(thita)+pts1[i][1]*cos(thita));
pts1[i][0]=tempx;
}
clearviewport();
draw();
delay(100);
clearviewport();
draw();
}
}
}
}
}
closegraph();
restorecrtmode();
}
void initialize()
{
pts1[0][0]=-100;
pts1[0][1]=-65;
pts1[0][2]=0;
pts1[1][0]=0;
pts1[1][1]=-65;
pts1[1][2]=-45;
pts1[2][0]=100;
pts1[2][1]=-65;
pts1[2][2]=0;
pts1[3][0]=0;
pts1[3][1]=-65;
pts1[3][2]=45;
pts1[4][0]=0;
pts1[4][1]=65;
pts1[4][2]=0;
}
void firstpage()
{
clearviewport();
setcolor(WHITE);
settextstyle(2,HORIZ_DIR,5);
outtextxy(250,15,"A Project on");
setcolor(GREEN);
settextstyle(3,HORIZ_DIR,4);
outtextxy(170,25,"PYRAMID MODELING");
rectangle(300,120,580,320);
rectangle(295,115,585,325);
setcolor(6);
settextstyle(4,HORIZ_DIR,3);
outtextxy(50,100, "OPTIONS");
settextstyle(3,HORIZ_DIR,1);
setcolor(11);
outtextxy(20,150,"1. VISIBLE SURFACE DETECTION");
outtextxy(20,190,"2. SURFACE RENDERING");
outtextxy(20,230,"3. TRANSFORMATIONS");
outtextxy(20,270,"4. WIREFRAME DISPLAY");
outtextxy(20,310,"5. EXIT");
settextstyle(2,HORIZ_DIR,4);
outtextxy(400,370,"Group Memebers");
setcolor(YELLOW);
call_first();
//display();
setcolor(WHITE);
getch();
cleardevice();
clearviewport();
}
void display(void)
{ while(ch!='3')
{ clearviewport();
gotoxy(2,2);
cout<<"1. Visible Surface Detection
";
gotoxy(2,3);
cout<<"2. Surface Rendering
";
gotoxy(2,4);
cout<<"3. Transformations";
gotoxy(2,5);
cout<<"4. Wireframe Display
";
gotoxy(2,6);
cout<<"5. Exit
";
call_assign();
ch=getch();
if(ch=='5')
exit(0);
clearviewport();
if(ch=='3')
break;
}
}
void call_assign(void)
{
assign(pts[0][0],pts[0][1],pts[0][2],pts[1][0],pts[1][1],pts[1][2],pts[4][
0],pts[4][1],pts[4][2]);
assign(pts[1][0],pts[1][1],pts[1][2],pts[2][0],pts[2][1],pts[2][2],pts[4][
0],pts[4][1],pts[4][2]);
assign(pts[2][0],pts[2][1],pts[2][2],pts[3][0],pts[3][1],pts[3][2],pts[4][
0],pts[4][1],pts[4][2]);
assign(pts[0][0],pts[0][1],pts[0][2],pts[4][0],pts[4][1],pts[4][2],pts[3][
0],pts[3][1],pts[3][2]);
}
void call_first(void)
{
assign(pt[0][0],pt[0][1],pt[0][2],pt[1][0],pt[1][1],pt[1][2],pt[4][0],pt[4
][1],pt[4][2]);
assign(pt[1][0],pt[1][1],pt[1][2],pt[2][0],pt[2][1],pt[2][2],pt[4][0],pt[4
][1],pt[4][2]);
assign(pt[2][0],pt[2][1],pt[2][2],pt[3][0],pt[3][1],pt[3][2],pt[4][0],pt[4
][1],pt[4][2]);
assign(pt[0][0],pt[0][1],pt[0][2],pt[4][0],pt[4][1],pt[4][2],pt[3][0],pt[3
][1],pt[3][2]);
}
void drawpyramid(float x1,float y1,float x2,float y2,float x3,float y3)
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void assign(float x1,float y1,float z1,float x2,float y2,float z2,float
x3,float y3,float z3)
{
float A,B,C;
float temp,An,Bn,Cn,X,Y,Z;
float Xl=-6,Yl=10,Zl=50;
float templ;
A=y1*(z2-z3)+y2*(z3-z1)+y3*(z1-z2);
B=z1*(x2-x3)+z2*(x3-x1)+z3*(x1-x2);
C=x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2);
temp=sqrt(A*A+B*B+C*C);
templ=sqrt(Xl*Xl+Yl*Yl+Zl*Zl);
X=(float)Xl/templ; Y=(float)Yl/templ; Z=(float)Zl/templ;
An=(A/temp); Bn=(float)B/temp; Cn=(float)C/temp;
intensity=15*(An*X+Bn*Y+Cn*Z);
if (intensity<0)
intensity=0;
if (intensity>15)
intensity=15;
z1=55-z1;
x1=x1+300+(d*z1); y1=300-y1-(d*z1);
z2=55-z2;
x2=x2+300+(d*z2); y2=300-y2-(d*z2);
z3=55-z3;
x3=x3+300+(d*z3); y3=300-y3-(d*z3);
if(ch=='1')
{ if(intensity==0) return;
drawpyramid(x1,y1,x2,y2,x3,y3);
return;
}
if(ch=='3')
exit(0);
if(ch=='4')
drawpyramid(x1,y1,x2,y2,x3,y3);
if(ch=='2')
{
if(intensity==0) return;
if ((y1>y2) && (y1>y3) && (y2>y3))
scan_line(x1,y1,z1,x2,y2,z2,x3,y3,z3);
if ((y1>y2) && (y1>y3) && (y3>y2))
scan_line(x1,y1,z1,x3,y3,z3,x2,y2,z2);
if ((y2>y1) && (y2>y3) && (y1>y3))
scan_line(x2,y2,z2,x1,y1,z1,x3,y3,z3);
if ((y2>y1) && (y2>y3) && (y3>y1))
scan_line(x2,y2,z2,x3,y3,z3,x1,y1,z1);
if ((y3>y1) && (y3>y2) && (y1>y2))
scan_line(x3,y3,z3,x1,y1,z1,x2,y2,z2);
if ((y3>y1) && (y3>y2) && (y2>y1))
scan_line(x3,y3,z3,x2,y2,z2,x1,y1,z1);
}
}
void scan_line(float x1,float y1,float z1,float x2,float y2,float z2,float
x3,float y3,float z3)
{
int i;
float tempx,tempx1,tempy;
float m1,m2,thita,alpha;
alpha=0;
tempx=x1; tempx1=x1; tempy=y1;
m1=(y2-y1)/(x2-x1);
m2=(y3-y1)/(x3-x1);
while((int)tempy!=(int)y2)
{ alpha=alpha+5;
thita=(alpha*3.14/180);
tempx=tempx-1/m1;
tempx1=tempx1-1/m2;
if(tempx<tempx1)
{
for(i=0;i+tempx<=tempx1;i++)
{
putpixel(tempx+i,tempy,intensity);
}
}
else
if (tempx1<tempx)
{ for(i=0;i+tempx1<=tempx;i++)
{
putpixel(tempx1+i,tempy,intensity);
}
}
tempy--;
}
m1=(float)(y3-y2)/(x3-x2);
while((int)tempy!=(int)y3)
{
tempx=tempx-1/m1;
tempx1=tempx1-1/m2;
if(tempx<tempx1)
{
for(i=0;i+tempx<=tempx1;i++)
putpixel(tempx+i,tempy,intensity);
}
else
{
for(i=0;i+tempx1<=tempx;i++)
putpixel(tempx1+i,tempy,intensity);
}
tempy--;
}
}
void draw()
{ int i;
for(i=0;i<5;i++)
{
pts1[i][2]=50+pts1[i][2]+50;
pts1[i][0]=pts1[i][0]+300+.5*pts1[i][2];
pts1[i][1]=200+65-pts1[i][1]-.5*pts1[i][2];
}
line(pts1[0][0],pts1[0][1],pts1[1][0],pts1[1][1]);
line(pts1[1][0],pts1[1][1],pts1[2][0],pts1[2][1]);
line(pts1[2][0],pts1[2][1],pts1[3][0],pts1[3][1]);
line(pts1[3][0],pts1[3][1],pts1[0][0],pts1[0][1]);
line(pts1[0][0],pts1[0][1],pts1[4][0],pts1[4][1]);
line(pts1[1][0],pts1[1][1],pts1[4][0],pts1[4][1]);
line(pts1[2][0],pts1[2][1],pts1[4][0],pts1[4][1]);
line(pts1[3][0],pts1[3][1],pts1[4][0],pts1[4][1]);
}
void drawscale()
{
line(pts2[0][0],pts2[0][1],pts2[1][0],pts2[1][1]);
line(pts2[1][0],pts2[1][1],pts2[2][0],pts2[2][1]);
line(pts2[2][0],pts2[2][1],pts2[3][0],pts2[3][1]);
line(pts2[3][0],pts2[3][1],pts2[0][0],pts2[0][1]);
line(pts2[0][0],pts2[0][1],pts2[4][0],pts2[4][1]);
line(pts2[1][0],pts2[1][1],pts2[4][0],pts2[4][1]);
line(pts2[2][0],pts2[2][1],pts2[4][0],pts2[4][1]);
line(pts2[3][0],pts2[3][1],pts2[4][0],pts2[4][1]);
}
The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.
To create a program in Graphics Mode, the first step would be to include the header file graphics.h. This file is required for Graphics programming. After this, the graphics have to be initialized. C Language supports 16 Bit's MS-DOS environment. Initializing the Graphics mode is to call various functions, one such is called initgraph. initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver), and putting the system into graphics mode. To start the graphics system, first call the initgraph function. initgraph loads the graphics driver and puts the system into graphics mode. You can tell initgraph to use a particular graphics driver and mode, or to autodetect the attached video adapter at run time and pick the corresponding driver. If you tell initgraph to autodetect, it calls detectgraph to select a graphics driver and mode. initgraph also resets all graphics settings to their defaults (current position, palette, color, viewport, and so on)
Compute square root. Returns the square root of x. The sqrt() function in C++ returns the square root of a number. This function is defined in the cmath header file. There are various functions available in the C++ Library to calculate the square root of a number. Most prominently, sqrt is used. It takes double as an argument. The <cmath> header defines two more inbuilt functions for calculating the square root of a number (apart from sqrt) which has an argument of type float and long double. Therefore, all the functions used for calculating square root in C++ are. Mathematically, sqrt(x) = √x.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.
delay() function is used to hold the program's execution for given number of milliseconds, it is declared in dos.h header file. There can be many instances when we need to create a delay in our programs. C++ provides us with an easy way to do so. We can use a delay() function for this purpose in our code. We can run the code after a specific time in C++ using delay() function.
outtextxy displays a text string in the viewport at the given position (x, y), using the current justification settings and the current font, direction, and size. To maintain code compatibility when using several fonts, use textwidth and textheight to determine the dimensions of the string. If a string is printed with the default font using outtext or outtextxy, any part of the string that extends outside the current viewport is truncated. outtextxy is for use in graphics mode; it will not work in text mode.
In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.
restorecrtmode restores the original video mode detected by initgraph. This function can be used in conjunction with setgraphmode to switch back and forth between text and graphics modes. Textmode should not be used for this purpose; use it only when the screen is in text mode, to change to a different text mode. restorecrtmode is implemented in winbgim, but it does not do any work. This is because both the graphics window and the text window are always open during any Windows program, so there is no need to switch back and forth between the two modes.
Settextstyle function is used to change the way in which text appears, using it we can modify the size of text, change direction of text and change the font of text. settextstyle sets the text font, the direction in which text is displayed, and the size of the characters. A call to settextstyle affects all text output by outtext and outtextxy.
The exit function terminates the program normally. Automatic objects are not destroyed, but static objects are. Then, all functions registered with atexit are called in the opposite order of registration. The code is returned to the operating system. An exit code of 0 or EXIT_SUCCESS means successful completion. If code is EXIT_FAILURE, an indication of program failure is returned to the operating system. Other values of code are implementation-defined. Calls all functions registered with the atexit() function, and destroys C++ objects with static storage duration, all in last-in-first-out (LIFO) order. C++ objects with static storage duration are destroyed in the reverse order of the completion of their constructor. (Automatic objects are not destroyed as a result of calling exit().)
In C++, goto is a jump statement and sometimes also referred as unconditional jump statement. It can be used to jump from goto to a labeled statement within the same function. The target label must be within the same file and context. Please note that the use of goto statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, making hard to understand and modify the program.
Break statement in C++ is a loop control statement defined using the break keyword. It is used to stop the current execution and proceed with the next one. When a compiler calls the break statement, it immediately stops the execution of the loop and transfers the control outside the loop and executes the other statements. In the case of a nested loop, break the statement stops the execution of the inner loop and proceeds with the outer loop. The statement itself says it breaks the loop. When the break statement is called in the program, it immediately terminates the loop and transfers the flow control to the statement mentioned outside the loop.
rectangle() is used to draw a rectangle. Coordinates of left top and right bottom corner are required to draw the rectangle. left specifies the X-coordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner, bottom specifies the Y-coordinate of right bottom corner.
Positions cursor in text window. The gotoxy() function places the cursor at the desired location on the screen. This means it is possible to change the cursor location on the screen using the gotoxy() function. It is basically used to print text wherever the cursor is moved. If the coordinates are in any way invalid the call to gotoxy is ignored. Neither argument to gotoxy can be zero.
detectgraph() is a graphics system control function which checks the system hardware then selects a graphics driver and mode. detectgraph() works in two different ways: if initgraph() is called with 'graphdriver' set to DETECT, initgraph() calls detectgraph() to select a graphics driver and mode. detectgraph() can also be called directly to determine the graphics driver and mode. detectgraph is used to find out the current graphics driver and mode. The outcome of this function is used in initgraph function as input arguments. initgraph function is used to initialize with the graphics library and changes to the graphics screen for drawing. It is the first step you need to do during graphics programming.
As the name already suggests, these operators help in assigning values to variables. These operators help us in allocating a particular value to the operands. The main simple assignment operator is '='. We have to be sure that both the left and right sides of the operator must have the same data type. We have different levels of operators. Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language:
Compute cosine. Returns the cosine of an angle of x radians. cos() function is a library function of cmath header, it is used to find the cosine of the given number (angle), it accepts a number (x) and returns the cosine of angle x radians. In trigonometry, the cos function of a right-angled triangle is defined as the length of the adjacent side over the longest side, i.e., the hypotenuse. The cos function in C++ works precisely like the cosine function in trigonometry. The return value of the cos function is the cosine of an angle given in radian. Function returns cosine of x radians.
The header file graphics.h contains line() function which is used to draw a line from a point(x1, y1) to point(x2, y2) i.e. (x1, y1) and (x2, y2) are end points of the line. The function line() draws a line on the graphics screen between two specified points. So this function requires four parameters namely x1, y1, x2, and y2 to represent two points. This function draws a line from (x1, y1) coordinates to (x2, y2) coordinates on the graphics screen.
setcolor() function is used to set the foreground color in graphics mode. After resetting the foreground color you will get the text or any other shape which you want to draw in that color. setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor. The current drawing color is the value to which pixels are set when lines, and so on are drawn. The drawing colors shown below are available for the CGA and EGA, respectively.
In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.
In C++, classes and structs are blueprints that are used to create the instance of a class. Structs are used for lightweight objects such as Rectangle, color, Point, etc. Unlike class, structs in C++ are value type than reference type. It is useful if you have data that is not intended to be modified after creation of struct. C++ Structure is a collection of different data types. It is similar to the class that holds different types of data. A structure is declared by preceding the struct keyword followed by the identifier(structure name). Inside the curly braces, we can declare the member variables of different types.
The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program. The getch() function does not accept any parameter from the user. It returns the ASCII value of the key pressed by the user as an input.
Compute sine. Returns the sine of an angle of x radians. sin() function is a library function of cmath header, it is used to find the sine of the given number (angle), it accepts a number (x) and returns the sine of angle x radians. Additional overloads are provided in this header (<cmath>) for the integral types: These overloads effectively cast x to a double before calculations (defined for T being any integral type). This function is also overloaded in <complex> and <valarray> (see complex sin and valarray sin).
Absolute value. Returns the absolute value of parameter n ( /n/ ). In C++, this function is also overloaded in header <cmath> for floating-point types (see cmath abs), in header <complex> for complex numbers (see complex abs), and in header <valarray> for valarrays (see valarray abs). Basically the abs function evaluates the absolute value of the given value i.e. value after removing all the signs of negative and positive from the number. Which means it will always return a positive number. Function returns the absolute value of n. abs() function - In C the input is of type 'int' whereas in C++ input is of type 'int, long int or long long int'. In C the output is of 'int' type and in C++ the output has the same data type as input.
The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.
The header file graphics.h contains putpixel() function which plots a pixel at location (x, y) of specified color. Where, (x, y) is the location at which pixel is to be put, and color specifies the color of the pixel. To put a pixel on the screen at a particular position, calling the pixel() function is a good way. This function takes three parameters as the position of the pixel and also the color of the pixel.
An array is a collection of data items, all of the same type, accessed using a common name. A one-dimensional array is like a list; A two dimensional array is like a table; The C++ language places no limits on the number of dimensions in an array, though specific implementations may. Some texts refer to one-dimensional arrays as vectors, two-dimensional arrays as matrices, and use the general term arrays when the number of dimensions is unspecified or unimportant. (2D) array in C++ programming is also known as matrix. A matrix can be represented as a table of rows and columns. In C/C++, we can define multi dimensional arrays in simple words as array of arrays. Data in multi dimensional arrays are stored in tabular form (in row major order).
The header file graphics.h contains cleardevice() function. cleardevice() is a function which is used to clear the screen by filling the whole screen with the current background color. It means that cleardevice() function is used to clear the whole screen with the current background color and it also sets the current position to (0,0). Both clrscr() and cleardevice() functions are used to clear the screen but clrscr() is used in text mode and cleardevice function is used in the graphics mode.
A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.
clearviewport() function clears the current viewport. setviewport will create a new viewport by accepting left, top, right and bottom coordinates. clearviewport() function will erase the drawing done on the view port only and not the whole screen. Cleardevice is the function used to clear the whole screen with the background color.
The header file graphics.h contains closegraph() function which closes the graphics mode, deallocates all memory allocated by graphics system and restores the screen to the mode it was in before you called initgraph. closegraph() function is used to re-enter in the text mode and exit from the graphics mode. If you want to use both text mode and graphics mode in the program then you have to use both initgraph() and closegraph() function in the program. This function deallocates all memory allocated by graphics system and restores the screen to that mode in which it was presented before you called the initgraph() function.
Split the Data into two equal half until we get at most one element in both half. Merge Both into one making sure the resulting 'Sequence' is sorted. Then 'recursively' split them, merge