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

Computer Graphics 1

The document provides an introduction to Turbo C graphics, detailing its features, advantages, and limitations. It covers the setup process, basic graphics functions, and examples for drawing shapes and text using the graphics.h library. Additionally, it includes a brief history of Turbo C and its development by Borland in the late 1980s.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Computer Graphics 1

The document provides an introduction to Turbo C graphics, detailing its features, advantages, and limitations. It covers the setup process, basic graphics functions, and examples for drawing shapes and text using the graphics.h library. Additionally, it includes a brief history of Turbo C and its development by Borland in the late 1980s.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Computer Graphics

Section1
Introduction to Turbo C Graphics
Table of contents

1. Introduction to Graphics
2. What is Turbo C?
3. Advantages and Disadvantages
4. History
5. Setting up
6. Structure of code
7. Basic graphics function
8. Assignment 1
Introduction to Graphics

● Graphics enable drawing shapes like lines, circles, rectangles, bars,


arc and more.
● It can display text in different colors, fonts and size.
● It can be used to make games, animation projects and GUI
applications.
What is Turbo c ?

● Turbo C provides an integrated development environment (IDE) with


a built-in compiler for writing and executing C programs.
● It was developed by Borland in the late 1980s.
● Turbo C supports basic graphics programming using the graphics.h
library. This library contains functions that allow developers to
create graphical applications using simple shapes, colors, and
transformations.
Why Learn Turbo C Graphics?

● Turbo C is lightweight, with a size of around 3.43 MB, making it easy


to install and run.
● The compiler is optimized for speed, allowing programs to compile
and execute quickly.
● Simple and user-friendly interface makes it easy to use.
● Comes with built-in tools like a linker and an offline help file for
quick reference.
Limitations of Turbo C Graphics

● Turbo C is outdated, as it does not support modern C/C++


standards.
● Its graphics capabilities are limited, relying on the basic graphics.h
library.
● It is not compatible with modern operating systems, requiring
DOSBox or similar emulators to run.
History
1. Version 1.0 (May 13, 1987) – First Turbo C release with an integrated
edit-compile-run environment for IBM PCs. Originally developed by
Bob Jervis and rebranded by Borland.
2. Version 1.5 (January 1988) – Added more sample programs, improved
manuals, bug fixes, and the Conio.h header.
3. Version 2.0 (1989) – Introduced the blue screen interface, which
became the standard for future Borland MS-DOS releases.
Setting up Turbo C

● https://www.mediafire.com/file/x6jhtoes5pdl6dz/Setup_TurboC_
7_v2.1.rar/file

● Options → Linker → Libraries → Enable Graphic library.


Structure of code
#include<stdio.h>
#include <graphics.h>
#include <conio.h>
int main() {
int gdriver = DETECT, gmode,errorcode;
initgraph(& gdriver, & gmode, "C:\TC\BGI");
/*
Block of code
*/
getch();
closegraph();
return 0;
}
graphics.h
● To design graphics, first we must include graphics.h header file.
● graphics.h → header file contains many predefined functions which
are used to implement GUI applications.
Basic graphics function
Initializing the graphics
● The first step in any graphics is to initialize the graphics drivers on
computer.
● initgraph() → function initialize the graphics system by loading a
graphics driver from disk then putting the system into graphics
mode.

void initgraph( int *graphicDriver, int *graphicMode, char *driverPath);


graphicDriver
It tells the compiler that what graphics driver to use or to automatically detect
the driver. We will use DETECT for auto detection of graphic driver.

graphicMode
It specifies the graphic mode to be used. If graphicDriver is set to DETECT,
Then graphicMode is set to the highest resolution available for the
detected driver.
driverPath
It specifies the directory path where graphics driver files (BGI files) are located.
Closing the graphics
● After work done with “Graphics mode”, it must be closed using a
pre-defined function closegraph().
● closegraph() → it deallocates all memory allocated by graphics
system and restores the screen to text mode.

void closegraph();

Note: getch(); helps us to wait until a key is pressed.


Graphics coordinate system
X
(0,0)
X – axis
Y

(X,Y)

Y – axis
Printing text

● outtext() and outtextxy()


● Similar to printf(), but printf() prints text on Text mode while outtext()
and outtxetxy() prints text on Graphics mode.

void outtext(char *string);


void outtextxy(int x, int y, char *string);
Example

1. outtext(“Hello World”);
2. outtextxy(100,100,”Hello World”);
3. char str[] = “Hello World”;
outtextxy(100,100,str);
Drawing a straight line

● line() → is used to draw a line from point (x1,y1) to point (x2,y2).

void line(int x1, int y1, int x2, int y2);


Example

1. line(50,50,100,100);
2. line(50,50,100,50); → Horizontal line.
3. line(50,20,50,100); → Vertical line
Drawing a Rectangle

● rectangle() → is used to draw a rectangle.

void rectangle(int left, int top, int right, int bottom);

● left → the x-coordinate of top left corner.


● top → the y-coordinate of top left corner.
● right → the x-coordinate of right bottom corner.
● bottom → the y-coordinate of right bottom corner.
Drawing a Rectangle
(0,0) x1 (left) x2 (right)
X – axis

y1 (top)

y2 (bottom)

Y – axis void rectangle(int left, int top, int right, int bottom);
Example

1. rectangle(50,50,100,100);
2. rectangle(100,50,100,50); → point.
3. rectangle(100,50,200,50); → line.
Drawing a Circle

● circle() → is used to draw a circle with center and radius.


x → the x-coordinate of center.
y → the y-coordinate of center.
and the radius of the circle.

void circle(int x, int y, int radius);


Example

1. circle(100,100,50);
2. circle(50,50,100);

50
50
(100,100)
Drawing an Arc

● arc() → is used to draw an arc with center, starting angle, ending


angle and radius.
x → the x-coordinate of center.
y → the y-coordinate of center.

void arc(int x, int y, int stangle, int endangle, int radius);


Example

1. arc(100,100,0,90,50); 90

50
50 0
180 360
(100,100)

270
1. arc(50,50,90,360,30);
2. arc(100,100,0,135,50);
Drawing an Ellipse

● ellipse() → is used to draw an ellipse and elliptical arc.


x and y → are coordinates of center of the ellipse
stangle → starting angle.
endangle → ending angle
xrad and yrad → x and y radius of the ellipse.

void ellipse(int x, int y, int stangle, int endangle, int xrad, int yrad);
Example

1. ellipse(100,100,0,360,50,25);
90

25
0
180
(100,100) 50 360

270

2. ellipse(100,100,90,270,50,25);
Drawing filled Ellipse

● fillellipse() → is used to draw filled ellipse.


x and y → are coordinates of center of the ellipse
xrad and yrad → x and y radius of the ellipse.

void fillellipse(int x, int y, int xrad, int yrad);


Example

1. fillellipse(100,100,50,25);

25

(100,100) 50
Set color

● setcolor() → is used to change the current drawing color.

void setcolor(int color);

● Total 16 colors are available. Each color is assigned to a number.


● Black is assigned to 0.
● Default drawing color is WHITE.
Int Int Int Int
Color Color Color Color
value value value value

BLACK 0 MAGENTA 5 LIGHTGREEN 10 WHITE 15

BLUE 1 BROWN 6 LIGHTCYAN 11

GREEN 2 LIGHTGRAY 7 LIGHTRED 12

CYAN 3 DARKGRAY 8 LIGHTMAGENTA 13

RED 4 LIGHTBLEU 9 YALLOW 14


Example

● circle(100,100,50);
● setcolor(RED);
● circle(200,200,50);
Set Background color

● setbkcolor() → is used to change the current background color.

void setbkcolor(int color);

● Default backgroung color is BLACK.


● Example:
○ setbkcolor(GREEN); or setbkcolor(2); → changes the current
background color to GREEN.
Clear device

● cleardevice() → is used to clear the screen in graphics mode and


sets the current position to (0,0).
● Clearing the screen consists of filling the screen with current
background color.

void cleardevice( );
Thanks!

You might also like