SlideShare une entreprise Scribd logo
1  sur  22
Unit 11
Graphics
Intro
• There are two modes of the standard output device:
• Text Mode
• Graphics Mode
• The programming for video games, animation and multimedia is
difficult in text mode as they predominantly work with computer
graphics.
• In graphics mode we work with tiny dots on the screen called pixels
(picture elements)
Ashim Lamichhane 2
Intro..
• The pixels are even present in text mode as they are used to form
characters that appear on the screen with only difference that they are
predefined pattern of pixels.
• However, the graphics mode provides the ability to manipulate the
individual pixels.
• To work with graphics, we can use graphics.h header file
Ashim Lamichhane 3
Graphics Characteristics
1. Pixels
• Short for Picture Element, a pixel is a single point (i.e. dot) in graphic image.
• Graphics monitor displays pictures by dividing the display screen into thousands of
pixels arranged in rows and columns.
• The pixels are so close together that they appear connected.
• The computer screen is a two dimensional; each pixel on the screen has some
location, illustrated by x and y values
• x is the horizontal offset from the left side of the screen
• y is the vertical offset from the top of the screen.
• So x=0 and y=0 represent top left corner of computer screen.
Ashim Lamichhane 4
Fig. representation of co-ordinates on computer screen
Ashim Lamichhane 5
(0,0)
(200,0)
y
(0,479)
(639,0)
x
(320,240)
(639,450)
(639,479)
Suppose to plot a pixel on the
Screen at say x=70 and y=100,
We’d put pixel information into
that two dimensional array,
at element array [100][70]
2. Resolution
• The number of pixels used on the screen is called resolution.
• There are fixed number of rows and each rows contains certain numbers of
pixels
• The frequently used resolutions supported by most of adapters are 640*480,
800*600,1024*768, 1152*864, 1220*1024 etc.
• The resolution 640*480 (640 by 480) means that there are 640 pixels in
horizontal direction(i.e. x-axis) and 480 pixels in vertical direction (i.e y-axis).
• In general for higher resolution, the picture is more pleasing.
Ashim Lamichhane 6
3. Colors
• Some graphics modes support more colors than other ranging from 2 to
millions colors.
• A particular mode may support only two colors at a time while other my
support 256 colors.
• These groups of colors are known as color palettes.
Ashim Lamichhane 7
4. Video Adapters
• Video adapters are drivers for display
• Each video adapter handles graphics in different way.
• Once a video adapter is initialized by the program for particular
graphics mode then it can use it to plot various elements as well as to
display text in different fonts.
• Some examples of video adapters are CGA, VGA and EGA
Ashim Lamichhane 8
Initializing Graphics Hardware
• The built in function initgraph() is used to initialize the graphics system
and put the computer screen in specified graphics mode.
• initgraph() initializes the graphics system by loading a graphics driver
from disk then putting the system into graphics mode.
• It also resets all graphics settings(color, palette, current position etc) to
their defaults.
• Ex:
initgraph(&graphics_driver,&graphics_mode,”path_to_driver”);
Ashim Lamichhane 9
initgraph(&graphics_driver,&graphics_mode,”path_to_driver”);
• graphics_driver is a variable of type int initialized to some constants that is
defined in graphics.h header file. It species the graphics driver to be used.
• C offers certain graphics drivers and these are the files with .BGI extension.
• Depending on what adapter is used, one of these drivers gets selected.
• Some constants defined in graphics.h file for this argument are
• DETECT(=0)
• CGA(=1)
• EGA(=3)
• EGA64(=4)
• EGAMONO(=5)
• VGA(=9)
Ashim Lamichhane 10
initgraph(&graphics_driver,&graphics_mode,”path_to_driver”);
• graphics_mode is also a type int that is initialized to the mode for the
particular video adapter to use.
• This variable tells which monitor we are using and its resolution, the
number of video pages it supports and the colors that are available.
• The third argument is path_to_driver is string constant that specifies
the directory containing .bgi files and .chr files.
• Its value may be like “C:TCBGI”
Ashim Lamichhane 11
Auto-Initialization of Graphics Hardware
• In the use of initgraph() function we have explicitly told initgraph()
what graphics driver and mode to use by assigning the values to driver
and mode arguments.
• It is possible to let the program to find out the video adapter installed
in the computer and use the best driver and mode.
• i.e. the combination that gives the highest resolution.
Ashim Lamichhane 12
• There are two approaches for auto initialization of graphics hardware.
• DETECT is used for driver argument. In this method program doesn’t
know in advance what mode will be used and cannot assume anything
about the resolution. Ex.
int gd,gm;
gd=DETECT;
initgraph(gd,&gm,”C:TCBGI”);
• A function called detectgraph() is used that returns value for the
best driver and mode.
int gd,gm;
Detectgraph(&gd,&gm);
initgraph(&gd,&gm,”C:TCBGI”);
Ashim Lamichhane 13
Closing Graphic Mode
• Once the program has finished its job using the graphics facilities, then
it should restore the system to the mode that was previously in use.
• If graphics mode is not closed explicitly by the programmer,
undesirable effects may be felt.
• The closegraph() function is used to restore the screen to the mode it
was in before we called initgraph() and deallocates all memory
allocated by the graphics system.
Ashim Lamichhane 14
Observation of graphics result
• The library function graphresult() is used to determine whether a
certain graphics operation succeeded or not.
• This function returns an error code for the last unsuccessful graphics
operation.
• grOk represents that there is no error. The variable maintained by
graphresult is reset to 0 after graphresult has been called.
• Therefore we should store the value of graphresult into a temporary
variable and then test it.
Ashim Lamichhane 15
Library Functions
• Plotting and getting points
• putpixel()
• Plots a point with a specified color
putpixel(int x, int y, int color);
• getpixel()
• Gets color of specified pixel
integer_variable=getpixel(int x, int y);
• Changing drawing/foreground and background color
• setcolor(): changes current fg color
setcolor(int color);
• stbkcolor(): changes current bg color
Setbkcolor(int color);
Ashim Lamichhane 16
• Drawing Lines
• line()
• Draws line from point having co-ordinate x1, y1 to x2, y2 using current settings for the line
drawing and current drawing color.
line(int x1, int x1, int x2, int y2);
Similarly
setlinestyle(int style, unsigned int pattern, int thickness);
• lineto()
• It draws a line from current position to point (x,y).
• The current position of point can be changed using moveto(x,y) function
lineto(int x, int y);
And
moveto(int x1,y1);
• linerel()
• Draws a line a relative distance from current position
linerel(int dx, int dy);
Ashim Lamichhane 17
• Drawing Shapes
• circle()
• Draws a circle having center point (x,y) and radius r with current color.
circle(int x,int y, int r);
• ellipse()
• Draws an ellipse with current color
ellipse(int x,int y, int startAngle, int endAngle, int xRadius,int yRadius);
• arc()
• Draws a circular arc in a portion of circle
arc(int x, int y, int startAngle, int endAngle, int radius);
• rectangle()
• Draws rectangle from two end points of a diagonal of the rectangle
reactangle(int x1, int y1, int x2, int y2);
Ashim Lamichhane 18
• drawpoly(): draws the outline of a polygon using required points
• fillpoly(): draws and fills polygon
• Draws the outline of a polygon using required points
drawpoly(int numberOfPoints, int points[]);
fillpoly(int numberOfPoints, int points[]);
• To draw a closed polygon with N vertices we must pass N+1 co-ordinates to
drawpoly() or fillpoly() where N+1th co-ordinate must be same as first co-
ordinate.
• Thus to draw hexagon we need seven points where first and seventh point is
same
Ashim Lamichhane 19
• Displaying text in graphics mode
• outtext(): it displays the string at the current position
outtext(string text);
• outtextxy(): it displays the string at point(x,y)
outtextxy(int x, int y, string text);
• settextstyle(): it changes font, size and direction of characters.
settextstyle(int font, int direction, int size);
Ashim Lamichhane 20
• pieslice(): draws a sector with current color and font setting
pieslice(int x,int y,int startAngle,int endAngle, int r);
• bar():
• draws bar diagram using two points left-top corner and bottom corner
bar(int left, int top, int right, int bottom);
• getmaxx()
• Returns max x value for current graphics driver and mode
integer_variable=getmaxx()
• getmaxy()
• Returns max y value for current graphics driver and mode
integer_variable=getmaxy()
Ashim Lamichhane 21
END
Ashim Lamichhane 22

Contenu connexe

Tendances

applications of computer graphics
applications of computer graphicsapplications of computer graphics
applications of computer graphics
Aaina Katyal
 
Constants, Variables and Data Types in Java
Constants, Variables and Data Types in JavaConstants, Variables and Data Types in Java
Constants, Variables and Data Types in Java
Abhilash Nair
 

Tendances (20)

Computer graphics notes
Computer graphics notesComputer graphics notes
Computer graphics notes
 
Unit 2. Elements of C
Unit 2. Elements of CUnit 2. Elements of C
Unit 2. Elements of C
 
ITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in javaITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in java
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
applications of computer graphics
applications of computer graphicsapplications of computer graphics
applications of computer graphics
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
 
Python GUI Programming
Python GUI ProgrammingPython GUI Programming
Python GUI Programming
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Preprocessor directives in c language
Preprocessor directives in c languagePreprocessor directives in c language
Preprocessor directives in c language
 
Control structure
Control structureControl structure
Control structure
 
Applications Of Computer Graphics
Applications Of Computer GraphicsApplications Of Computer Graphics
Applications Of Computer Graphics
 
Built in function
Built in functionBuilt in function
Built in function
 
Introduction to graphics programming in c
Introduction to graphics programming in cIntroduction to graphics programming in c
Introduction to graphics programming in c
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
 
String c
String cString c
String c
 
C programming - String
C programming - StringC programming - String
C programming - String
 
Constants, Variables and Data Types in Java
Constants, Variables and Data Types in JavaConstants, Variables and Data Types in Java
Constants, Variables and Data Types in Java
 
computer graphics
computer graphicscomputer graphics
computer graphics
 
Python GUI
Python GUIPython GUI
Python GUI
 

En vedette

En vedette (14)

UNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CUNIT 10. Files and file handling in C
UNIT 10. Files and file handling in C
 
Unit 8. Pointers
Unit 8. PointersUnit 8. Pointers
Unit 8. Pointers
 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
 
File handling in c
File handling in c File handling in c
File handling in c
 
File handling in c
File handling in cFile handling in c
File handling in c
 
File in C Programming
File in C ProgrammingFile in C Programming
File in C Programming
 
File handling in 'C'
File handling in 'C'File handling in 'C'
File handling in 'C'
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 
File in c
File in cFile in c
File in c
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
 
Unit 5. Control Statement
Unit 5. Control StatementUnit 5. Control Statement
Unit 5. Control Statement
 
Unit 4. Operators and Expression
Unit 4. Operators and Expression  Unit 4. Operators and Expression
Unit 4. Operators and Expression
 
Unit 3. Input and Output
Unit 3. Input and OutputUnit 3. Input and Output
Unit 3. Input and Output
 
File handling in C
File handling in CFile handling in C
File handling in C
 

Similaire à Unit 11. Graphics

Computer graphics
Computer graphicsComputer graphics
Computer graphics
amitsarda3
 
Smedberg niklas bringing_aaa_graphics
Smedberg niklas bringing_aaa_graphicsSmedberg niklas bringing_aaa_graphics
Smedberg niklas bringing_aaa_graphics
changehee lee
 

Similaire à Unit 11. Graphics (20)

C Graphics Functions
C Graphics FunctionsC Graphics Functions
C Graphics Functions
 
Cg lab cse-v (1) (1)
Cg lab cse-v (1) (1)Cg lab cse-v (1) (1)
Cg lab cse-v (1) (1)
 
Introduction to Computer graphics
Introduction to Computer graphicsIntroduction to Computer graphics
Introduction to Computer graphics
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Graphics mod
Graphics modGraphics mod
Graphics mod
 
Prinsip gambar digital
Prinsip gambar digitalPrinsip gambar digital
Prinsip gambar digital
 
Chapter-3.pdf
Chapter-3.pdfChapter-3.pdf
Chapter-3.pdf
 
Computer Graphics
Computer GraphicsComputer Graphics
Computer Graphics
 
Overview of graphics systems
Overview of  graphics systemsOverview of  graphics systems
Overview of graphics systems
 
Computer graphics
Computer graphics Computer graphics
Computer graphics
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
 
3 CG_U1_P2_PPT_3 OpenGL.pptx
3 CG_U1_P2_PPT_3 OpenGL.pptx3 CG_U1_P2_PPT_3 OpenGL.pptx
3 CG_U1_P2_PPT_3 OpenGL.pptx
 
Programming with OpenGL
Programming with OpenGLProgramming with OpenGL
Programming with OpenGL
 
Smedberg niklas bringing_aaa_graphics
Smedberg niklas bringing_aaa_graphicsSmedberg niklas bringing_aaa_graphics
Smedberg niklas bringing_aaa_graphics
 
Overview of graphics systems.ppt
Overview of graphics systems.pptOverview of graphics systems.ppt
Overview of graphics systems.ppt
 
Applet in java
Applet in javaApplet in java
Applet in java
 
Buffers
BuffersBuffers
Buffers
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
 
Данило Ульянич “C89 OpenGL for ARM microcontrollers on Cortex-M. Basic functi...
Данило Ульянич “C89 OpenGL for ARM microcontrollers on Cortex-M. Basic functi...Данило Ульянич “C89 OpenGL for ARM microcontrollers on Cortex-M. Basic functi...
Данило Ульянич “C89 OpenGL for ARM microcontrollers on Cortex-M. Basic functi...
 
2D graphics
2D graphics2D graphics
2D graphics
 

Plus de Ashim Lamichhane

Plus de Ashim Lamichhane (10)

Searching
SearchingSearching
Searching
 
Sorting
SortingSorting
Sorting
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Linked List
Linked ListLinked List
Linked List
 
Queues
QueuesQueues
Queues
 
The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
 
Algorithm big o
Algorithm big oAlgorithm big o
Algorithm big o
 
Algorithm Introduction
Algorithm IntroductionAlgorithm Introduction
Algorithm Introduction
 
Introduction to data_structure
Introduction to data_structureIntroduction to data_structure
Introduction to data_structure
 
Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer   Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer
 

Dernier

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Dernier (20)

Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 

Unit 11. Graphics

  • 2. Intro • There are two modes of the standard output device: • Text Mode • Graphics Mode • The programming for video games, animation and multimedia is difficult in text mode as they predominantly work with computer graphics. • In graphics mode we work with tiny dots on the screen called pixels (picture elements) Ashim Lamichhane 2
  • 3. Intro.. • The pixels are even present in text mode as they are used to form characters that appear on the screen with only difference that they are predefined pattern of pixels. • However, the graphics mode provides the ability to manipulate the individual pixels. • To work with graphics, we can use graphics.h header file Ashim Lamichhane 3
  • 4. Graphics Characteristics 1. Pixels • Short for Picture Element, a pixel is a single point (i.e. dot) in graphic image. • Graphics monitor displays pictures by dividing the display screen into thousands of pixels arranged in rows and columns. • The pixels are so close together that they appear connected. • The computer screen is a two dimensional; each pixel on the screen has some location, illustrated by x and y values • x is the horizontal offset from the left side of the screen • y is the vertical offset from the top of the screen. • So x=0 and y=0 represent top left corner of computer screen. Ashim Lamichhane 4
  • 5. Fig. representation of co-ordinates on computer screen Ashim Lamichhane 5 (0,0) (200,0) y (0,479) (639,0) x (320,240) (639,450) (639,479) Suppose to plot a pixel on the Screen at say x=70 and y=100, We’d put pixel information into that two dimensional array, at element array [100][70]
  • 6. 2. Resolution • The number of pixels used on the screen is called resolution. • There are fixed number of rows and each rows contains certain numbers of pixels • The frequently used resolutions supported by most of adapters are 640*480, 800*600,1024*768, 1152*864, 1220*1024 etc. • The resolution 640*480 (640 by 480) means that there are 640 pixels in horizontal direction(i.e. x-axis) and 480 pixels in vertical direction (i.e y-axis). • In general for higher resolution, the picture is more pleasing. Ashim Lamichhane 6
  • 7. 3. Colors • Some graphics modes support more colors than other ranging from 2 to millions colors. • A particular mode may support only two colors at a time while other my support 256 colors. • These groups of colors are known as color palettes. Ashim Lamichhane 7
  • 8. 4. Video Adapters • Video adapters are drivers for display • Each video adapter handles graphics in different way. • Once a video adapter is initialized by the program for particular graphics mode then it can use it to plot various elements as well as to display text in different fonts. • Some examples of video adapters are CGA, VGA and EGA Ashim Lamichhane 8
  • 9. Initializing Graphics Hardware • The built in function initgraph() is used to initialize the graphics system and put the computer screen in specified graphics mode. • initgraph() initializes the graphics system by loading a graphics driver from disk then putting the system into graphics mode. • It also resets all graphics settings(color, palette, current position etc) to their defaults. • Ex: initgraph(&graphics_driver,&graphics_mode,”path_to_driver”); Ashim Lamichhane 9
  • 10. initgraph(&graphics_driver,&graphics_mode,”path_to_driver”); • graphics_driver is a variable of type int initialized to some constants that is defined in graphics.h header file. It species the graphics driver to be used. • C offers certain graphics drivers and these are the files with .BGI extension. • Depending on what adapter is used, one of these drivers gets selected. • Some constants defined in graphics.h file for this argument are • DETECT(=0) • CGA(=1) • EGA(=3) • EGA64(=4) • EGAMONO(=5) • VGA(=9) Ashim Lamichhane 10
  • 11. initgraph(&graphics_driver,&graphics_mode,”path_to_driver”); • graphics_mode is also a type int that is initialized to the mode for the particular video adapter to use. • This variable tells which monitor we are using and its resolution, the number of video pages it supports and the colors that are available. • The third argument is path_to_driver is string constant that specifies the directory containing .bgi files and .chr files. • Its value may be like “C:TCBGI” Ashim Lamichhane 11
  • 12. Auto-Initialization of Graphics Hardware • In the use of initgraph() function we have explicitly told initgraph() what graphics driver and mode to use by assigning the values to driver and mode arguments. • It is possible to let the program to find out the video adapter installed in the computer and use the best driver and mode. • i.e. the combination that gives the highest resolution. Ashim Lamichhane 12
  • 13. • There are two approaches for auto initialization of graphics hardware. • DETECT is used for driver argument. In this method program doesn’t know in advance what mode will be used and cannot assume anything about the resolution. Ex. int gd,gm; gd=DETECT; initgraph(gd,&gm,”C:TCBGI”); • A function called detectgraph() is used that returns value for the best driver and mode. int gd,gm; Detectgraph(&gd,&gm); initgraph(&gd,&gm,”C:TCBGI”); Ashim Lamichhane 13
  • 14. Closing Graphic Mode • Once the program has finished its job using the graphics facilities, then it should restore the system to the mode that was previously in use. • If graphics mode is not closed explicitly by the programmer, undesirable effects may be felt. • The closegraph() function is used to restore the screen to the mode it was in before we called initgraph() and deallocates all memory allocated by the graphics system. Ashim Lamichhane 14
  • 15. Observation of graphics result • The library function graphresult() is used to determine whether a certain graphics operation succeeded or not. • This function returns an error code for the last unsuccessful graphics operation. • grOk represents that there is no error. The variable maintained by graphresult is reset to 0 after graphresult has been called. • Therefore we should store the value of graphresult into a temporary variable and then test it. Ashim Lamichhane 15
  • 16. Library Functions • Plotting and getting points • putpixel() • Plots a point with a specified color putpixel(int x, int y, int color); • getpixel() • Gets color of specified pixel integer_variable=getpixel(int x, int y); • Changing drawing/foreground and background color • setcolor(): changes current fg color setcolor(int color); • stbkcolor(): changes current bg color Setbkcolor(int color); Ashim Lamichhane 16
  • 17. • Drawing Lines • line() • Draws line from point having co-ordinate x1, y1 to x2, y2 using current settings for the line drawing and current drawing color. line(int x1, int x1, int x2, int y2); Similarly setlinestyle(int style, unsigned int pattern, int thickness); • lineto() • It draws a line from current position to point (x,y). • The current position of point can be changed using moveto(x,y) function lineto(int x, int y); And moveto(int x1,y1); • linerel() • Draws a line a relative distance from current position linerel(int dx, int dy); Ashim Lamichhane 17
  • 18. • Drawing Shapes • circle() • Draws a circle having center point (x,y) and radius r with current color. circle(int x,int y, int r); • ellipse() • Draws an ellipse with current color ellipse(int x,int y, int startAngle, int endAngle, int xRadius,int yRadius); • arc() • Draws a circular arc in a portion of circle arc(int x, int y, int startAngle, int endAngle, int radius); • rectangle() • Draws rectangle from two end points of a diagonal of the rectangle reactangle(int x1, int y1, int x2, int y2); Ashim Lamichhane 18
  • 19. • drawpoly(): draws the outline of a polygon using required points • fillpoly(): draws and fills polygon • Draws the outline of a polygon using required points drawpoly(int numberOfPoints, int points[]); fillpoly(int numberOfPoints, int points[]); • To draw a closed polygon with N vertices we must pass N+1 co-ordinates to drawpoly() or fillpoly() where N+1th co-ordinate must be same as first co- ordinate. • Thus to draw hexagon we need seven points where first and seventh point is same Ashim Lamichhane 19
  • 20. • Displaying text in graphics mode • outtext(): it displays the string at the current position outtext(string text); • outtextxy(): it displays the string at point(x,y) outtextxy(int x, int y, string text); • settextstyle(): it changes font, size and direction of characters. settextstyle(int font, int direction, int size); Ashim Lamichhane 20
  • 21. • pieslice(): draws a sector with current color and font setting pieslice(int x,int y,int startAngle,int endAngle, int r); • bar(): • draws bar diagram using two points left-top corner and bottom corner bar(int left, int top, int right, int bottom); • getmaxx() • Returns max x value for current graphics driver and mode integer_variable=getmaxx() • getmaxy() • Returns max y value for current graphics driver and mode integer_variable=getmaxy() Ashim Lamichhane 21