Advertisement

Graphics programming in C.

 (Read 13126 times)

Admin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1549
    • The Jucktion
Graphics programming in C.
« on: Oct 21, 2010, 01:24 PM »
Here we are going to learn the simple codes and syntax used to code graphics in C. I'm assuming that you're using Turbo C++. and you've activated the graphics feature through:

                              Options > Linker > Graphics

And you need to include conio.h and graphics.h to get the output.

This will enable you to include graphics.h and create graphics in your program.

To draw a rectangle
Syntax:

void main()
{  int gd=DETECT, gm;
initgraph(&gd, &gm, "c:/tc/bgi ");
rectangle (10,50,600,400);
getch(); closegraph();
restorecrtmode ();
}
Explanation: The syntax abobe creates a rectable with top left corner at (10,30) and The bottom right corner at (600,400). These two vertices are combined to create a single rectangle.

To draw a circle
Syntax:
void main()
{  int gd=DETECT, gm;
initgraph(&gd, &gm, "c:/tc/bgi ");
circle(330,180,100);
getch(); closegraph();
restorecrtmode ();
}
Explanation: The syntax abobe translate to circle(x-coordinate,y-coordinate,radius).


To draw a line
Syntax:
void main()
{  int gd=DETECT, gm;
initgraph(&gd, &gm, "c:/tc/bgi ");
line(10,50,500,50);
getch(); closegraph();
restorecrtmode ();
}
Explanation: The syntax above creates a horizontal line and  translate to circle(x-coordinate,y-coordinate,radius). You can create a vertical one with the co-ordinates.


To draw a ellipse
Syntax:

void main()
{  int gd=DETECT, gm;
initgraph(&gd, &gm, "c:/tc/bgi ");
ellipse(100,150,0,360,100,50);
getch(); closegraph();
restorecrtmode ();
}
Explanation: The syntax above translates to ellipse (x,y,stangle,endangle,xrad,yrad);


To draw a arc
Syntax:
void main()
{  int gd=DETECT, gm;
initgraph(&gd, &gm, "c:/tc/bgi ");
arc(120,160,300,90,70);
getch(); closegraph();
restorecrtmode ();
}
Explanation: The syntax above translates to  circle(x,y,stangle,endangle,rad). The (x,y ) creates a circle from tha point, and goes through stangle and endangle and rad refers to radius.


To draw a bar
Syntax:
 void main()
{  int gd=DETECT, gm;
initgraph(&gd, &gm, "c:/tc/bgi ");
bar(70,50,500,70);

getch(); closegraph();
restorecrtmode ();
}
Explanation: The syntax above translates to bar(x1,y1,x2,y2) similar to rectangle but fills in the area of the rectangle. The first (x1,y1) is top left co-ordinates, and the later (x2,y2) are bottom right co-ordinates.


putpixel
putpixel is used to put a pixel at a specific point.
Syntax:
putpixel(x,y,color);

Explanation: The x and y are co-ordinates and color represents the color that can be used in C like RED, BLUE, GREEN, etc.


COLORS IN C - 16 in total
BLACK
WHITE
YELLOW
BLUE
CYAN
GREEN
RED
MAGENTA
BROWN
LIGHTGRAY
DARKGRAY
LIGHTBLUE
LIGHTGREEN
LIGHTCYAN
LIGHTRED
LIGHTMAGENTA


I hope you've enjoyed the Tutorial. Now go program some great graphics designs in C

prateekshrestha

  • Jr. Jucktioner
  • *
  • Posts: 64
    • Buy Thangka Online
Re: Graphics programming in C.
« Reply #1 on: Nov 14, 2010, 07:27 AM »
I needed the color..thanks

xsisonlineistushar

  • Jr. Jucktioner
  • *
  • Posts: 10
Re: Graphics programming in C.
« Reply #2 on: Nov 24, 2010, 09:05 PM »
thanks.... nice post.....
appreciate the good work

Rose

  • Global Moderator
  • Hero Member
  • ****
  • Posts: 1047
Re: Graphics programming in C.
« Reply #3 on: Jun 02, 2012, 12:38 PM »
This is a nice tutorial but you can create much better graphics using the SDL library which is free.

Here is a brief tutorial on how you can setup SDL library and create graphics with C much easily. This has helped me so hope it'll help you guys too.

http://friedspace.com/cprogramming/cgraphics.php