Saturday, 19 November 2011

C and C++ Graphics


Graphics are used in C++ and C programming to make a user friendly software.
Here I will discuss all graphics that I know well. S let's begin

Graphics in C and C++ languages:
Let's start with graphics header file

#include<graphics.h>



Use cleardevice(); instead of clrscr();


void main()
{
cleardevice();

int gd=DETECT,gm; //gd mean graphics detection
//gm mean graphics mode

initgraphics(&gd, &gm,"c:\\tc\\bgi");

/*Above 2 lines are used to get graphics supported files thst may be in c or d derive where you have installed turbo c++ 3.0.
You just have to memorize above 4 lines commands then graphics will become much easy for you.*/

//To draw a circle use following command

circle(x-coordinate,y-coordinate,radius);
for example
circle(250,150,10);

this command will draw circle on 250 and 150 coordinate.
Our computer screen contain 800 columns and 250 rows so adjust according to requirement.

Now to draw rectangle, write

rectangle(x1 coordinate, y1 coordinate, x2 coordinate, y2 coordinate);

for example:
rectangle(120,250,450,100);
similarly
pyramid(120,250,450,100);
and
line(120,250,450,100);
setfillstyle(SOLIDFILL,XFILL); //to fill rectangle r circle with x's

Set color and text styles:

When we use a colour in our program then we use upper case letters for example, RED, BLUE.
We can also colour code for this purpose that's start from 0 and end at 15.

Now to change your background colour write
setbkcolor(RED);
or
setbkcolor(3);

To change text or shapes colours use
setcolor(RED);
or
setcolor(2); //there can be any number instead of 2

To change text style:

settextstyle(horizontal or vertical, style, size);
settextstyle(1,5,8);

and then
outtextxy(x-coordinate,y-coordinate,"any text");
outtextxy(25,100,"My name is waqas");


and at the end
closegraph();

Now let's review the whole commands:

#include<graphics.h>

void main()
{
cleardevice();

int gd=DETECT,gm;
initgraphics(&gd, &gm,"c:\\tc\\bgi");

setbkcolor(ORANGE);
setcolor(RED);

rectangle(25,45,100,150);
line((25,45,100,150);
pyramid((25,45,100,150);

circle(25,100,8);

settextstyle(2,5,10);
outtextxy(100,25,"Welcome");

closegraph();

getch();
}

Link: http://cprogg.blogspot.com/2011/10/c-graphics.html