Skip to content

C Tutorial #4:- Drawing in C

January 20, 2013

Drawing in C

It is easy to draw in C. We have so many built in functions in C for drawing. Before going to the program, let us see the coordinate system of C. Here, the origin is at let-top most corner (0,0). Going on right, X-Coordinate will increase. Going on down, y-coordinate will increase.

getmaxx() function will return the MAXIMUM X-Coordinate of the current monitor (it may be 1000-2000).

getmaxy() function will return the MAXIMUM Y-Coordinate of the current monitor (it may be 500-600).

#include<stdio.h>

#include<graphics.h>

main()

{

int gd,gm;

int maxx,maxy;

detectgraph(&gd,&gm);

initgraph(&gd,&gm,” “);

maxx=  getmaxx();

maxy=getmaxy();

while (!kbhit())

{

circle (maxx/2,maxy/2,50);

}

getch();

closegraph();

}

kbhit() funtion will return ‘1’ if we press any key on the keyboard. it will return ‘0’ if NO PRESS key is pressed.

so, while( !kbhit() ) will execute until any key is pressed on keyboard.

circle(x,y,radius) – takes three parameters. (x,y) are the x,y coordinates of center of the circle. and third parameter is for radius.

closegraph()- closes the graphics mode. and shifts to text mode.

#include<stdio.h>

#include<graphics.h>

main()

{

int gd,gm;

int maxx,maxy;

detectgraph(&gd,&gm);

initgraph(&gd,&gm,” “);

maxx=  getmaxx();

maxy=getmaxy();

while (!kbhit())

{

circle (maxx/2,maxy/2,50);

}

getch();

closegraph();

}

#include<stdio.h>

#include<graphics.h>

main()

{

int gd,gm;

int maxx,maxy;

detectgraph(&gd,&gm);

initgraph(&gd,&gm,” “);

maxx=  getmaxx();

maxy=getmaxy();

while (!kbhit())

{

setcolor(14);

rectangle(10,10,maxx-10,maxy-10);

}

getch();

closegraph();

}

setcolor() function sets the color attribute to specified color. from now, all drawings ( borders) are colored with this color.

rectangle() function draws rectangle with specified coordinates.

rectangle(x1,y1,x2,y2) – where (x1,y1), (x2,y2) are vertices of diagnol.

#include<stdio.h>

#include<graphics.h>

main()

{

int gd,gm;

int maxx,maxy;

detectgraph(&gd,&gm);

initgraph(&gd,&gm,” “);

maxx=  getmaxx();

maxy=getmaxy();

while (!kbhit())

{

settextstyle(4,0,5);

outtextxy(200,200,”Teja”);

}

getch();

closegraph();

}

outtextxy(X,Y,TEXT) – it draws text on canvas. X,Y are  coodinates of starting letter. TEXT is a string to be displayed.

settextstyle(font-style,direction,size).

#include<stdio.h>

#include<graphics.h>

main()

{

int gd,gm;

int maxx,maxy;

detectgraph(&gd,&gm);

initgraph(&gd,&gm,” “);

maxx=  getmaxx();

maxy=getmaxy();

while (!kbhit())

{

line(0,0,maxx,maxy);

}

getch();

closegraph();

}

line(x1,y1,x2,y2) – draws a line.

#include<stdio.h>

#include<graphics.h>

main()

{

int gd,gm;

int maxx,maxy;

detectgraph(&gd,&gm);

initgraph(&gd,&gm,” “);

maxx=  getmaxx();

maxy=getmaxy();

while (!kbhit())

{

setfillstyle(4,6);

bar(10,10,100,100);

}

getch();

closegraph();

}

setfillstyle( fill-pattern-number, color)

bar – draw a rectangle with filled pattern.

Advertisements

From → C Graphics, Tutorials

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: