0% found this document useful (0 votes)
4 views

graphics

This tutorial introduces beginners to graphics programming in C, covering key concepts and basic functions using the Turbo C graphics library. It explains how to set up the environment, draw basic shapes, and implement common algorithms like DDA and Bresenham's for line drawing. The tutorial also offers tips for beginners and suggests exploring modern libraries for further development.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

graphics

This tutorial introduces beginners to graphics programming in C, covering key concepts and basic functions using the Turbo C graphics library. It explains how to set up the environment, draw basic shapes, and implement common algorithms like DDA and Bresenham's for line drawing. The tutorial also offers tips for beginners and suggests exploring modern libraries for further development.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

C Graphics Tutorial for Beginners

This tutorial introduces graphics programming in the C language. It follows


concepts from computer graphics textbooks and simplifies terms for beginners.

What is Computer Graphics?

Computer Graphics involves creating visual content using computers. It is widely


used in video games, simulations, animations, and graphical user interfaces
(GUIs).

Graphics in C

In C, graphics are typically implemented using libraries like Turbo C's graphics.h.
Although this library is outdated, it remains a useful tool for learning.

Setup

1. Turbo C Installation: Download and install the Turbo C++ IDE, which
includes the graphics.h library.

2. Include Library: Start your code by including the library:


#include <graphics.h>

Graphics Initialization: Use initgraph() to initialize the graphics mode:


int gd = DETECT, gm;

3. initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

o DETECT: Automatically detects the graphics driver.


o gm (Graphics Mode): Sets the resolution and color settings.

o BGI Path: Specifies the location of the BGI folder containing


graphics drivers.

4. Close Graphics: Use closegraph() to exit the graphics mode.

Basic Terms and Functions


Pixel

A pixel is the smallest unit of a graphic—a single point of color on the screen.
 Use putpixel(x, y, color) to draw a pixel at coordinates (x, y).
putpixel(100, 100, WHITE); // Draws a white pixel at (100, 100)

Line
A line connects two points.

 Use line(x1, y1, x2, y2) to draw a line.


line(50, 50, 200, 200); // Line from (50, 50) to (200, 200)

Circle

A circle is defined by its center and radius.

 Use circle(x, y, radius).


circle(100, 100, 50); // Circle centered at (100, 100) with radius 50

Rectangle

A rectangle is defined by two opposite corners.

 Use rectangle(left, top, right, bottom).


rectangle(50, 50, 150, 100); // Rectangle corners (50, 50) and (150, 100)

Ellipse

An ellipse is defined by its center, axes, and start/end angles.

 Use ellipse(x, y, start_angle, end_angle, x_radius, y_radius).


ellipse(100, 100, 0, 360, 50, 30); // Full ellipse centered at (100, 100)

Text

To display text on the screen:


 Use outtextxy(x, y, "Text").
outtextxy(100, 100, "Hello, Graphics!");

Colors
Colors are predefined constants like BLACK, WHITE, RED, etc. These can be
used as parameters in functions.

Example: Drawing Shapes

#include <graphics.h>

#include <conio.h>
int main() {

int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

// Draw shapes

circle(200, 200, 50);

rectangle(100, 100, 300, 200);


line(50, 50, 250, 250);

outtextxy(150, 300, "Graphics in C!");

getch(); // Wait for key press

closegraph();

return 0;

Understanding Coordinate System

The screen uses an (x, y) coordinate system:

 The top-left corner is (0, 0).

 x increases to the right, and y increases downward.

Common Algorithms in Graphics

1. DDA Line Drawing Algorithm


The Digital Differential Analyzer (DDA) algorithm calculates intermediate points
between two endpoints to draw a line.

Steps:

1. Calculate dx = x2 - x1 and dy = y2 - y1.


2. Determine the number of steps: steps = max(|dx|, |dy|).

3. Calculate increments: Xinc = dx/steps, Yinc = dy/steps.

4. Start from (x1, y1) and plot points incrementally.

Code:

void drawLineDDA(int x1, int y1, int x2, int y2) {

float dx = x2 - x1, dy = y2 - y1;

int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);

float Xinc = dx / steps, Yinc = dy / steps;


float x = x1, y = y1;

for (int i = 0; i <= steps; i++) {

putpixel(x, y, WHITE);

x += Xinc;

y += Yinc;

2. Bresenham's Line Drawing Algorithm

An efficient algorithm for drawing lines using integer calculations.

Steps:

1. Calculate dx, dy, and the decision parameter p.

2. Based on p, determine the next pixel.

Code:

void drawLineBresenham(int x1, int y1, int x2, int y2) {


int dx = x2 - x1, dy = y2 - y1;

int p = 2 * dy - dx;

int x = x1, y = y1;


while (x <= x2) {

putpixel(x, y, WHITE);

x++;

if (p < 0) {

p += 2 * dy;

} else {

p += 2 * (dy - dx);

y++;
}

3. Flood Fill Algorithm

This algorithm fills a closed shape with a specific color.

Steps:

1. Start at a seed pixel inside the shape.

2. Recursively fill neighboring pixels with the fill color until the boundary
color is reached.

Code:
void floodFill(int x, int y, int fillColor, int boundaryColor) {
if (getpixel(x, y) != boundaryColor && getpixel(x, y) != fillColor) {

putpixel(x, y, fillColor);

floodFill(x + 1, y, fillColor, boundaryColor);

floodFill(x - 1, y, fillColor, boundaryColor);

floodFill(x, y + 1, fillColor, boundaryColor);

floodFill(x, y - 1, fillColor, boundaryColor);

}
Advanced Topics

1. Clipping: Restricting drawing to a defined area.

2. Animations: Use loops to move shapes across the screen.

Tips for Beginners

 Experiment with each function to understand its behavior.

 Debug small sections of code to ensure correctness.


 Use paper to sketch shapes and plan coordinates.

Common Errors

1. Graphics Driver Error: Ensure the BGI path is correct.

2. Incorrect Coordinates: Verify that values match your intended design.

Moving Forward

This tutorial provides a foundational understanding of graphics programming in


C. For modern development, consider exploring libraries like OpenGL or SDL.

You might also like