C Graphics

download C Graphics

of 5

Transcript of C Graphics

  • 8/6/2019 C Graphics

    1/5

    Borland C Graphics

    If 70% of Cprogramming is inheritance,structure,classes and other theoretical subjects

    the rest 40% is related with C graphics programming.It is the fun side of programmingactually.I was Ignorant about graphics programming in c ,c++ since the day I learned

    #include the portion is not usually covered in usual C programming tutorials, there are special books for C graphics programming alone.Nobody will speak aboutgraphics if you go and search for 'learn C' in google.I still believe its necessary to teach

    about graphics programming with the basic lesson itself.There was nobody there to teach

    me about about this topic , finally I dug up some c graphics programming books from my

    college library and mastered the concepts in few days.Its simple than the rest of theOOPS concepts actually.So let's move into work.

    Your Screen For C graphics

    Borland C graphics.h tutorial

    The most basic thing you have to learn before stepping in the C shore of graphics is about

    the monitor.

    The monitor you are staring at right now contains lots of small dots called pixels , wheneach pixels glow in different colours you see an image.You can imagine it as minute

    bricks which can change to any colour used in building up the screen you see.

    When we initialize a programming for using C graphics.h it converts the whole screen

    into a matrix, if you dont know what a matrix in mathematics means ,just understandthat your screens top left corner is taken as origin the x-axis increases by width from left

    to right and the y-axis increases by length from top to bottom.The co-ordinates we enter

    in each function is mapped according to this order.Now I dont want to confuse you withfurther complicated theory.If you need to get indepth technichal knowledge about this

    topic you can go for C grapics book from amazon i've linked somewhere in the bottom of

    this page.We will hover straight into the application now.For developing graphics usingC we need to do it in three simple steps.

    http://hubpages.com/_intrahub/hub/How-To-Become-A-Programmerhttp://hubpages.com/_intrahub/hub/How-To-Become-A-Programmer
  • 8/6/2019 C Graphics

    2/5

    1. Loading the header file of C graphics.h2. Initializing 'graphics driver' and 'grahpics mode'

    3. Using the functions where ever necessary

    Loading The Header File

    #include#includevoidmain(){}

    Loading the header file of C graphics.h

    The C graphics.h is the header file which should be included to initalize your computer tostart using grahipcs methods and also to initialize the monitor.These kinds of statements

    before the main program are called preprocessor dierctives.Its very simple as codedbelow.

    Initializing Graphics Form Borland C graphics library

    This initialization is done inside the main program.The method initgraph() is used for

    this purpose there are three parameters for this , the first two are of integer type and the

    next is the path which you can leave blank in quoutes to take the default path.The integerparameters are initalized in this method for all the C graphics programs so you just need

    to memorize it and apply it for all C graphics programming from now on.

    Initializing 'Graphics Driver' and 'Graphics Mode'

    from Borland C graphics library

    #include#include#includevoidmain(){intgdriver=DETECT,gmode;initgraph(&gdriver,&gmode," ");}

    Methods In Borland C Graphics programming

    cleardevice()

    gotoxy(x,y)

    putpixel(x,y,WHITE)

  • 8/6/2019 C Graphics

    3/5

    outtextxy(x,y,"HELLO")

    rectangle(x,y,intx_width,inty_height)

    circle(x,y,radius)

    line(x1,y1,x2,y2)

    moveto(dx,dy)

    lineto(x,y)

    ellipse(x-centre,y-center,starting_angle,ending_angle,x_radius,y_radius)

    drawpoly(num_of_points + 1, points)

    settextstyle(DEFAULT_FONT,HORIZ_DIR,1)

    setfillstyle(SOLID_FILL,RED)

    setcolor(CYAN)

    floodfill(x,y,getmaxcolor())

    setviewport(intleft,inttop,intright,intbottom, intclip)setviewport(10,10,630,470,1)

    itoa(x1,st1,10)

    delay(100)

    closegraph()

    Some Common C Graphics Programming Methods

    Well, these are some of the most popular C Graphics Methods used in while graphicsprogramming soon after you learn these you can think of developing small games ,

    puzzles and so on.I'll just give only a small overview on each methods.x,y,x1,y1,x2,y2etc are all variables you'll have to substitute with numeric values to get workingprograms.Further clear-cut reference can be obtained from any of the C graphics book

    available from amazon or your local library(those books would be pretty old and dusty by

    now). If you find it difficult to view the long C graphics library functions code in thewindow because the code is too lengthy, then click on the 'view plain' written in small

    fonts at the top left of the codes box on the left side of this text.

  • 8/6/2019 C Graphics

    4/5

    Cleardevice()

    Clears all previous graphical outputs generated by the previous programs.Its a goodpractice to include this method at the starting of each program.

    gotoxy()

    This will initialize the graphics cusor to the specified co-ordiante.In C gotoxy function is

    used very frequently to locate the cursor at different locations whenever as necessary.

    putpixex()

    It will colour the pixel specified by the co-ordinates.

    outtextxy()

    This method is used to display a text in any position on the screen.The numericcoordinates are substituted for x and y.

    rectangle()

    Draws a rectangle according to the given parameter x and y are the top-left corner co-ordinates.

    circle()

    Draws a circle with x,y as the center .

    line()

    Draws a line as per the given co-ordinates.

    moveto()

    Cursor is moved from the current location to the specified location dx,dy.Theseparameters can also be used as incremental values.

    lineto()

    Draws a line from its current location to the co-ordinate(x,y)

    ellipse()

    Draws the ellipse with the specified angles and coordinates.

    drawpoly()

  • 8/6/2019 C Graphics

    5/5

    Draws a polygon with (num_of_points +1) edges.The array 'points'

    int points[ ]=(x1,y1,x2,y2,x3,y3...)

    settextstyle()

    The fonts available are :TRIPLEX_FONT, SMALL_FONT, SANS_SERIE_FONT,

    GOTHIC_FONT

    The direction can be changed asHORIZ_DIR orVERT_DIR

    The charecter size increases from 1 to 10

    setfillstyle()

    The fill styles avaliable are SOLID_FILL, LINE_FILL, HATCH_FILL, SLASH_FILL etc.

    setcolor()

    Sets the color

    floodfill()

    The function fills the enclosed area about x,y with the highest value of the color returned

    by thegetmaxcolor().

    setviewport()

    This marks a rectangle starting from (10,10) having widht 630 pixels height 470

    pixels.The integer specifies this identified area may be clipped. clearviewport() reverse

    this function.

    itoa()

    Converts the integer x1into an alphabet, and puts it in st1, which is an array to hold 10characters.10 is the buffer size.

    delay()

    Cause a pause in execution of the program 1000ms= 1 second

    closegraph()

    Terminates all graphics operations and revert the hardware back to the normal mode.