22
Unit 11 Graphics

Unit 11. Graphics

Embed Size (px)

Citation preview

Page 1: Unit 11. Graphics

Unit 11

Graphics

Page 2: Unit 11. Graphics

Ashim Lamichhane 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)

Page 3: Unit 11. Graphics

Ashim Lamichhane 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

Page 4: Unit 11. Graphics

Ashim Lamichhane 4

Graphics Characteristics1. 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.

Page 5: Unit 11. Graphics

Ashim Lamichhane 5

Fig. representation of co-ordinates on computer screen

(0,0)(200,0)

y

(0,479)

(639,0)x

(320,240)

(639,450)

(639,479)

Suppose to plot a pixel on theScreen at say x=70 and y=100,We’d put pixel information into that two dimensional array,at element array [100][70]

Page 6: Unit 11. Graphics

Ashim Lamichhane 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.

Page 7: Unit 11. Graphics

Ashim Lamichhane 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.

Page 8: Unit 11. Graphics

Ashim Lamichhane 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

Page 9: Unit 11. Graphics

Ashim Lamichhane 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”);

Page 10: Unit 11. Graphics

Ashim Lamichhane 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)

Page 11: Unit 11. Graphics

Ashim Lamichhane 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:\\TC\\BGI”

Page 12: Unit 11. Graphics

Ashim Lamichhane 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.

Page 13: Unit 11. Graphics

Ashim Lamichhane 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:\\TC\\BGI”);

• 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:\\TC\\BGI”);

Page 14: Unit 11. Graphics

Ashim Lamichhane 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.

Page 15: Unit 11. Graphics

Ashim Lamichhane 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.

Page 16: Unit 11. Graphics

Ashim Lamichhane 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 pixelinteger_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);

Page 17: Unit 11. Graphics

Ashim Lamichhane 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 positionlinerel(int dx, int dy);

Page 18: Unit 11. Graphics

Ashim Lamichhane 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 rectanglereactangle(int x1, int y1, int x2, int y2);

Page 19: Unit 11. Graphics

Ashim Lamichhane 19

• drawpoly(): draws the outline of a polygon using required points• fillpoly(): draws and fills polygon

• Draws the outline of a polygon using required pointsdrawpoly(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

Page 20: Unit 11. Graphics

Ashim Lamichhane 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);

Page 21: Unit 11. Graphics

Ashim Lamichhane 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

cornerbar(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()

Page 22: Unit 11. Graphics

Ashim Lamichhane 22

END