Introduction to Graphics in C
By Paribesh Sapkota
Introduction to Graphics in C
Graphics programming in C involves creating visual elements on the screen, such as shapes, images, and animations. This capability allows for the development of applications ranging from simple drawings to complex graphical user interfaces and games.
Concepts of Graphics
- Pixel: The smallest unit of a digital image or display. Graphics are composed of thousands or millions of pixels arranged in a grid.
- Coordinate System: In graphics programming, the screen is considered a coordinate plane where each pixel has a specific position defined by x (horizontal) and y (vertical) coordinates.
- Graphics Mode: A mode that allows the program to draw shapes, text, and images on the screen. This is different from text mode, where the screen is treated as a grid of characters.
Graphics Initialization and Modes
To work with graphics in C, particularly with the Borland Graphics Interface (BGI) library in Turbo C or Turbo C++, you need to initialize the graphics mode and set up the graphics environment.
Initializing Graphics
- Including Graphics Library: Include the graphics library header file.
#include <graphics.h> #include <conio.h> // For getch()
2. Setting Up Graphics Mode:
int gd
: Graphics driver (e.g.,DETECT
to automatically detect the appropriate driver).int gm
: Graphics mode (e.g.,DETECT
to automatically select the highest resolution mode).
int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
initgraph
: Initializes the graphics system by loading the graphics driver.
3. Closing Graphics: Close the graphics mode and return to text mode.
closegraph();
Graphics Functions
The BGI library provides various functions to draw shapes, set colors, and manipulate the screen.
Drawing Shapes
- Line:
void line(int x1, int y1, int x2, int y2);
Draws a line from
(x1, y1)
to(x2, y2)
.line(100, 100, 200, 200);
2. Circle:
void circle(int x, int y, int radius);
Draws a circle with center (x, y)
and given radius
.
circle(150, 150, 50);
3.Rectangle:
void rectangle(int left, int top, int right, int bottom);
Draws a rectangle with the top-left corner at (left, top)
and bottom-right corner at (right, bottom)
.
rectangle(100, 100, 200, 200);
4.Ellipse:
void ellipse(int x, int y, int start_angle, int end_angle, int x_radius, int y_radius);
Draws an ellipse with center (x, y)
, starting angle start_angle
, ending angle end_angle
, and radii x_radius
and y_radius
.
ellipse(200, 200, 0, 360, 100, 50);
Setting Colors
- Set Color:
void setcolor(int color);
Sets the current drawing color.
setcolor(RED); line(100, 100, 200, 200);
2. Set Background Color:
void setbkcolor(int color);
Filling Shapes
- Flood Fill:
void floodfill(int x, int y, int border);
2. Set Fill Style:
void setfillstyle(int pattern, int color);
Example Program
Here is an example program that demonstrates initializing graphics, drawing shapes, and setting colors:
#include <graphics.h> #include <conio.h> int main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Set background color setbkcolor(BLACK); cleardevice(); // Draw shapes with different colors setcolor(WHITE); line(100, 100, 200, 200); setcolor(RED); circle(300, 300, 50); setcolor(GREEN); rectangle(150, 150, 250, 250); setcolor(YELLOW); ellipse(400, 200, 0, 360, 100, 50); // Fill the rectangle with blue color setfillstyle(SOLID_FILL, BLUE); floodfill(200, 200, GREEN); getch(); // Wait for user input to close the graphics window closegraph(); // Close the graphics mode return 0; }
Write a Program to draw basic graphics construction like line, circle, arc, ellipse and rectangle.
#include<graphics.h> #include<conio.h> void main() { intgd=DETECT,gm; initgraph (&gd,&gm,"c:\\tc\\bgi"); setbkcolor(GREEN); printf("\t\t\t\n\nLINE"); line(50,40,190,40); printf("\t\t\n\n\n\nRECTANGLE"); rectangle(125,115,215,165); printf("\t\t\t\n\n\n\n\n\n\nARC"); arc(120,200,180,0,30); printf("\t\n\n\n\nCIRCLE"); circle(120,270,30); printf("\t\n\n\n\nECLIPSE"); ellipse(120,350,0,360,30,20); getch(); }
Program to make screen saver in that display different size circles filled with different colors and at random places.
#include<stdio.h> #include<conio.h> #include"graphics.h" #include"stdlib.h" void main() { intgd=DETECT,gm,i=0,x,xx,y,yy,r; //Initializes the graphics system initgraph(&gd,&gm,"c:\\tc\\bgi"); x=getmaxx(); y=getmaxy(); while(!kbhit()) { i++; // setfillstyle(random(i),random(30)); circle(xx=random(x),yy=random(y),random(30)); setfillstyle(random(i),random(30)); floodfill(xx,yy,getmaxcolor()); delay(200); } getch(); }
Write a Program control a ball using arrow keys.
#include<graphics.h> #include<stdio.h> void main() { intgd=DETECT,gm,x,y,r=40; charch; initgraph(&gd,&gm,"C:/TURBOC3/BGI"); setbkcolor(3); x=getmaxx()/2; y=getmaxy()/2; setfillstyle(1,RED); circle(x,y,r); floodfill(x,y,getmaxcolor()); while((ch=getch())!=13) { switch(ch) { case 75: if(x>=r+1) { cleardevice(); circle(x-=10,y,r); floodfill(x,y,getmaxcolor()); } break; case 72: if(y>=r+1) { cleardevice(); circle(x,y-=10,r); floodfill(x,y,getmaxcolor()); } break; case 77: if(x<=(getmaxx()-r-10)) { cleardevice(); circle(x+=10,y,r); floodfill(x,y,getmaxcolor()); } break; case 80: if(y<=(getmaxy()-r-10)) { cleardevice(); circle(x,y+=10,r); floodfill(x,y,getmaxcolor()); } } } getch(); }
Write a program of Translation, Rotation, and Scaling using Composite Transformation.
#include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> int x1,y1,x2,y2,x3,y3,a,b; void draw(); void rotate(); int main(void) { int gd=DETECT,gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); printf("Enter first co-ordinate value for triangle:"); scanf("%d%d",&x1,&y1); printf("Enter second co-ordinatevalues for triangle:"); scanf("%d%d",&x2,&y2); printf("Enter third co-ordinate valuesfor triangle:"); scanf("%d%d",&x3,&y3); draw(); getch(); rotate(); getch(); return 0; } void draw() { line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); } void rotate() { int a1,a2,a3,b1,b2,b3; float angle; printf("Enter the rotation angle co-ordinates:"); scanf("%f",&angle); cleardevice(); angle=(angle*3.14)/180; a1=a+(x1-a)*cos(angle)-(y1-b)*sin(angle); b1=b+(x1-a)*sin(angle)+(y2-b)*cos(angle); a2=a+(x2-a)*cos(angle)-(y1-b)*sin(angle); b2=b+(x2-a)*sin(angle)+(y2-b)*cos(angle); a3=a+(x3-a)*cos(angle)-(y1-b)*sin(angle); b3=b+(x3-a)*sin(angle)+(y2-b)*cos(angle); printf("ROTATION"); printf("\n Changed coordinates\n"); printf("%d %d\n%d %d\n%d %d",a1,b1,a2,b2,a3,b3); line(a1,b1,a2,b2); line(a2,b2,a3,b3); line(a3,b3,a1,b1); }
Write a program to draw a Circle using midpoint implementation Method.
#include<stdio.h> #include<graphics.h> void drawcircle(int x0, int y0, int radius) { int x = radius; int y = 0; int err = 0; while (x >= y) { putpixel(x0 + x, y0 + y, 7); putpixel(x0 + y, y0 + x, 7); putpixel(x0 - y, y0 + x, 7); putpixel(x0 - x, y0 + y, 7); putpixel(x0 - x, y0 - y, 7); putpixel(x0 - y, y0 - x, 7); putpixel(x0 + y, y0 - x, 7); putpixel(x0 + x, y0 - y, 7); if (err <= 0) { y += 1; err += 2*y + 1; } if (err > 0) { x -= 1; err -= 2*x + 1; } } } void main() { int gdriver=DETECT, gmode, error, x, y, r; initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi"); printf("Enter radius of circle: "); scanf("%d", &r); printf("Enter co-ordinates of center(x and y): "); scanf("%d%d", &x, &y); drawcircle(x, y, r); getch(); }