I want to know about the C programming? What is the graphics in C programming? How can I create graphics for circle,rectangle and Triangle?
How can I build my application in C using graphics?
Printable View
I want to know about the C programming? What is the graphics in C programming? How can I create graphics for circle,rectangle and Triangle?
How can I build my application in C using graphics?
C is a basic purpose language that is developed by Dennis Ritchie at the AT&TBells Laboratories in 1972 for specially compatible with the Unix operating system and specially for designing system software but now a days it is being used by MS-DOS.
It is mother language of the newer programming languages .It was designed to be compiled using a relatively straightforward compiler that execute the code using top to down approach.
C supports a rich set of operators, which are symbols used within an expression and programmer can perform a lot of operation and calculation using variables and many more Like:
1. Basic datatypes and operators
2. Uinon and structure
3. File handling
4. Functions
5. Graphics
6. Pointers
using these provided facility,we can build a strong application
Using Graphics in C language
To make a good look of our application,we use the graphics that provides a lot of shapes that are achieved by graphics codes.In a C program you need to initialize the graphics to your computer which is controlled by in-build header file "graphics.h" into the library of C.
Graphics Mode initialization
First of all you need to call the initgraph function which takes the following form:
void initgraph(int far *grdriver, int far *grmode, char far *pathtodriver);
This prototype will set the graphics system using graphics driver from disk(validating a registered driver) .
Take a look of sample program which initialize graphics mode in C Language.
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
int grdriver = DETECT, grmode, errorcode;
initgraph(&grdriver, &grmode, "");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
line(0, 0, getmaxx(), getmaxy());
getch();
closegraph();
return 0;
}
Creating circle in graphics in C:
The circle command takes three parameters x coordinate ,y coordinate and radius which indicates x coordinate for Vertical axis, y coordinate for Horizontal axis and last one is radius of circle.We can construct it as follows:
#include<graphics.h>
#include<conio.h>
main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"");
circle(150,150,100);
getch();
closegraph();
}
Creating Rectangle in C
To create a rectangle in you need to initialize the graphics mode first and use rectangle function to draw a rectangle. This function takes four arguments left, top, right and bottom and every parameters should be in integer. A sample program which creates a rectangle:
#include<graphics.h>
#include<conio.h>
main()
{
int graphd=DETECT,graphm;
initgraph(&graphd,&graphm,"");
rectangle(100,80,100,390);
getch();
closegraph();
}