C++ Programming Code Examples
C++ > Computer Graphics Code Examples
Two-Dimension Transformation In Homogeneous Coordinate
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/* Two-Dimension Transformation In Homogeneous Coordinate
This Program Deals With All Two-D Transformation Such As Translation, Scaling, Rotation,
Reflection, Shearing In Homogeneous Coordinates. */
//TwoDimensional Transformations In Homogeneous
#include<graphics.h>
#include<iostream.h>
#include<Math.h>
#include<conio.h>
#define maxsize 3
class D_2
{
private:
double Points[maxsize][maxsize];
void Mult(double [maxsize][maxsize]);
void MultTwoMat(double [maxsize][maxsize],double [maxsize][maxsize]);
void Print();
int x,y;
public:
D_2();
void initialize();
void GetPoints();
void Draw(int);
void DrawCord();
void Translate();
void Rotate();
void Reflect();
void Display(double[maxsize][maxsize]);
void Shear();
void Scale_Fixed();
void Scale_Dir();
};
D_2::D_2()
{
for(int i=0;i<maxsize;i++)
{
for(int j=0;j<maxsize;j++)
{
if(i == (maxsize-1))
Points[i][j] = 1;
else
Points[i][j] = 0;
}
}
initialize();
x = getmaxx();
y = getmaxy();
}
void D_2::initialize()
{
int gdrive = DETECT,gmode;
initgraph(&gdrive,&gmode,"c: cgi");
}
void D_2::GetPoints()
{
closegraph();
cout<<"Enter The Points Of The Triangle.
";
for(int j=0;j<maxsize;j++)
{
cout<<"Enter Point "<<j+1<<":-";
for(int i=0;i<maxsize-1;i++)
{
cout<<"
Enter "<<char(i+'X')<<": ";
cin>>Points[i][j];
}
}
initialize();
}
void D_2::Mult(double temp[maxsize][maxsize])
{
int i,j,k;
double z[maxsize][maxsize];
for(i=0;i<maxsize;i++)
{
for(j=0;j<maxsize;j++)
z[i][j]=0;
}
for(i=0;i<maxsize;i++)
{
for(j=0;j<maxsize;j++)
{
for(k=0;k<maxsize;k++)
z[i][j]=z[i][j]+(temp[i][k] * Points[k][j]);
}
}
for(i=0;i<maxsize;i++)
{
for(j=0;j<maxsize;j++)
{
Points[i][j] = z[i][j];
}
}
}
void D_2::Draw(int color)
{
int Poly[2*maxsize+2];
int k = 0;
if(color == GREEN)
DrawCord();
for(int j=0;j<maxsize;j++)
{
for(int i=0;i<maxsize-1;i++)
{
if(i==0)
Poly[k++] = x/2+Points[i][j];
else
Poly[k++] = y/2-Points[i][j];
}
}
Poly[k++] = Poly[0];
Poly[k] = Poly[1];
setcolor(color);
drawpoly(4,Poly);
}
void D_2::Display(double Mat[maxsize][maxsize])
{
for(int i=0;i<maxsize;i++)
{
for(int j=0;j<maxsize;j++)
{
cout<<Mat[i][j]<<" ";
}
cout<<"
";
}
}
void D_2::Print()
{
setcolor(GREEN);
setfillstyle(SOLID_FILL,GREEN);
fillellipse(19,36,2,2);
outtextxy(23,34," Original Triangle");
setcolor(MAGENTA);
setfillstyle(SOLID_FILL,MAGENTA);
fillellipse(x-178,y-32,2,2);
outtextxy(x-175,y-34," Tranformed Triangle");
}
void D_2::DrawCord()
{
setcolor(12);
line(x/2,0,x/2,y);
line(0,y/2,x,y/2);
setcolor(10);
setfillstyle(SOLID_FILL,10);
fillellipse(x/2,y/2,2,2);
for(int i=(x/2+50),j=(x/2-50);i<=x,j>=0;i=i+50,j=j-50)
{
fillellipse(i,y/2,2,2);
fillellipse(j,y/2,2,2);
}
for(i=(y/2+50),j=(y/2-50);i<=x,j>=0;i=i+50,j=j-50)
{
fillellipse(x/2,i,2,2);
fillellipse(x/2,j,2,2);
}
outtextxy(x/2+3,y/2+4,"0");
outtextxy(x/2+45,y/2+5,"50");
outtextxy(x/2+95,y/2+5,"100");
outtextxy(x/2+145,y/2+5,"150");
outtextxy(x/2+195,y/2+5,"200");
outtextxy(x/2+245,y/2+5,"250");
outtextxy(x/2+295,y/2+5,"300");
outtextxy(x/2-65,y/2+5,"-50");
outtextxy(x/2-115,y/2+5,"-100");
outtextxy(x/2-165,y/2+5,"-150");
outtextxy(x/2-215,y/2+5,"-200");
outtextxy(x/2-265,y/2+5,"-250");
outtextxy(x/2-315,y/2+5,"-300");
outtextxy(x/2+5,y/2+45,"-50");
outtextxy(x/2+5,y/2+95,"-100");
outtextxy(x/2+5,y/2+145,"-150");
outtextxy(x/2+5,y/2+195,"-200");
outtextxy(x/2+5,y/2-50,"50");
outtextxy(x/2+5,y/2-100,"100");
outtextxy(x/2+5,y/2-150,"150");
outtextxy(x/2+5,y/2-200,"200");
}
void D_2::MultTwoMat(double temp[maxsize][maxsize],double
temp1[maxsize][maxsize])
{
int i,j,k;
double z[maxsize][maxsize];
for(i=0;i<maxsize;i++)
{
for(j=0;j<maxsize;j++)
z[i][j]=0;
}
for(i=0;i<maxsize;i++)
{
for(j=0;j<maxsize;j++)
{
for(k=0;k<maxsize;k++)
z[i][j]=z[i][j]+(temp[i][k] * temp1[k][j]);
}
}
for(i=0;i<maxsize;i++)
{
for(j=0;j<maxsize;j++)
{
temp1[i][j] = z[i][j];
}
}
}
void D_2::Translate()
{
int Tx,Ty;
double Trans[maxsize][maxsize];
closegraph();
cout<<"Enter Translation Factor Along X-Axis: ";
cin>>Tx;
cout<<"Enter Translation Factor Along Y-Axis: ";
cin>>Ty;
initialize();
for(int j=0;j<maxsize;j++)
{
for(int i=0;i<maxsize;i++)
{
if(i==j)
Trans[i][j] = 1;
else
Trans[i][j] = 0;
}
}
Trans[0][maxsize-1] = Tx;
Trans[1][maxsize-1] = Ty;
Draw(GREEN);
Mult(Trans);
Draw(MAGENTA);
Print();
}
void D_2::Rotate()
{
double ang;
const double PI = 22.0/7;
double xr,yr;
double TransMat[maxsize][maxsize];
double RotMat[maxsize][maxsize];
double InvTransMat[maxsize][maxsize];
closegraph();
cout<<"Enter Angle Of Rotation: ";
cin>>ang;
cout<<"Enter Point Of Rotation:
X: ";
cin>>xr;
cout<<"
Y: ";
cin>>yr;
initialize();
ang = (PI * ang)/180.0;
setcolor(YELLOW);
setfillstyle(SOLID_FILL,YELLOW);
fillellipse(x/2+xr,y/2-yr,2,2);
outtextxy(x/2+xr,y/2-yr-2," Point Of Rotation");
//Transformation Matrix
//Translate arbitrary point to origin then rotate then translate back.
for(int i=0;i<maxsize;i++)
{
for(int j=0;j<maxsize;j++)
{
if(i == j)
{
TransMat[i][j] = 1;
InvTransMat[i][j] = 1;
RotMat[i][j] = 1;
}
else
{
TransMat[i][j] = 0;
InvTransMat[i][j] = 0;
RotMat[i][j] = 0;
}
}
}
TransMat[0][2] = -xr;
TransMat[1][2] = -yr;
InvTransMat[0][2] = xr;
InvTransMat[1][2] = yr;
RotMat[0][0] = cos(ang);
RotMat[0][1] = -sin(ang);
RotMat[1][0] = sin(ang);
RotMat[1][1] = cos(ang);
Draw(GREEN);
Print();
MultTwoMat(InvTransMat,RotMat);
MultTwoMat(RotMat,TransMat);
Mult(TransMat);
Draw(MAGENTA);
}
void D_2::Reflect()
{
double ang;
double a,b,c;
double xr,yr;
double TransMat[maxsize][maxsize];
double RotMat[maxsize][maxsize];
double InvTransMat[maxsize][maxsize];
double InvRotMat[maxsize][maxsize];
double RefMat[maxsize][maxsize];
closegraph();
cout<<"Enter The Line (ax+by+c=0): ";
cout<<"
a: ";
cin>>a;
cout<<"
b: ";
cin>>b;
cout<<"
c: ";
cin>>c;
if(b!=0)
{
yr = (-c/b);
xr = 0;
double m = -a/b;
ang = atan(m);
}
else
{
yr = 0;
xr = (-c/a);
ang = 22.0/14.0; // Angle = PI/2
}
initialize();
//Transformation Matrix
//Translate arbitrary point to origin then rotate then translate back.
for(int i=0;i<maxsize;i++)
{
for(int j=0;j<maxsize;j++)
{
if(i == j)
{
TransMat[i][j] = 1;
InvTransMat[i][j] = 1;
RotMat[i][j] = 1;
InvRotMat[i][j] = 1;
RefMat[i][j] = 1;
}
else
{
TransMat[i][j] = 0;
InvTransMat[i][j] = 0;
RotMat[i][j] = 0;
InvRotMat[i][j] = 0;
RefMat[i][j] = 0;
}
}
}
TransMat[0][2] = -xr;
TransMat[1][2] = -yr;
InvTransMat[0][2] = xr;
InvTransMat[1][2] = yr;
RotMat[0][0] = cos(ang);
RotMat[0][1] = sin(ang);
RotMat[1][0] = -sin(ang);
RotMat[1][1] = cos(ang);
InvRotMat[0][0] = cos(ang);
InvRotMat[0][1] = -sin(ang);
InvRotMat[1][0] = sin(ang);
InvRotMat[1][1] = cos(ang);
for(i=0;i<maxsize;i++)
{
for(int j=0;j<maxsize;j++)
{
if(i==j)
RefMat[i][j] = pow(-1,i)*1;
else
RefMat[i][j] = 0;
}
}
Print();
Draw(GREEN);
MultTwoMat(InvTransMat,InvRotMat);
MultTwoMat(InvRotMat,RefMat);
MultTwoMat(RefMat,RotMat);
MultTwoMat(RotMat,TransMat);
Mult(TransMat);
Draw(MAGENTA);
}
void D_2::Shear()
{
double ang;
double a,b,c;
double xr,yr,shx;
double TransMat[maxsize][maxsize];
double RotMat[maxsize][maxsize];
double InvTransMat[maxsize][maxsize];
double InvRotMat[maxsize][maxsize];
double ShearMat[maxsize][maxsize];
closegraph();
cout<<"Enter The Line (ax+by+c=0): ";
cout<<"
a: ";
cin>>a;
cout<<"
b: ";
cin>>b;
cout<<"
c: ";
cin>>c;
cout<<"Enter Shearing Factor Along X-Axis: ";
cin>>shx;
if(b!=0)
{
yr = (-c/b);
xr = 0;
double m = -a/b;
ang = atan(m);
}
else
{
yr = 0;
xr = (-c/a);
ang = 22.0/14.0; // Angle = PI/2
}
initialize();
//Transformation Matrix
for(int i=0;i<maxsize;i++)
{
for(int j=0;j<maxsize;j++)
{
if(i == j)
{
TransMat[i][j] = 1;
InvTransMat[i][j] = 1;
RotMat[i][j] = 1;
InvRotMat[i][j] = 1;
ShearMat[i][j] = 1;
}
else
{
TransMat[i][j] = 0;
InvTransMat[i][j] = 0;
RotMat[i][j] = 0;
InvRotMat[i][j] = 0;
ShearMat[i][j] = 0;
}
}
}
TransMat[0][2] = -xr;
TransMat[1][2] = -yr;
InvTransMat[0][2] = xr;
InvTransMat[1][2] = yr;
RotMat[0][0] = cos(ang);
RotMat[0][1] = sin(ang);
RotMat[1][0] = -sin(ang);
RotMat[1][1] = cos(ang);
InvRotMat[0][0] = cos(ang);
InvRotMat[0][1] = -sin(ang);
InvRotMat[1][0] = sin(ang);
InvRotMat[1][1] = cos(ang);
ShearMat[0][1] = shx;
Print();
Draw(GREEN);
MultTwoMat(InvTransMat,InvRotMat);
MultTwoMat(InvRotMat,ShearMat);
MultTwoMat(ShearMat,RotMat);
MultTwoMat(RotMat,TransMat);
Mult(TransMat);
Draw(MAGENTA);
}
void D_2::Scale_Fixed()
{
double sx,sy;
double xr,yr;
double TransMat[maxsize][maxsize];
double ScaleMat[maxsize][maxsize];
double InvTransMat[maxsize][maxsize];
closegraph();
cout<<"Enter The Scaling Factor Along X-Axis: ";
cin>>sx;
cout<<"Enter The Scaling Factor Along Y-Axis: ";
cin>>sy;
cout<<"Enter Point Of Scaling:
X: ";
cin>>xr;
cout<<"
Y: ";
cin>>yr;
initialize();
//Transformation Matrix
for(int i=0;i<maxsize;i++)
{
for(int j=0;j<maxsize;j++)
{
if(i == j)
{
TransMat[i][j] = 1;
InvTransMat[i][j] = 1;
ScaleMat[i][j] = 1;
}
else
{
TransMat[i][j] = 0;
InvTransMat[i][j] = 0;
ScaleMat[i][j] = 0;
}
}
}
TransMat[0][2] = -xr;
TransMat[1][2] = -yr;
InvTransMat[0][2] = xr;
InvTransMat[1][2] = yr;
ScaleMat[0][0] = sx;
ScaleMat[1][1] = sy;
Draw(GREEN);
Print();
MultTwoMat(InvTransMat,ScaleMat);
MultTwoMat(ScaleMat,TransMat);
Mult(TransMat);
Draw(MAGENTA);
}
void D_2::Scale_Dir()
{
double sx,sy;
double ang;
const double PI = 22.0/7;
double RotMat[maxsize][maxsize];
double ScaleMat[maxsize][maxsize];
double InvRotMat[maxsize][maxsize];
closegraph();
cout<<"Enter The Scaling Factor Along X-Axis: ";
cin>>sx;
cout<<"Enter The Scaling Factor Along Y-Axis: ";
cin>>sy;
cout<<"Enter The Direction Of Scaling: ";
cin>>ang;
ang = (PI * ang)/180.0;
initialize();
//Transformation Matrix
for(int i=0;i<maxsize;i++)
{
for(int j=0;j<maxsize;j++)
{
if(i == j)
{
RotMat[i][j] = 1;
InvRotMat[i][j] = 1;
ScaleMat[i][j] = 1;
}
else
{
RotMat[i][j] = 0;
InvRotMat[i][j] = 0;
ScaleMat[i][j] = 0;
}
}
}
RotMat[0][0] = cos(ang);
RotMat[0][1] = sin(ang);
RotMat[1][0] = -sin(ang);
RotMat[1][1] = cos(ang);
InvRotMat[0][0] = cos(ang);
InvRotMat[0][1] = -sin(ang);
InvRotMat[1][0] = sin(ang);
InvRotMat[1][1] = cos(ang);
ScaleMat[0][0] = sx;
ScaleMat[1][1] = sy;
Draw(GREEN);
Print();
MultTwoMat(RotMat,ScaleMat);
MultTwoMat(ScaleMat,InvRotMat);
Mult(InvRotMat);
Draw(MAGENTA);
}
void main()
{
D_2 D1;
D1.DrawCord();
getch();
int ch;
D1.GetPoints();
D1.Draw(GREEN);
getch();
do
{
closegraph();
clrscr();
cout<<"1.To ReDraw The Triangle.
";
cout<<"2.Translate The Triangle.
";
cout<<"3.Scaling The Triangle About Fixed Point.
";
cout<<"4.Scaling The Triangle In A Direction.
";
cout<<"5.Rotating The Triangle About Arbitrary Point.
";
cout<<"6.Reflecting The Triangle About Arbitrary Line.
";
cout<<"7.Shearing Of The Triangle.
";
cout<<"8.Exit.
";
cout<<"Enter The Choice: ";
cin>>ch;
D1.initialize();
switch(ch)
{
case 1:
D1.GetPoints();
D1.Draw(GREEN);
getch();
break;
case 2:
cleardevice();
D1.Translate();
getch();
closegraph();
break;
case 3:
cleardevice();
D1.Scale_Fixed();
getch();
closegraph();
break;
case 4:
cleardevice();
D1.Scale_Dir();
getch();
closegraph();
break;
case 5:
cleardevice();
D1.Rotate();
getch();
closegraph();
break;
case 6:
cleardevice();
D1.Reflect();
getch();
closegraph();
break;
case 7:
cleardevice();
D1.Shear();
getch();
closegraph();
break;
case 8:
return;
default:
cout<<"
WRONG CHOICE.
";
getch();
break;
}
}while(1);
}
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).
Raise to power. The pow() function returns the result of the first argument raised to the power of the second argument. This function is defined in the cmath header file. pow() function is a library function of cmath header, it is used to find the raise to the power, it accepts two arguments and returns the first argument to the power of the second argument.
In the C++ Programming Language, the #define directive allows the definition of macros within your source code. These macro definitions allow constant values to be declared for use throughout your code. Macro definitions are not variables and cannot be changed by your program code like variables. You generally use this syntax when creating constants that represent numbers, strings or expressions. The syntax for creating a constant using #define in the C++ is: #define token value
Compute arc tangent. Function returns the principal value of the arc tangent of x, expressed in radians. In trigonometrics, arc tangent is the inverse operation of tangent. Notice that because of the sign ambiguity, the function cannot determine with certainty in which quadrant the angle falls only by its tangent value. See atan2 for an alternative that takes a fractional argument instead. Function returns principal arc tangent of x, in the interval [-pi/2,+pi/2] radians. One radian is equivalent to 180/PI degrees.
Rotate left the elements in range. Rotates the order of the elements in the range [first,last), in such a way that the element pointed by middle becomes the new first element. rotate() function is a library function of algorithm header, it is used to rotate left the elements of a sequence within a given range, it accepts the range (start, end) and a middle point, it rotates the elements in such way that the element pointed by the middle iterator becomes the new first element. ForwardIterator shall point to a type for which swap is properly defined and which is both move-constructible and move-assignable. Function returns an iterator pointing to the element that now contains the value previously pointed by first.
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.
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.
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.
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.
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.
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.
It is a predefined function in "conio.h" (console input output header file) used to clear the console screen. It is a predefined function, by using this function we can clear the data from console (Monitor). Using of clrscr() is always optional but it should be place after variable or function declaration only. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.
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.
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.
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).
The header file graphics.h contains getmaxx() function which returns the maximum X coordinate for current graphics mode and driver. getmaxx() returns the maximum (screen-relative) x value for the current graphics driver and mode. For example, on a CGA in 320*200 mode, getmaxx returns 319. getmaxx is invaluable for centering, determining the boundaries of a region onscreen, and so on.
Switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. Each case in a block of a switch has a different name/number which is referred to as an identifier. The value provided by the user is compared with all the cases inside the switch block until the match is found. If a case match is NOT found, then the default statement is executed, and the control goes out of the switch block. • The expression can be integer expression or a character expression. • Value-1, 2, n are case labels which are used to identify each case individually. Remember that case labels should not be same as it may create a problem while executing a program. Suppose we have two cases with the same label as '1'. Then while executing the program, the case that appears first will be executed even though you want the program to execute a second case. This creates problems in the program and
The header file graphics.h contains drawpoly() function which is used to draw polygons i.e. triangle, rectangle, pentagon, hexagon etc. Drawpoly draws a polygon with numpoints points, using the current line style and color. To understand more clearly we will draw a triangle using drawpoly, consider for example,the array :- int points[] = { 320, 150, 420, 300, 250, 300, 320, 150};
The header file graphics.h contains getmaxy() function which returns the maximum Y coordinate for current graphics mode and driver. getmaxy returns the maximum (screen-relative) y value for the current graphics driver and mode. For example, on a CGA in 320*200 mode, getmaxy returns 199. getmaxy is invaluable for centering, determining the boundaries of a region onscreen, and so on.
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.
#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.
The header file graphics.h contains setfillstyle() function which sets the current fill pattern and fill color. Current fill pattern and fill color is used to fill the area. setfillstyle sets the current fill pattern and fill color. To set a user-defined fill pattern, do not give a pattern of 12 (USER_FILL) to setfillstyle; instead, call setfillpattern.
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.
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.
Draws an ellipse using (x,y) as a center point and xradius and yradius as the horizontal and vertical axes, and fills it with the current fill color and fill pattern. The header file graphics.h contains fillellipse() function which draws and fills an ellipse with center at (x, y) and (xradius, yradius) as x and y radius of ellipse. Where, (x, y) is center of the ellipse. (xradius, yradius) are x and y radius of ellipse.
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.
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.
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)
Algorithm takes the input of "n" data element and 'prints all possible' combination of length 'k'. It maintains a boolean array of length 'n'. If the corresponding boolean value is true, then