Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C++ Programming Code Examples

C++ > Computer Graphics Code Examples

Program to implement Line Clipping Algorithm

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
/* Program to implement Line Clipping Algorithm */ #include<iostream.h> #include<conio.h> #include<math.h> #include<graphics.h> #include<dos.h> #include<process.h> int pixels[2][4]; float xn1,xn2,yn1,yn2,x3,y3,m; void show_quadrant() { cleardevice(); rectangle(120,40,320,240); rectangle(320,40,520,240); rectangle(120,240,320,440); rectangle(320,240,520,440); for(int i=130;i<=510;i+=10) { if(i==320) continue; outtextxy(i,237,"+"); } for(i=50;i<=430;i+=10) { if(i==240) continue; outtextxy(317,i,"-"); } outtextxy(310,230,"O"); outtextxy(530,240,"X"); outtextxy(320,450,"-Y"); outtextxy(100,240,"-X"); outtextxy(320,30,"Y"); } void su_co(int x1,int y1,int x2,int y2,int xmin,int ymin,int xmax,int ymax) { int i,j,fl; for(i=0;i<2;i++) for(j=0;j<4;j++) pixels[i][j]=0; if(y1>ymax) pixels[0][0]=1; if(y1<ymin) pixels[0][1]=1; if(x1>xmax) pixels[0][2]=1; if(x1<xmin) pixels[0][3]=1; if(y2>ymax) pixels[1][0]=1; if(y2<ymin) pixels[1][1]=1; if(x2>xmax) pixels[1][2]=1; if(x2<xmin) pixels[1][3]=1; for(j=0;j<4;j++) { if(pixels[0][j]==0&&pixels[1][j]==0) continue; if(pixels[0][j]==1&&pixels[1][j]==1) { fl=3; break; } fl=2; } switch(fl) { case 1: line(320+x1,240-y1,320+x2,240-y2); break; case 3: cout<<" a" Line Is Not Visible...:-("; break; case 2: m=(y2-y1)/(x2-x1); xn1=x1; yn1=y1; xn2=x2; yn2=y2; if(pixels[0][0]==1) { xn1=x1+(ymax-y1)/m; yn1=ymax; } if(pixels[0][1]==1) { xn1=x1+(ymin-y1)/m; yn1=ymin; } if(pixels[0][2]==1) { yn1=y1+(xmax-x1)*m; xn1=xmax; } if(pixels[0][3]==1) { yn1=y1+(xmin-x1)*m; xn1=xmin; } if(pixels[1][0]==1) { xn2=x2+(ymax-y2)/m; yn2=ymax; } if(pixels[1][1]==1) { xn2=x2+(ymin-y2)/m; yn2=ymin; } if(pixels[1][2]==1) { yn2=y2+(xmax-x2)*m; xn2=xmax; } if(pixels[1][3]==1) { yn2=y2+(xmin-x2)*m; xn2=xmin; } line(320+xn1,240-yn1,320+xn2,240-yn2); break; } } void midpt(int x1,int y1,int x2,int y2,int xmin,int ymin,int xmax,int ymax) { int fl=1; int i,j; int ox1=x1,ox2=x2,oy1=y1,oy2=y2; for(i=0;i<2;i++) for(j=0;j<4;j++) pixels[i][j]=0; if(y1>ymax) pixels[0][0]=1; if(y1<ymin) pixels[0][1]=1; if(x1>xmax) pixels[0][2]=1; if(x1<xmin) pixels[0][3]=1; if(y2>ymax) pixels[1][0]=1; if(y2<ymin) pixels[1][1]=1; if(x2>xmax) pixels[1][2]=1; if(x2<xmin) pixels[1][3]=1; for(j=0;j<4;j++) { if(pixels[0][j]==0&&pixels[1][j]==0) continue; if(pixels[0][j]==1&&pixels[1][j]==1) { fl=3; break; } fl=2; } switch(fl) { case 1: line(320+x1,240-y1,320+x2,240-y2); break; case 3: cout<<" a" Line Is Not Visible...:-("; break; case 2: xn1=x1; yn1=y1; xn2=x2; yn2=y2; fl=0; x3=x1; y3=y1; while(1) { if(!(y1>ymax || y1<ymin || x1>xmax || x1<xmin) && (x3 || y3)!=0.1) break; x3=(x1+x2)/2; y3=(y1+y2)/2; if(!(y3>ymax || y3<ymin || x3>xmax || x3<xmin)) fl=1; else fl=0; if(fl) { x2=x3; y2=y3; } else { x1=x3; y1=y3; } } xn1=x3; yn1=y3; fl=0; x1=ox1; x2=ox2; y1=oy1; y2=oy2; x3=x2; y3=y2; while(1) { if(!(y2>ymax || y2<ymin || x2>xmax || x2<xmin) && (x3 || y3)!=0.1) break; x3=(x1+x2)/2; y3=(y1+y2)/2; if(!(y3>ymax || y3<ymin || x3>xmax || x3<xmin)) fl=1; else fl=0; if(fl) { x1=x3; y1=y3; } else { x2=x3; y2=y3; } } xn2=x3; yn2=y3; line(320+xn1,240-yn1,320+xn2,240-yn2); break; } } void show_message() { char *mess[]={"-","=","["," ","L","i","n","e"," ","C","l","i", "p","p","i","n","g"," ","]","=","-",}; int xx=29,xxx=50,i,j; _setcursortype(_NOCURSOR); for(i=0,j=21;i<13,j>=11;i++,j--) { gotoxy(xx,1); cout<<mess[i]; xx++; gotoxy(xxx,1); cout<<mess[j]; xxx--; delay(50); } _setcursortype(_NORMALCURSOR); } void main() { clrscr(); int gd=DETECT,gm,i,j; int xmin,ymin,xmax,ymax,x1,y1,x2,y2; int choice,ed[20],num; show_message(); cout<<" " Enter The Co-Ordinates Of The Clipping Window.""; cout<<" " Enter X(min) & Y(min) ":="; cin>>xmin>>ymin; cout<<" " Enter X(max) & Y(max) ":="; cin>>xmax>>ymax; cout<<" " Enter The Co-Ordinates Of The Line.""; cout<<" " Enter X(1) & Y(1) ":="; cin>>x1>>y1; cout<<" " Enter X(2) & Y(2) ":="; cin>>x2>>y2; clrscr(); show_message(); cout<<" 1:==>" Sutherland-Cohen ""; cout<<" 2:==>" Mid-Point Method ""; cout<<" 3:==>" Exit ""; cout<<" " Enter Your Choice ":="; cin>>choice; switch(choice) { case 1: initgraph(&gd,&gm,"..\bgi"); clearviewport(); show_quadrant(); line(320+xmin,240-ymin,320+xmin,240-ymax); line(320+xmin,240-ymax,320+xmax,240-ymax); line(320+xmax,240-ymax,320+xmax,240-ymin); line(320+xmax,240-ymin,320+xmin,240-ymin); line (320+x1,240-y1,320+x2,240-y2); getch(); cleardevice(); show_quadrant(); line(320+xmin,240-ymin,320+xmin,240-ymax); line(320+xmin,240-ymax,320+xmax,240-ymax); line(320+xmax,240-ymax,320+xmax,240-ymin); line(320+xmax,240-ymin,320+xmin,240-ymin); su_co(x1,y1,x2,y2,xmin,ymin,xmax,ymax); getch(); break; case 2: initgraph(&gd,&gm,"..\bgi"); clearviewport(); show_quadrant(); line(320+xmin,240-ymin,320+xmin,240-ymax); line(320+xmin,240-ymax,320+xmax,240-ymax); line(320+xmax,240-ymax,320+xmax,240-ymin); line(320+xmax,240-ymin,320+xmin,240-ymin); line (320+x1,240-y1,320+x2,240-y2); getch(); cleardevice(); show_quadrant(); line(320+xmin,240-ymin,320+xmin,240-ymax); line(320+xmin,240-ymax,320+xmax,240-ymax); line(320+xmax,240-ymax,320+xmax,240-ymin); line(320+xmax,240-ymin,320+xmin,240-ymin); midpt(x1,y1,x2,y2,xmin,ymin,xmax,ymax); getch(); break; case 3: exit(0); default: cout<<" a" Press A Valid Key...!!! ""; getch(); main(); break; } closegraph(); }

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.

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.

#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.

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

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.

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.

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 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 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.

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.

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.

Continue statement is used inside loops. Whenever a continue statement is encountered inside a loop, control directly jumps to the beginning of the loop for next iteration, skipping the execution of statements inside loop's body for the current iteration. The continue statement works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between. For the for loop, continue causes the conditional test and increment portions of the loop to execute. For the while and do...while loops, program control passes to the conditional tests.

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 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 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.

C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of statements inside another loop. Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any number of loops. The nesting level can be defined at n times. You can define any type of loop inside another loop; for example, you can define 'while' loop inside a 'for' loop. A loop inside another loop is called a nested loop. The depth of nested loop depends on the complexity of a problem. We can have any number of nested loops as required. Consider a nested loop where the outer loop runs n times and consists of another loop inside it. The inner loop runs m times. Then, the total number of times the inner loop runs during the program execution is n*m.

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 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.

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).

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:

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)

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.


'Insert substring' in a string. 'Erase substring' from a string. 'Append substring' to a string. Replace the string with a substrng. Size of a string. Find substring in a string. Display the






Get size of the current type. Align depens on the next type and also alignment defined by #pragam pack(#). Offset will be minimum of this size. If the "size of next type" is less than