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

Analog Clock - This is a graphics program which depict a wall clock.

/* Analog Clock - This is a graphics program which depict a wall clock. */ #include<conio.h> #include<iostream.h> #include<graphics.h> #include<dos.h> #include<math.h> #include<stdlib.h> #include<alloc.h> void main() { int d=DETECT,m,r=50; float gx,gy,x,y,x1,y1,xm,ym,xh,yh,k=0,i,j,ii,l; initgraph(&d,&m,"e:\tcc\bgi"); struct time t; void *mem; int size; ii=13.89; setcolor(14); line(159,100,159,387); line(450,100,450,382); line(159,100,450,100); setcolor(14); arc(300,100,236,307,250); arc(300,100,244,298,320); //14.36,13.89 while(1) { while(ii<14.36) { if(kbhit()) exit(1); // second's pixel for(i=11;i<=17.3;i+=0.105) { gx=55*cos(i); gy=55*sin(i); putpixel(gx+300,gy+200,4); } // outer circle setcolor(12); circle(300,200,85); circle(300,200,87); circle(300,200,90); setcolor(15); circle(300,200,80); setcolor(4); outtextxy(330,143,"1"); outtextxy(350,165,"2"); outtextxy(360,197,"3"); outtextxy(352,228,"4"); outtextxy(328,252,"5"); outtextxy(298,260,"6"); outtextxy(264,252,"7"); outtextxy(242,226,"8"); outtextxy(235,197,"9"); outtextxy(235,165,"10"); outtextxy(260,140,"11"); outtextxy(292,135,"12"); //time function ///* gettime(&t); gotoxy(35,20); cout<<int(t.ti_hour)<<":"<<int(t.ti_min)<<":"<<int(t.ti_sec)<<""; i=(int(t.ti_sec)*(0.105))+11; j=(int(t.ti_min)*(0.105))+11; k=((int(t.ti_hour)*(0.105))*5+11); int min=int (t.ti_min); int rem=min/12; k=k+(rem*0.105); ///*/ //calculations for second hand x=r*cos(i); y=r*sin(i); setcolor(14); line(300,200,x+300,y+200); //calculations for minute hand xm=(r-5)*cos(j); ym=(r-5)*sin(j); setcolor(9); line(300,200,xm+300,ym+200); //calculations for hour hand xh=(r-20)*cos(k); yh=(r-20)*sin(k); setcolor(6); line(300,200,xh+300,yh+200); delay(200.5); int pks=0; if(kbhit()) exit(1); float l,m; for(pks=1;pks<=4;pks++) { l=200*cos(ii); m=200*sin(ii); setcolor(4); line(300,290,300+l,190+m); setcolor(14); setfillstyle(SOLID_FILL,14); circle(l+300,m+190,15); floodfill(l+300,m+190,14); ii+=.105; delay(200.5); setcolor(0); setfillstyle(SOLID_FILL,0); circle(l+300,m+190,15); floodfill(l+300,m+190,0); line(300,290,300+l,190+m); } setcolor(0); line(300,200,x+300,y+200); line(300,200,xm+300,ym+200); line(300,200,xh+300,yh+200); } while(ii>13.89) { if(kbhit()) exit(1); for(i=11;i<=17.3;i+=0.105) { gx=55*cos(i); gy=55*sin(i); putpixel(gx+300,gy+200,4); } setcolor(12); circle(300,200,85); circle(300,200,87); circle(300,200,90); setcolor(15); setfillstyle(SOLID_FILL,0); circle(300,200,80); floodfill(300,200,0); setcolor(4); outtextxy(330,143,"1"); outtextxy(350,165,"2"); outtextxy(360,197,"3"); outtextxy(352,228,"4"); outtextxy(328,252,"5"); outtextxy(298,260,"6"); outtextxy(264,252,"7"); outtextxy(242,226,"8"); outtextxy(235,197,"9"); outtextxy(235,165,"10"); outtextxy(260,140,"11"); outtextxy(292,135,"12"); gettime(&t); gotoxy(35,20); cout<<int(t.ti_hour)<<":"<<int(t.ti_min)<<":"<<int(t.ti_sec)<<""; i=(int(t.ti_sec)*(0.105))+11; j=(int(t.ti_min)*(0.105))+11; k=((int(t.ti_hour)*(0.105))*5+11); int min=int (t.ti_min); int rem=min/12; k=k+(rem*0.105); x=r*cos(i); y=r*sin(i); setcolor(14); line(300,200,x+300,y+200); xm=(r-5)*cos(j); ym=(r-5)*sin(j); setcolor(9); line(300,200,xm+300,ym+200); xh=(r-20)*cos(k); yh=(r-20)*sin(k); setcolor(6); line(300,200,xh+300,yh+200); delay(200.5); if(kbhit()) exit(1); for(int pks=1;pks<=4;pks++) { l=200*cos(ii); m=200*sin(ii); setcolor(4); line(300,290,300+l,190+m); setcolor(14); setfillstyle(SOLID_FILL,14); circle(l+300,m+190,15); floodfill(l+300,m+190,14); ii-=0.105; delay(200.5); setcolor(0); setfillstyle(SOLID_FILL,0); circle(l+300,m+190,15); floodfill(l+300,m+190,0); line(300,290,300+l,190+m); } setcolor(0); line(300,200,x+300,y+200); line(300,200,xm+300,ym+200); line(300,200,xh+300,yh+200); } } }

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 gettime() function is used to find current system time. We pass address of a structure varibale of type ( struct time ). gettime() function asks for a time object to be passed and then uses that object to get the current hour and minute etc. The gettime() function fills in the fields of the time structure pointed to by the t parameter. The current system time data is written in DOS format.

In the C programming language, there is an option to create an arc of a circle of a given radius with a given center coordinates and degree of the arc. The arc() function is used to create an arc. This arc function is included in graphics.h library in C which contains methods that can draw figures on the output screen. The function to make an arc(), accepts five parameters for x, y co-ordinate, starting angle, end angle and radius. This will make the arc will all the values are fine. The Example below takes care of all these things as it have four arcs implemented.

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

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

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.

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:

The kbhit is basically the Keyboard Hit. This function is present at conio.h header file. So for using this, we have to include this header file into our code. The functionality of kbhit() is that, when a key is pressed it returns nonzero value, otherwise returns zero. kbhit() is used to determine if a key has been pressed or not. If a key has been pressed then it returns a non zero value otherwise returns zero.

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

floodfill function is used to fill an enclosed area. Current fill pattern and fill color is used to fill the area.(x, y) is any point on the screen if (x,y) lies inside the area then inside will be filled otherwise outside will be filled, border specifies the color of boundary of area. To change fill pattern and fill color use setfillstyle.

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

This library function is declared in graphics.h and used to draw a circle; it takes centre point coordinates and radius. Circle function is used to draw a circle with center (x,y) and third parameter specifies the radius of the circle. The code given below draws a circle. Where, (x, y) is center of the circle. 'radius' is the Radius of the circle.

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.

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)

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



"Aho-Corasick" string matching algorithm is a 'searching algorithm', it is a kind of dictionary matching Algorithm that locates elements of a finite set of strings (the "Dictionary") within

2 const variables row & col are used to define size. If we do not make both const then error found because without "const reserve word" they are behaving as variable. Before placing