C graphics basic

download C graphics basic

of 21

Transcript of C graphics basic

  • 8/8/2019 C graphics basic

    1/21

    C Graphics

    Pixel:Pixel is otherwise called as picture elements. These are nothing but small dots.

    Using these tiny dots or in other words pixels images especially graphics images are

    built on screen.

    If all pictures are built by concept of pixel then wondering how each picture differ

    that is how some picture appear more brighter while some other have a shadyeffect. All this is by the concept or technically terminology called as resolution.

    Resolution:Resolution is the number of rows that appear from top to bottom of a screenand in turn the number of pixels or pixel elements that appear from left toright on each scan line. Based on this resolution only the effect of pictureappears on screen.

    In other words greater the resolution greater will be the clarity of picture.This is because greater the number of dots greater will be sharpness of

    picture. That is resolution value is directly proportional to clarity of picture.

    Modes:There are generally two modes available in C namely text mode andgraphics mode.

    Text mode:Text mode is a kind of computer display mode in which the content of the screen isinternally represented in terms of textual characters rather than individual pixels.

    Typically, the screen consists of a uniform grid ofcharacter cells, each of whichcontains one of the characters of the character set.

    Text mode applications communicate with the user with command-lineinterfaces and text user interfaces. The advantages of text modes comparedto graphics modes include lower memory consumption and faster screenmanipulation.

    Also, text mode applications have relatively low bandwidth requirements inremote terminal use. An obvious disadvantage of text mode is the restrictedscreen content, which makes text mode impractical for many types ofapplications.

    Graphics mode:In a graphics mode we have generally the following adapters namelyCGA (Color Graphics Adapter) [640x200, 4 bit 2 colors]EGA (Enhanced Graphics Adapter) [640x350, 4 bit 16 colors]VGA(Video Graphics Adapter) [ 640x480, 4 bit 16 colors]

    Each adapter differs in the way of generating colors and also in the numberof colors produced by each adapter. Pixel being a picture element when weconsider the graphics mode each pixel has a color associated with it. But the

    Prepared By: Jaydev Gajera

    http://en.wikipedia.org/wiki/Pixelhttp://en.wikipedia.org/wiki/Character_(computing)http://en.wikipedia.org/wiki/Character_sethttp://en.wikipedia.org/wiki/Command-line_interfacehttp://en.wikipedia.org/wiki/Command-line_interfacehttp://en.wikipedia.org/wiki/Text_user_interfacehttp://en.wikipedia.org/wiki/Pixelhttp://en.wikipedia.org/wiki/Character_(computing)http://en.wikipedia.org/wiki/Character_sethttp://en.wikipedia.org/wiki/Command-line_interfacehttp://en.wikipedia.org/wiki/Command-line_interfacehttp://en.wikipedia.org/wiki/Text_user_interface
  • 8/8/2019 C graphics basic

    2/21

    C Graphics

    way these colors are used depends on adapters because each adapter differsin the way they handle colors and also in the number of colors supported.There are numerous graphics functions available in c.

    For example:Function: putpixel(x, y, color)Purpose: The functionality of this function is it put a pixel or in other words a dotat position x, y given in inputted argument. Here one must understand that thewhole screen is imagined as a graph. In other words the pixel at the top left handcorner of the screen represents the value (0, 0).Here the color is the integer value associated with colors and when specifiedthe picture element or the dot is placed with the appropriate color associatedwith that integer value.

    Two Dimensional Coordinate System

    1. Rectangle Coordinate SystemThis system commonly known as Cartesian coordinate system.

    A Cartesian coordinate system specifies each point uniquely in a plane bya pair ofnumericalcoordinates, which are the signed distances from thepoint to two fixed perpendicular directed lines, measured in the same unit oflength.

    Each reference line is called a coordinate axis or just axis of the system,and the point where they meet is its origin.

    The coordinates can also be defined as the positions of the perpendicularprojections of the point onto the two axes, expressed as a signed distancesfrom the origin.

    Prepared By: Jaydev Gajera

    http://en.wikipedia.org/wiki/Point_(geometry)http://en.wikipedia.org/wiki/Plane_(mathematics)http://en.wikipedia.org/wiki/Numberhttp://en.wikipedia.org/wiki/Positive_and_negative_numbershttp://en.wikipedia.org/wiki/Perpendicularhttp://en.wikipedia.org/wiki/Unit_lengthhttp://en.wikipedia.org/wiki/Unit_lengthhttp://en.wikipedia.org/wiki/Orthogonal_projectionhttp://en.wikipedia.org/wiki/Orthogonal_projectionhttp://en.wikipedia.org/wiki/Point_(geometry)http://en.wikipedia.org/wiki/Plane_(mathematics)http://en.wikipedia.org/wiki/Numberhttp://en.wikipedia.org/wiki/Positive_and_negative_numbershttp://en.wikipedia.org/wiki/Perpendicularhttp://en.wikipedia.org/wiki/Unit_lengthhttp://en.wikipedia.org/wiki/Unit_lengthhttp://en.wikipedia.org/wiki/Orthogonal_projectionhttp://en.wikipedia.org/wiki/Orthogonal_projection
  • 8/8/2019 C graphics basic

    3/21

    C Graphics

    Illustration of a Cartesian coordinate plane. Four points are marked and labeled withtheir coordinates: (2,3) in green, (3,1) in red, (1.5,2.5) in blue, and the origin(0,0) in purple.Choosing a Cartesian coordinate system for a plane means choosing an ordered pairof lines (axes) at right angles to each other, a single unit of length for both axes,and an orientation for each axis. The point where the axes meet is taken as theorigin for both axes, thus turning each axis into a number line. Each coordinate of apointp is obtained by drawing a line throughp perpendicular to the associated axis,finding the point q where that line meets the axis, and interpreting q as a number of

    that number line.

    Cartesian coordinates are the foundation ofanalytic geometry, and provideenlightening geometric interpretations for many other branches ofmathematics, such as linear algebra, complex analysis, differentialgeometry, multivariate calculus, group theory, and more.

    A familiar example is the concept of the graph of a function. Cartesiancoordinates are also essential tools for most applied disciplines that deal withgeometry, including astronomy, physics, engineering, and many more. Theyare the most common coordinate system used in computer graphics,

    computer-aided geometric design, and other geometry-related dataprocessing.

    2. Polar Coordinate SystemIn mathematics, the polar coordinate system is a two-dimensionalcoordinate

    system in which each point on a plane is determined by a distance from a fixed

    point and an angle from a fixed direction.

    Prepared By: Jaydev Gajera

    http://en.wikipedia.org/wiki/Ordered_pairhttp://en.wikipedia.org/wiki/Right_anglehttp://en.wikipedia.org/wiki/Analytic_geometryhttp://en.wikipedia.org/wiki/Linear_algebrahttp://en.wikipedia.org/wiki/Complex_numberhttp://en.wikipedia.org/wiki/Differential_geometryhttp://en.wikipedia.org/wiki/Differential_geometryhttp://en.wikipedia.org/wiki/Calculushttp://en.wikipedia.org/wiki/Group_theoryhttp://en.wikipedia.org/wiki/Graph_(mathematics)http://en.wikipedia.org/wiki/Function_(mathematics)http://en.wikipedia.org/wiki/Astronomyhttp://en.wikipedia.org/wiki/Physicshttp://en.wikipedia.org/wiki/Engineeringhttp://en.wikipedia.org/wiki/Computer_graphicshttp://en.wikipedia.org/wiki/Computer-aided_geometric_designhttp://en.wikipedia.org/wiki/Computational_geometryhttp://en.wikipedia.org/wiki/Computational_geometryhttp://en.wikipedia.org/wiki/Mathematicshttp://en.wikipedia.org/wiki/Dimensionhttp://en.wikipedia.org/wiki/Coordinate_systemhttp://en.wikipedia.org/wiki/Coordinate_systemhttp://en.wikipedia.org/wiki/Point_(mathematics)http://en.wikipedia.org/wiki/Plane_(mathematics)http://en.wikipedia.org/wiki/Distancehttp://en.wikipedia.org/wiki/Anglehttp://en.wikipedia.org/wiki/Ordered_pairhttp://en.wikipedia.org/wiki/Right_anglehttp://en.wikipedia.org/wiki/Analytic_geometryhttp://en.wikipedia.org/wiki/Linear_algebrahttp://en.wikipedia.org/wiki/Complex_numberhttp://en.wikipedia.org/wiki/Differential_geometryhttp://en.wikipedia.org/wiki/Differential_geometryhttp://en.wikipedia.org/wiki/Calculushttp://en.wikipedia.org/wiki/Group_theoryhttp://en.wikipedia.org/wiki/Graph_(mathematics)http://en.wikipedia.org/wiki/Function_(mathematics)http://en.wikipedia.org/wiki/Astronomyhttp://en.wikipedia.org/wiki/Physicshttp://en.wikipedia.org/wiki/Engineeringhttp://en.wikipedia.org/wiki/Computer_graphicshttp://en.wikipedia.org/wiki/Computer-aided_geometric_designhttp://en.wikipedia.org/wiki/Computational_geometryhttp://en.wikipedia.org/wiki/Computational_geometryhttp://en.wikipedia.org/wiki/Mathematicshttp://en.wikipedia.org/wiki/Dimensionhttp://en.wikipedia.org/wiki/Coordinate_systemhttp://en.wikipedia.org/wiki/Coordinate_systemhttp://en.wikipedia.org/wiki/Point_(mathematics)http://en.wikipedia.org/wiki/Plane_(mathematics)http://en.wikipedia.org/wiki/Distancehttp://en.wikipedia.org/wiki/Angle
  • 8/8/2019 C graphics basic

    4/21

  • 8/8/2019 C graphics basic

    5/21

    C Graphics

    A diagram illustrating the relationship between polar and Cartesian

    coordinates.

    The two polar coordinates r and can be converted to the Cartesiancoordinates x and y by using the trigonometric functions sine and cosine:

    while the two Cartesian coordinates x and y can be converted to polarcoordinate r by

    (as in the Pythagorean theorem), and

    Header Files

    stdlib.h

    stdlib.h is the header of the general purpose standard library ofC programming language whichincludes functions involving memory allocation, process control, conversions and others. It is

    compatible with C++ and is known as cstdlib in C++. The name "stdlib" stands for "standard

    library".

    graphics.h

    Prepared By: Jaydev Gajera

    http://en.wikipedia.org/wiki/Cartesian_coordinate_systemhttp://en.wikipedia.org/wiki/Cartesian_coordinate_systemhttp://en.wikipedia.org/wiki/Trigonometric_functionhttp://en.wikipedia.org/wiki/Pythagorean_theoremhttp://en.wikipedia.org/wiki/C_programming_languagehttp://en.wikipedia.org/wiki/C%2B%2Bhttp://en.wikipedia.org/wiki/File:Polar_to_cartesian.svghttp://en.wikipedia.org/wiki/Cartesian_coordinate_systemhttp://en.wikipedia.org/wiki/Cartesian_coordinate_systemhttp://en.wikipedia.org/wiki/Trigonometric_functionhttp://en.wikipedia.org/wiki/Pythagorean_theoremhttp://en.wikipedia.org/wiki/C_programming_languagehttp://en.wikipedia.org/wiki/C%2B%2B
  • 8/8/2019 C graphics basic

    6/21

    C Graphics

    graphics.h is the header of the graphics library ofC programming language.

    FunctionsBelow are the important functions and their description mostly used in

    graphics applications in C Language.

    InitgraphThis function is used to load the graphics drivers and initialize the graphicssystem. For every function, that uses graphics mode. Graphics mode mustbe initialized before using that function.

    Deceleration : void far initgraph(int far *driver, int far *mode, char far *path)

    Where: Path determines that path to the specified graphics driver.(generally C:\\tc\\bgi)

    Remarks:To start the graphics system, you must first call initgraph.

    initgraph initializes the graphics system by loading a graphics driver from disk then

    putting the system into graphics mode.

    initgraph also resets all graphics settings (color, palette, current position, viewport,

    etc.) to their defaults, then resets graphresult to 0.

    *graphdriver and *graphmode must be set to valid graphics_drivers and

    graphics_mode values or you'll get unpredictable results.

    detectgraph

    Detectgraph function determines the graphics hardware in the system, if thefunction finds a graphics adapter then it returns the highest graphics modethat the adapter supports.

    Deceleration : void far detectgraph(int far *driver, int far *mode)

    Cleardevice This function clears the graphics screen contents and returnthe control to the location (0,0).

    Deceleration : void far cleardevice(void)closegraphThis function shutdown the graphics mode and returns to the position it wasbefore the initgraph function was called. Closegraph function releases all theresources occupied by the graphics system like memry, fonts, drivers etc

    Declaration : void far closegraph(void);

    Remarks:

    closegraph deallocates all memory allocated by the graphics system.

    Prepared By: Jaydev Gajera

    http://en.wikipedia.org/wiki/C_programming_languagehttp://en.wikipedia.org/wiki/C_programming_language
  • 8/8/2019 C graphics basic

    7/21

    C Graphics

    It then restores the screen to the mode it was in before you called initgraph.(The graphics system deallocates memory, such as the drivers, fonts, and aninternal buffer, through a call to _graphfreemem.)

    Setgraphmodesetgraphmode sets the system to graphics mode, clears the screen

    Declaration: void far setgraphmode(int mode);

    Remarks:

    setgraphmode selects a graphics mode different than the default one set byinitgraph. It clears the screen and resets all graphics settings to theirdefaults.

    mode must be a valid mode for the current device driver.Return Value:setgraphmode does not return.

    graphresult :

    Returns an error code for the last unsuccessful graphics operation.

    Declaration: int far graphresult(void);

    Remarks:

    graphresult returns the error code for the last graphics operation that reported anerror, then resets the error level to grOk.

    The variable maintained by graphresult is reset to 0 after graphresult hasbeen called. Therefore, you should store the value of graphresult into atemporary variable and then test it.

    Return Value: Returns the current graphics error number, (an integer in therange -15 to 0).

    graphdefaultsResets all graphics settings to their defaults

    Declaration: void far graphdefaults(void);

    Remarks:

    graphdefaults resets all graphics settings to their defaults:

    Prepared By: Jaydev Gajera

  • 8/8/2019 C graphics basic

    8/21

    C Graphics

    sets the viewport to the entire screen. moves the current position to (0,0).sets the default palette colors, background color, and drawing color.sets thedefault fill style and pattern. sets the default text font and justification.

    Return Value: None

    grapherrormsg:

    Returns a pointer to an error message string

    Declaration: char *far grapherrormsg(int errorcode);

    Remarks:

    grapherrormsg returns a pointer to the error message string associated with

    errorcode, the value returned by graphresult.

    Return Value : Returns a pointer to an error message string.

    restorecrtmode

    Restores screen mode to pre-initgraph setting

    Declaration: void far restorecrtmode(void);

    Remarks:

    restorecrtmode restores the original video mode detected by initgraph.

    This function can be used in conjunction with setgraphmode to switch backand forth between text and graphics modes.

    getmaxx, getmaxy

    Returns maximum x or y screen coordinate

    Declaration:

    int far getmaxx(void);

    int far getmaxy(void);

    Remarks:

    getmaxx returns the maximum x value (screen-relative) for the current

    graphics driver and mode.getmaxy returns the maximum y value (screen-relative) for the current

    graphics driver and mode.

    For ex: On a CGA in 320 x 200 mode, getmaxx returns 319 and getmaxy

    returns 199.

    Return Value:

    getmaxx: maximum x screen coordinate

    getmaxy: maximum y screen coordinate

    Prepared By: Jaydev Gajera

  • 8/8/2019 C graphics basic

    9/21

    C Graphics

    getpixel, putpixel

    getpixel gets the color of a specified pixel

    putpixel plots a pixel at a specified point

    Declaration:unsigned far getpixel(int x, int y);

    void far putpixel(int x, int y, int color);

    Remarks:

    getpixel gets the color of the pixel located at (x,y).

    putpixel plots a point in the color defined by color at (x,y).

    Return Value:

    getpixel returns the color of the given pixel.

    putpixel does not return.

    getx, gety

    getx returns the current position's x coordinate

    gety returns the current position's y coordinate

    Declaration:

    int far getx(void);

    int far gety(void);

    Remarks:

    getx returns the x-coordinate of the current graphics position.

    gety returns the y-coordinate of the current graphics position.

    Return Value:

    getx: x-coordinate of current position , gety: y-coordinate of current position

    outtext, outtextxy

    outtext displays a string in the viewport (graphics mode)

    outtextxy displays a string at the specified location (graphics mode)

    Declaration:

    void far outtext(char far *textstring);

    void far outtextxy(int x, int y, char far *textstring);

    Remarks:

    outtext and outtextxy display a text string, using the current justification

    settings and the current font, direction, and size.

    Prepared By: Jaydev Gajera

  • 8/8/2019 C graphics basic

    10/21

    C Graphics

    outtext outputs textstring at the current position (CP)

    outtextxy displays textstring in the viewport at the position (x, y)

    To maintain code compatibility when using several fonts, use textwidth and

    textheight to determine the dimensions of the string.

    If a string is printed with the default font using outtext or outtextxy, any

    part of the string that extends outside the current viewport is truncated.

    With outtext, if the horizontal text justification is LEFT_TEXT and the text

    direction is HORIZ_DIR, the CP's x-coordinate is advanced by

    textwidth(textstring).

    Otherwise, the CP remains unchanged.

    outtext and outtextxy are for use in graphics mode; they will not work in

    text mode.

    Return Value: None

    settextjustify

    Sets text justification for graphics mode

    Declaration: void far settextjustify(int horiz, int vert);

    Remarks:

    Text output after a call to settextjustify is justified around the current

    position (CP) horizontally and vertically, as specified.

    The default justification settings are

    LEFT_TEXT (for horizontal) and

    TOP_TEXT (for vertical)

    The enumeration text_just in GRAPHICS.H provides names for the horiz and

    vert settings passed to settextjustify.

    settextjustify affects text written with outtext and can't be used with

    text-mode and stream functions.

    Return Value:

    If invalid input is passed to settextjustify, graphresult returns -11, and

    the current text justification remains unchanged.

    settextstyle

    Sets the current text characteristics

    Prepared By: Jaydev Gajera

  • 8/8/2019 C graphics basic

    11/21

    C Graphics

    Declaration:

    void far settextstyle(int font, int direction, int charsize);

    Remarks:

    settextstyle sets the text font, the direction in which text is displayed,

    and the size of the characters.A call to settextstyle affects all text output by outtext and outtextxy.

    font

    One 8x8 bit-mapped font and several "stroked" fonts are available. The 8x8

    bit-mapped font, the default, is built into the graphics system.

    The enumeration font_names, defined in GRAPHICS.H, provides names for

    the

    different font settings.

    direction

    Font directions supported are horizontal text (left to right) and verticaltext (rotated 90 degrees counterclockwise).

    textheight, textwidth

    textheight returns the height of a string in pixels

    textwidth returns the width of a string in pixels

    Declaration:

    int far textheight(char far *textstring);

    int far textwidth(char far *textstring);

    Remarks:

    textheight takes the current font size and multiplication factor, and

    determines the height of textstring in pixels.

    textwidth takes the string length, current font size, and multiplication factor,

    and determines the width of textstring in pixels.

    These functions are useful for adjusting the spacing between lines,

    computing viewport heights, sizing a title to make it fit on a graph or in a

    box, etc..

    For example, with the 8x8 bit-mapped font and a multiplication factor of 2

    (set by settextstyle), the string "Turbo C++" is 16 pixels high.

    Instead of doing the computations manually, use textheight to compute the

    height of strings, and use textwidth to compute their width.

    When you use these functions, no source code modifications are required

    when

    you select different fonts.

    Prepared By: Jaydev Gajera

  • 8/8/2019 C graphics basic

    12/21

    C Graphics

    Return Value:

    textheight returns the text height in pixels. textwidth returns the text width

    in pixels.

    getcolor, setcolorgetcolor returns the current drawing color

    setcolor sets the current drawing color

    Declaration:

    int far getcolor(void);

    void far setcolor(int color);

    Remarks:

    getcolor returns the current drawing color.

    setcolor sets the current drawing color to color, which can range from 0 to

    getmaxcolor.

    To select a drawing color with setcolor, you can pass either the color

    number or the equivalent color name.

    The drawing color is the value that pixels are set to when the program draws

    lines, etc.

    For example, in CGAC0 mode (palette number 0), the palette contains four

    colors (background, light green, light red, and yellow):

    If getcolor returns 1, the current drawing color is light green.

    Either setcolor(3) or setcolor(CGA_YELLOW) selects yellow as the drawing

    color.

    In CGAC3 mode, if getcolor returns 1, the current drawing color is cyan.

    Return Value:getcolor returns the current drawing color.

    setcolor does not return

    getmaxcolor

    Returns maximum color value

    Declaration: int far getmaxcolor(void);

    Prepared By: Jaydev Gajera

  • 8/8/2019 C graphics basic

    13/21

    C Graphics

    Remarks:

    getmaxcolor returns the highest valid color value that can be passed to

    setcolor for the current graphics driver and mode.

    For example, on a 256K EGA, getmaxcolor always returns 15. This means

    that any call to setcolor with a value from 0 to 15 is valid.

    On a CGA in high-resolution mode getmaxcolor returns 1.

    Return Value: Returns the highest available color value.

    getbkcolor, setbkcolor

    getbkcolor returns the current background color

    setbkcolor sets the current background color using the palette

    Declaration:

    int far getbkcolor(void);

    void far setbkcolor(int color);

    Remarks:

    getbkcolor returns the current background color.

    setbkcolor sets the background to the color specified by color.

    color is either a number or symbolic name specifying the color to set.

    For example, if you want to set the background color to blue, you can callsetbkcolor(BLUE) /* or */ setbkcolor(1)

    On CGA and EGA systems, setbkcolor changes the background color by

    changing

    the first entry in the palette.

    On an EGA or a VGA, if you call setpalette or setallpalette to change the

    palette colors, the defined symbolic constants might not give the correct

    color.

    This is because the color parameter to setbkcolor indicates the entry number

    in the current palette, rather than a specific color. (Except 0, whichalways sets the background color to black).

    Return Value:

    getbkcolor returns the current background color.

    setbkcolor does not return.

    setfillpattern

    setfillpattern selects a user-defined fill pattern

    Prepared By: Jaydev Gajera

  • 8/8/2019 C graphics basic

    14/21

    C Graphics

    Declaration: void far setfillpattern(char far *upattern, int color);

    Remarks:

    setfillpattern sets the current fill pattern to a user-defined 8x8 pattern.

    Whenever a bit in a pattern's byte is set to 1, the corresponding pixel isplotted.

    Return Value: None

    Setfillstyle :

    Sets the fill pattern and color

    Declaration: void far setfillstyle(int pattern, int color);

    Remarks:

    setfillstyle sets the current fill pattern and fill color.

    To set a user-defined fill pattern, do not give a pattern of 12 (USER_FILL) to

    setfillstyle; instead, call setfillpattern.

    The enumeration fill_patterns, defined in GRAPHICS.H, gives names for the

    predefined fill patterns, plus an indicator for a user-defined pattern.

    Return Value: None

    If invalid input is passed to setfillstyle, graphresult returns -11 (grError), and

    the current fill pattern and fill color remain unchanged.

    floodfill

    Flood-fills a bounded region

    Declaration: void far floodfill(int x, int y, int border);

    Remarks:

    floodfill fills an enclosed area on bitmap devices.

    The area bounded by the color border is flooded with the current fill pattern

    and fill color.

    (x,y) is a "seed point".

    If the seed is within an enclosed area, the inside will be filled.

    If the seed is outside the enclosed area, the exterior will be filled.

    Prepared By: Jaydev Gajera

  • 8/8/2019 C graphics basic

    15/21

    C Graphics

    Return Value:

    If an error occurs while flooding a region, graphresult returns -7.

    setlinestyle

    Sets the current line style and width or pattern

    Declaration: void far setlinestyle(int linestyle, unsigned upattern, int

    thickness);

    Remarks:

    setlinestyle sets the style for all lines drawn by line, lineto, rectangle,

    drawpoly, etc.

    Return Value:

    If invalid input is passed to setlinestyle, graphresult returns -11, and thecurrent line style remains unchanged.

    imagesize

    Returns the number of bytes required to store a bit image

    Declaration:

    unsigned far imagesize(int left, int top, int right, int bottom);

    Remarks:

    imagesize determines the size of the memory area required to store a bitimage.

    Return Value:

    On success, returns the size of the required memory area in bytes.

    On error (if the size required for the selected image is >= (64K - 1) bytes),

    returns 0xFFFF (-1)

    getimage, putimage

    getimage saves a bit image of the specified region into memoryputimage outputs a bit image onto the screen

    Declaration:

    void far getimage(int left, int top, int right, int bottom,void far *bitmap);

    void far putimage(int left, int top, void far *bitmap, int op);

    Remarks:

    getimage copies an image from the screen to memory.

    Prepared By: Jaydev Gajera

  • 8/8/2019 C graphics basic

    16/21

    C Graphics

    putimage puts the bit image previously saved with getimage back onto the

    screen, with the upper left corner of the image placed at (left,top)

    Argument What It Is/Doesbitmap -Points to the area in memory where the bit image is

    stored.

    -The first two words of this area are used for the widthand

    height of the rectangle.

    -The remainder holds the image itself.

    Bottom left right

    top

    -(left, top) and (right, bottom) define the rectangular

    screen area from which getimage copies the bit image.

    -(left, top) is where putimage places the upper left

    corner of the stored image.

    op Specifies a combination operator that controls how the

    color for each destination pixel onscreen is computed,

    based on the pixel already onscreen and the

    corresponding source pixel in memory.

    Return Value: None

    setviewport

    Sets the current viewport for graphics outputDeclaration:

    void far setviewport(int left, int top, int right, int bottom, int clip);

    Remarks:

    setviewport establishes a new viewport for graphics output.

    The viewport's corners are given in absolute screen coordinates by (left,top)

    and (right,bottom).

    The current position (CP) is moved to (0,0) in the new window.

    The clip argument determines whether drawings are clipped (truncated) atthe current viewport boundaries. If clip is non-zero, all drawings will be

    clipped to the current viewport.

    Return Value:

    setviewport does not return.

    If invalid input is passed to setviewport, graphresult returns -11, and the

    current view settings remain unchanged.

    Prepared By: Jaydev Gajera

  • 8/8/2019 C graphics basic

    17/21

    C Graphics

    clearviewport

    Clears the current viewport

    Declaration: void far clearviewport(void);

    Remarks:

    clearviewport erases the viewport and moves the CP (current position) tohome (0,0), relative to the viewport.

    Return Value: None

    line, linerel, lineto

    line draws a line between two specified points

    linerel draws a line a relative distance from the current position (CP)

    lineto draws a line from the current position (CP) to (x,y)

    Declaration:void far line(int x1, int y1, int x2, int y2);

    void far linerel(int dx, int dy);

    void far lineto(int x, int y);

    Remarks:

    line draws a line from (x1, y1) to (x2, y2) using the current color, line style,

    and thickness. It does not update the current position (CP).

    linerel draws a line from the CP to a point that is a relative distance (dx, dy)

    from the CP, then advances the CP by (dx, dy).

    lineto draws a line from the CP to (x, y), then moves the CP to (x, y).

    Return Value: None

    moverel, moveto

    moverel moves the current position (CP) a relative distance moveto moves

    the CP to (x, y)

    Declaration:

    void far moverel(int dx, int dy);

    void far moveto(int x, int y);

    Remarks:

    moverel moves the current position (CP) dx pixels in the x direction and dy

    pixels in the y direction.

    moveto moves the current position (CP) to viewport position (x, y).

    Prepared By: Jaydev Gajera

  • 8/8/2019 C graphics basic

    18/21

    C Graphics

    Return Value: None

    bar: Draws a bar

    Declaration: void far bar(int left, int top, int right, int bottom);

    Remarks:

    bar draws a filled-in, rectangular, two-dimensional bar.

    The bar is filled using the current fill pattern and fill color. bar does not

    outline the bar.

    To draw an outlined two-dimensional bar, use bar3d with depth = 0.

    Parameters What they are(left, top) the rectangle's upper left corner(right, bottom) the rectangle's lower right corner

    The coordinates are in pixels.

    Return Value: None

    bar3d: Draws a 3-D bar

    Declaration: void far bar3d(int left, int top, int right, int bottom, int depth, int

    topflag);

    Remarks:

    bar3d draws a three-dimensional rectangular bar, then fills it using the current fillpattern and fill color.

    The three-dimensional outline of the bar is drawn in the current line style and color.

    Parameters What they aredepth Bar's depth in pixels

    (left, top) the rectangle's upper left corner(right, bottom) the rectangle's lower right cornertopflag Governs whether a three-dimensional

    top is put on the bar

    If topflag is non-zero, a top is put on the bar. If topflag is 0, no top is put on the bar:This makes it possible to stack several bars on top of one another.

    To calculate a typical depth for bar3d, take 25% of the width of the bar,like this:

    bar3d(left, top, right, bottom, (right-left)/4, 1);

    Return Value: None

    Rectangle : Draws a rectangle (graphics mode)

    Prepared By: Jaydev Gajera

  • 8/8/2019 C graphics basic

    19/21

    C Graphics

    Declaration: void far rectangle(int left, int top, int right, int bottom);

    Remarks:

    rectangle draws a rectangle in the current line style, thickness, and drawing color.

    (left,top) is the upper left corner of the rectangle, and (right,bottom) is its lowerright corner.

    Return Value: None

    arc, circle, pieslice

    arc draws a circular arc

    circle draws a circle

    pieslice draws and fills a circular pie slice

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

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

    void far pieslice(int x, int y, int stangle, int endangle, int radius);

    Remarks:

    arc draws a circular arc in the current drawing color.

    circle draws a circle in the current drawing color.

    pieslice draws a pie slice in the current drawing color, then fills it using

    the current fill pattern and fill color.

    Argument What It Is/Does(x,y) Center point of arc, circle, or pie slice

    stangle Start angle in degrees

    endangle End angle in degreesradius Radius of arc, circle, and pieslice

    The arc or slice travels from stangle to endangle.

    If stangle = 0 and endangle = 360, the call to arc draws a complete circle.

    Angle for arc, circle, and pieslice (counter-clockwise)

    90 degrees

    |

    |

    180 --------------------0 degrees,

    degrees | 360 degrees

    |

    270 degrees

    Prepared By: Jaydev Gajera

  • 8/8/2019 C graphics basic

    20/21

    C Graphics

    ReturnValue: None

    Ellipse, fillellipse, sector

    ellipse draws an elliptical arcfillellipse draws and fills an ellipse

    sector draws and fills an elliptical pie slice

    Declaration:

    void far ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius);

    void far fillellipse(int x, int y, int xradius, int yradius);

    void far sector(int x, int y, int stangle, int endangle, int xradius, int yradius);

    Remarks:

    ellipse draws an elliptical arc in the current drawing color.

    fillellipse draws an ellipse, then fills the ellipse with the current fill color and fill

    pattern.

    sector draws and fills an elliptical pie slice in the current drawing color, then fills it

    using the pattern and color defined by setfillstyle or setfillpattern.

    Argument What It Is(x,y) Center of ellipseXradius Horizontal axis

    Yradius Vertical axisStangle Starting angleEndangle Ending angle

    The ellipse or sector travels from stangle to endangle.

    If stangle = 0 and endangle = 360, the call to ellipse draws a complete ellipse.

    ReturnValue: None

    drawpoly, fillpoly

    drawpoly draws the outline of a polygon

    fillpoly draws and fills a polygon

    Declaration:

    void far drawpoly(int numpoints, int far *polypoints);

    void far fillpoly(int numpoints, int far *polypoints);

    Remarks:

    Prepared By: Jaydev Gajera

  • 8/8/2019 C graphics basic

    21/21

    C Graphics

    drawpoly draws a polygon using the current line style and color.

    fillpoly draws the outline of a polygon using the current line style and color, then

    fills the polygon using the current fill pattern and fill color.

    Argument What It Isnumpoints Specifies number of pointspolypoints Points to a sequence of (numpoints x 2)

    integers

    Each pair of integers gives the x and y coordinates of a point on the polygon.

    To draw a closed figure with N vertices, you must pass N+1 coordinates to

    drawpoly, where the Nth coordinate == the 0th coordinate.

    ReturnValue: None