C++ Programming Code Examples
C++ > Computer Graphics Code Examples
A simple rotation of an ellipse by different angle values.
/* A simple rotation of an ellipse by different angle values.
Atomic model by 2-D transformation */
//#include "winbgim.h"
#include <graphics.h>
#include <math.h>
void Translation(int X,int Y,int Tx,int Ty,int *Xp,int *Yp)
{ *Xp=X+Tx;
*Yp=Y+Ty;
}
void Scaling(int X,int Y,float Sx,float Sy,int R,int P,int *Xp,int *Yp)
{ *Xp=(int)(Sx*(X-R)+0.5)+R;
*Yp=(int)(Sy*(Y-P)+0.5)+P;
}
void Rotation(int X,int Y,float S,float C,int R,int P,int *Xp,int *Yp)
{ int Xx,Yy;
Xx=X-R; Yy=Y-P;
*Xp=(int)(Xx*C-Yy*S+0.5)+R;
*Yp=(int)(Xx*S+Yy*C+0.5)+P;
}
int main(){
int X[361],Y[361],Xp,Yp,I,J,K,Tx,Ty;
float T,C,S,Pi;
initwindow(450,450);
int i, x, y, color,MaxColors;
//srand( seed ); /* Restart random # function
*/
MaxColors=getmaxcolor()-1;
for( i=0 ; i<1500 ; ++i ){ /* Put 5000 pixels on screen
*/
x = 1 + random( 450 - 1 ); /* Generate a random location
*/
y = 1 + random( 450 - 1 );
color = random( MaxColors );
putpixel( x, y, color );
}
Pi=4*atan(1);
for(J=0;J<=360;J++)
{ T=J*Pi/180; S=sin(T); C=cos(T) ;
X[J]=(int)(200.0+150.0*C+0.5);
Y[J]=(int)(180.0+100.0*S+0.5);
//setcolor(4);
setfillstyle(12,1);
fillellipse(92,80,4,4);
fillellipse(324,200,4,4);
}
for(J=0;J<9;J++)
{ setcolor(6+J);
T=J*45*Pi/180; S=sin(T); C=cos(T);
for(I=0;I<=360;I++)
{ Rotation(X[I],Y[I],S,C,200,180,&Xp,&Yp);
if(I==0) moveto(Xp,Yp);
else lineto(Xp,Yp);
}
setfillstyle(12,1);
fillellipse(192,82,4,4);
fillellipse(200,281,4,4);
}
//setfillstyle(13,2);
fillellipse(200,180,10,10);
getch();
closegraph();
return 0;
}
Compute tangent. Returns the tangent of an angle of x radians. tan() function is a library function of cmath header, it is used to find the tangent of the given number (angle), it accepts a number (x) and returns the tangent of angle x radians. In trigonometry, the tangent function of a right-angled triangle is the ratio between the adjacent and opposite sides of a right triangle. The tan function in C++ works precisely like the tangent in trigonometry. The return value of the tan function is the tangent of an angle given in radian. Function returns tangent of x radians.
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.
#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.
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.
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.
Function lineto() draws a line from the current position (CP) to the point (x, y), you can get current position using getx and gety function. Where, (x, y) are the coordinates upto which the line will be drawn from previous point.
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.
getmaxcolor() returns highest possible color value. The header file graphics.h contains getmaxcolor() function, which returns maximum color value for current graphics mode and driver. As color numbering starts from zero, total number of colors available for current graphics mode and driver are ( getmaxcolor() + 1 ) . getmaxcolor returns the maximum color value. There are various colors that are also mentioned in graphics.h header file and the possible color values are from 0 - 15:
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 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.
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.
Initialize random number generator. The pseudo-random number generator is initialized using the argument passed as seed. The C++ <cstdlib> srand() function seeds the pseudo-random number generator used by rand() function. If rand() is used before any calls to srand(), rand() behaves as if it was seeded with srand(1). For every different seed value used in a call to srand, the pseudo-random number generator can be expected to generate a different succession of results in the subsequent calls to rand. Two different initializations with the same seed will generate the same succession of results in subsequent calls to rand. If seed is set to 1, the generator is reinitialized to its initial value and produces the same values as before any call to rand or srand.
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 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).
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.
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.
Function moves the current position(cp) to (x,y). The header file graphics.h contains moveto() function which changes the current position to (x, y). Means if you want to move a point from the current position to a new position then you can use this function. outtext is the function used to display the given string on graphics mode. It uses the font style set by settextstyle and the current position. The current position can be changed with moveto function. Alternatively you can use outtextxy function which takes xpos, ypos and string for displaying at a particular location. outtextxy does the work of both moveto and outtext functions.
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.
This is a C++ Program to find 'Prime number' between the given range using 'Wheel Seive' method. "Wheel Factorization" is a graphical method for manually performing preliminary
Prints a 'subset' of the given array using coin flipping method. The time complexity of this algorithm is O(n). Takes the input of 'n' data element and prints a possible subset. that, it
'Constructors with parameters' are known as "Parameterized Constructors". So these type of constructor allows us to "pass arguments" while object creation. Lets see how they look: