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

Moving 3D Box in free space

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
/* Moving 3D Box in free space */ #include <stdio.h> #include <dos.h> #include <graphics.h> #include <conio.h> #define MAGFY 20 class Bline { private: int CEN_X,CEN_Y,radius,X,Y,i,j; public: void line1(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4); }; void Bline::line1(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4) { CEN_X = (x1+x2)/2 + (x3-x2)/2; CEN_Y = (y1+y2)/2 + (y3-y2)/2; //radius = (x1+x2)/2; for(;!kbhit();) { delay(300); cleardevice(); //1st box line(x1,y1,x2,y2); line(x1,y1-20,x2,y2-20); // line(x3,y3,x4,y4); line(x3,y3-20,x4,y4-20); // line(x1,y1,x4,y4); line(x1,y1-20,x4,y4-20); // line(x2,y2,x3,y3); line(x2,y2-20,x3,y3-20); // //2nd box line(x1,y1,x1,y1-20); line(x2,y2,x2,y2-20); line(x3,y3,x3,y3-20); line(x4,y4,x4,y4-20); //some point which will be moving around 3Dbox //line(x2+40,y2-20,x2+40,y2-20); //rectangle(x2,y2,x4,y4); //1st Quadrant if(x1>CEN_X && y1<CEN_Y) x1--,y1--; if(x1==CEN_X && y1<CEN_Y) x1--,y1++; if(x1<CEN_X && y1<CEN_Y) x1--,y1++; if(x1<CEN_X && y1==CEN_Y) x1++,y1++; if(x1<CEN_X && y1>CEN_Y) x1++,y1++; if(x1==CEN_X && y1>CEN_Y) x1++,y1--; if(x1>CEN_X && y1>CEN_Y) x1++,y1--; if(x1>CEN_X && y1==CEN_Y) x1--,y1--; //2nd Quadrant if(x2>CEN_X && y2<CEN_Y) x2--,y2--; if(x2==CEN_X && y2<CEN_Y) x2--,y2++; if(x2<CEN_X && y2<CEN_Y) x2--,y2++; if(x2<CEN_X && y2==CEN_Y) x2++,y2++; if(x2<CEN_X && y2>CEN_Y) x2++,y2++; if(x2==CEN_X && y2>CEN_Y) x2++,y2--; if(x2>CEN_X && y2>CEN_Y) x2++,y2--; if(x2>CEN_X && y2==CEN_Y) x2--,y2--; //3rd Quadrant if(x3>CEN_X && y3<CEN_Y) x3--,y3--; if(x3==CEN_X && y3<CEN_Y) x3--,y3++; if(x3<CEN_X && y3<CEN_Y) x3--,y3++; if(x3<CEN_X && y3==CEN_Y) x3++,y3++; if(x3<CEN_X && y3>CEN_Y) x3++,y3++; if(x3==CEN_X && y3>CEN_Y) x3++,y3--; if(x3>CEN_X && y3>CEN_Y) x3++,y3--; if(x3>CEN_X && y3==CEN_Y) x3--,y3--; //4th Quadrant if(x4>CEN_X && y4<CEN_Y) x4--,y4--; if(x4==CEN_X && y4<CEN_Y) x4--,y4++; if(x4<CEN_X && y4<CEN_Y) x4--,y4++; if(x4<CEN_X && y4==CEN_Y) x4++,y4++; if(x4<CEN_X && y4>CEN_Y) x4++,y4++; if(x4==CEN_X && y4>CEN_Y) x4++,y4--; if(x4>CEN_X && y4>CEN_Y) x4++,y4--; if(x4>CEN_X && y4==CEN_Y) x4--,y4--; } } int main() { int GD=DETECT,GM; Bline line1; initgraph(&GD,&GM,"c:\tc\bgi"); //according to your own computer (TC\BIN) Directory line1.line1(5*MAGFY,7*MAGFY,1*MAGFY,3*MAGFY,3*MAGFY,1*MAGFY,7*MAGFY,5*MAGFY); getch(); restorecrtmode(); closegraph(); return 0; }

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.

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)

The main purpose of C++ programming is to add object orientation to the C programming language and classes are the central feature of C++ that supports object-oriented programming and are often called user-defined types. A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class.

In C++, constructor is a special method which is invoked automatically at the time of object creation. It is used to initialize the data members of new object generally. The constructor in C++ has the same name as class or structure. Constructors are special class functions which performs initialization of every object. The Compiler calls the Constructor whenever an object is created. Constructors initialize values to object members after storage is allocated to the object. Whereas, Destructor on the other hand is used to destroy the class object. • Default Constructor: A constructor which has no argument is known as default constructor. It is invoked at the time of creating object.

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.

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

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.

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.

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

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.

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.

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.

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.

Logical Operators are used to compare and connect two or more expressions or variables, such that the value of the expression is completely dependent on the original expression or value or variable. We use logical operators to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0. Assume variable A holds 1 and variable B holds 0:

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.

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.

In this C++ example, you'll 'learn to overload' Increment ++ and Decrement -- operators in C++. "increment & decrements" operator are overloaded in best possible way, increase the

To write a "Multiline Comment" you have to write "/*" at the start of the Comment. After this you can write as many number of lines of description. When you are done with writing

Stooge sort is a recursive sorting algorithm. It Divides the Array into 2 overlapping parts, 2/3 each. Sort the first part. Sort second part and again sort the first part. Then Print the Result