CGLab File for college

Embed Size (px)

Citation preview

  • 8/18/2019 CGLab File for college

    1/38

    Computer Graphics Lab.File

  • 8/18/2019 CGLab File for college

    2/38

    SS.No

    Name of Experiment   Date Signature of 

    Faculty

  • 8/18/2019 CGLab File for college

    3/38

    List of Experiments

    1)  Study of basic graphics functions defined in “graphics.h”.

    2) WAP to draw a line through Bresenha!sAlgorith.

    ") Write a progra to draw a line using ##A algorith.

    $) Write a progra to draw a circle using Bresenha!salgorith.

    %) Write a progra to rotate a &ircle around anarbitrary point

    ') Write a progra to ipleent reflection of a point(line.

    ) Write a progra to scale an ob*ect about theorigin.

  • 8/18/2019 CGLab File for college

    4/38

  • 8/18/2019 CGLab File for college

    5/38

    EXPERIMENT NO 1

    BASIC GRAPICS F!NC"I#NS !SE$

    %& INI"GRAP•  Initializes the graphics system.

    $eclaration

    •  Void far initgraph(int far *graphdriver)

    Remar's

    •  To start the graphic system, you must first call initgraph.

    •  Initgraph initializes the graphic system by loading a graphics driver from dis (or

    validating a registered driver) then putting the system into graphics mode.

    •  Initgraph also resets all graphics settings (color, palette, current position, vie!port, etc) to

    their defaults then resets graph.

    (& GE"PI)EL* P!"PI)EL

    •  "etpi#el gets the color of a specified pi#el.

    •  $utpi#el places a pi#el at a specified point.

    $eclaration

    •  %nsigned far getpi#el(int #, int y)

    •  Void far putpi#el(int #, int y, int color)

    Remar's

    •  "etpi#el gets the color of the pi#el located at (#,y)&

    •  $utpi#el plots a point in the color defined at (#, y).

  • 8/18/2019 CGLab File for college

    6/38

    Return +alue

    •  "etpi#el returns the color of the given pi#el.

    •  $utpi#el does not return.

    ,& CL#SE GRAP

    •  'huts do!n the graphic system.

    $eclaration

    •  Void far closegraph(void)&

    Remar's

    •  lose graph deallocates all memory allocated by the graphic system.

    •  It then restores the screen to the mode it !as in before you called initgraph.

    Return +alue

    •   )one.

    -& ARC* CIRCLE* PIESLICE

    •  arc dra!s a circular arc.

    •  ircle dra!s a circle

    •  $ieslice dra!s and fills a circular pieslice

    $eclaration

    •  Void far arc(int #, int y, int stangle, int endangle, int radius)&

    • 

    Void far circle(int #, int y, int radius)&

    •  Void far pieslice(int #, int y, int stangle, int endangle, int radius)&

    Remar's

    •  rc dra!s a circular arc in the current dra!ing color

  • 8/18/2019 CGLab File for college

    7/38

    •  ircle dra!s a circle in the current dra!ing color

    •  $ieslice dra!s a pieslice in the current dra!ing color, then fills it using the current fill

     pattern and fill color.

    & ELLIPSE* FILLELIPSE* SEC"#R

    •  +llipse dra!s an elliptical arc.

    •  illellipse dra!s and fills ellipse.

    •  'ector dra!s and fills an elliptical pie slice.

    $eclaration

    •  Void far ellipse(int #, int y, int stangle, int endangle, int #radius, int yradius)

    •  Void far fillellipse(int #, int y, int #radius, int yradius)

    •  Void farsectoe(int #, int y, int stangle, int endangle, int #radius, int yradius)

    Remar's

    •  +llipse dra!s an elliptical arc in the current dra!ing color.

    •  illellipse dra!s an elliptical arc in the current dra!ing color and than fills it !ith fill color

    and fill pattern.

    • 

    'ector dra!s an elliptical pie slice in the current dra!ing color and than fills it using the

     pattern and color defined by setfillstyle or setfillpattern.

    /& FL##$FILL

    •  lood-fills a bounded region.

    $eclaration

    • 

    Void far floodfill(int #, int y, int border)

    Remar's

    •  loodfills an enclosed area on bitmap device.

    •  The area bounded by the color border is flooded !ith the current fill pattern and fill color.

  • 8/18/2019 CGLab File for college

    8/38

    • (#,y) is a seed point/

      If the seed is !ithin an enclosed area, the inside !ill be filled.

      If the seed is outside the enclosed area, the e#terior !ill be filled.

    •  %se fillpoly instead of floodfill !herever possible so you can maintain code compatibility

    !ith future versions.

    •  loodfill doesnot !or !ith the I01-2345 driver.

    Return +alue

    •  If an error occurs !hile flooding a region, graph result returns 647.

    0& GE"C#L#R* SE"C#L#R

    •  "etcolor returns the current dra!ing color.

    •  'etcolor returns the current dra!ing color.

    $eclaration

    •  Int far getcolor(void)&

    •  Void far setcolor(int color)

    Remar's

    •  "etcolor returns the current dra!ing color.

    •  'etcolor sets the current dra!ing color to color, !hich can range from 8 to getma#color.

    •  To set a dra!ing color !ith setcolor , you can pass either the color number or the

    e9uivalent color name.

    1& LINE*LINEREL*LINE"#

    •  :ine dra!s a line bet!een t!o specified pints.

    •  ;nerel dra!s a line relative distance from current position($).

    •  :inrto dra!s a line from the current position ($) to(#,y).

    $eclaration

  • 8/18/2019 CGLab File for college

    9/38

    •  Void far lineto(int #, int y)

    Remar's

    •  :ine dra!s a line from (#4, y4) to (#

  • 8/18/2019 CGLab File for college

    10/38

      EXPERIMENT 2

    BRESENHAM’S ALGOR!HM FOR LNE DRA"NG#

    4.  'tart.

    y4.

    @.  alculate d#>#y(

  • 8/18/2019 CGLab File for college

    11/38

    "A$ !O DRA" A LNE %SNG BRESENHAM’S ALGOR!

    GincludeEstdio.hH

    GincludeEconio.hHGincludeEgraphics.hH

    void main()

    int #,y,#4,y4,#

  • 8/18/2019 CGLab File for college

    12/38

     p>#F4&

    y>yF4&

     p>pF

  • 8/18/2019 CGLab File for college

    13/38

    #!"P!"

  • 8/18/2019 CGLab File for college

    14/38

      EXPERIMENT NO 3.

    ALGOR!HM !O DRA" A LNE %SNG DDA ALGOR!HM

    4.  'tart.

    y4.

    @.  alculate d#>#yabs(d#).

    2. 

    ;ther!ise s> abs(dy).

    D.  Then #i>d#Os and yi>dyOs.

    48. 'tart from >8 and continuing till Es,the points !ill be

    i.  #>#F#i.

    ii.  y>yFyi.

    44. $lace pi#els using putpi#el at points (#,y) in specified colour.

    4

  • 8/18/2019 CGLab File for college

    15/38

    "A$ !O DRA" A LNE %SNG DDA ALGOR!HM

    GincludeEstdio.hH

    GincludeEconio.hH

    GincludeEgraphics.hH

    void main()

    int #,y,#4,##yabs(d#)&

    else

    s>abs(dy)&

    #i>d#Os&

    yi>dyOs&

    #>#4&

    y>y4&

     putpi#el(#,y,B)&

    for(>8&Es&FF)

  • 8/18/2019 CGLab File for college

    16/38

    #>#F#i&

    y>yFyi&

     putpi#el(#,y,B)&

    N

    getch()&

    closegraph()&

    N

  • 8/18/2019 CGLab File for college

    17/38

    #!"P!"

  • 8/18/2019 CGLab File for college

    18/38

      EXPERIMENT 4.

    BRESENHAM’S ALGOR!HM !O DRA" A &R&LE#

    4.  'tart.

    =+T+T,gmode.?.  Initialise the graphic mode !ith the path location in T folder.

    5.  Input the radius of the circle r.

    3.  :oad #-8,y>r,initial decision parameter p>4-r.so the first point is (8,r).

    @.  Cepeat 'tep B !hile (#Ey) and increment #-value simultaneously.

    B.  If (pH8),do p>pFpF

  • 8/18/2019 CGLab File for college

    19/38

    "A$ !O DRA" A &R&LE %SNG BRESENHAM’SALGOR!HM.

    GincludeEstdio.hH

    GincludeEconio.hH

    GincludeEgraphics.hH

    void circlepoints(int,int)&

    void main()

    int #,y,p,r&

    int gdriver>=+T+T,gmode&

    initgraph(Jgdriver,Jgmode,KALLtcLLbgiAK)&

    clrscr()&

     printf(Kenter the radiusK)&

    scanf(KMdK,Jr)&

    #>8&y>r&p>4-r&

    !hile(#Ey)

    #FF&

    if(pH8)

     p>pFpF

  • 8/18/2019 CGLab File for college

    20/38

     putpi#el(#F?88,yF?88,2)&

     putpi#el(#F?88,-yF?88,2)&

     putpi#el(-#F?88,yF?88,2)&

     putpi#el(-#F?88,-yF?88,2)&

     putpi#el(yF?88,#F?88,2)&

     putpi#el(yF?88,-#F?88,2)&

     putpi#el(-yF?88,#F?88,2)&

     putpi#el(-yF?88,-#F?88,2)&

    N

  • 8/18/2019 CGLab File for college

    21/38

    #!"P!"

  • 8/18/2019 CGLab File for college

    22/38

      EXPERIMENT 5.

    PR#GRA3 "# REFLEC" A "RIANGLE

    GincludeEiostream.hH

    GincludeEconio.hH

    GincludeEgraphics.hH

    GincludeEprocess.hH

    GincludeEmath.hH

    void main()

    clrscr()&

    int graphdriver>=+T+T,graphmode&

    initgraph(Jgraphdriver,Jgraphmode,K...LLbgiK)&

    int #,y,#4,aP?QP?Q&

    double bP?QP?Q,cP?QP?Q&

    coutEEKLn +nter Ist coordinates of triangleAK&

    cinHHaP8QP8QHHaP4QP8Q&

    coutEEKLn +nter

  • 8/18/2019 CGLab File for college

    23/38

    line(aP8QP8Q,aP4QP8Q,aP8QP4Q,aP4QP4Q)&

    line(aP8QP4Q,aP4QP4Q,aP8QP

  • 8/18/2019 CGLab File for college

    24/38

    case ?A bP8QP8Q>@58-aP8QP8Q&

     bP8QP4Q>@58-aP8QP4Q&

     bP8QP@58-aP8QPaP4QP8Q&

     bP4QP4Q>aP4QP4Q&

     bP4QPaP4QP

  • 8/18/2019 CGLab File for college

    25/38

    #!"P!"

  • 8/18/2019 CGLab File for college

    26/38

  • 8/18/2019 CGLab File for college

    27/38

      EXPERIMENT 6.

    PR#GRA3 "# SCALE "E "RIANGLE

    GincludeEiostream.hH

    GincludeEconio.hH

    GincludeEgraphics.hH;

    void main()

    int gd>=+T+T,gm&

    initgraph(Jgd, Jgm,KK)&

    cleardevice()&

    int #4,y4,#

  • 8/18/2019 CGLab File for college

    28/38

    ysy*ys#*#?-#5&

    y?>sy*y?-y5&

     polyP8Q>#4&

     polyP4Q>y4&

     polyP#y#?&

     polyP3Q>y?&

     polyP@Q>#4&

     polyPBQ>y4&

    getch()&

    cleardevice()&

    dra!poly(5,poly)&

    getch()&

    closegraph()&

    N

  • 8/18/2019 CGLab File for college

    29/38

    #!"P!"

  • 8/18/2019 CGLab File for college

    30/38

  • 8/18/2019 CGLab File for college

    31/38

  • 8/18/2019 CGLab File for college

    32/38

      EXPERIMENT NO 7.

    "A$ !O RO!A!E A !RANGLE ABO%! ORGN.

    GincludeEiostream.hH

    GincludeEconio.hH

    GincludeEgraphics.hHGincludeEprocess.hH

    GincludeEmath.hH

    void main()

    clrscr()&

    int graphdriver>=+T+T,graphmode&

    initgraph(Jgraphdriver,Jgraphmode,K...LLbgiK)&

    int #,y,#4,aP?QP?Q&

    double bP?QP?Q,cP?QP?Q&

    coutEEKLn +nter Ist coordinates of triangleAK&

    cinHHaP8QP8QHHaP4QP8Q&

    coutEEKLn +nter

  • 8/18/2019 CGLab File for college

    33/38

     bP8QP8Q>bP4QP4Q>cos((#*?.45)O428)&

     bP8QP4Q>-sin((#*?.45)O428)&

     bP4QP8Q>sin((#*?.45)O428)&

     bP8&iE?&iFF)

    for(int R>8&RE?&RFF)

    cPiQPRQ>8&

    for (int >8& E?&FF)

    cPiQPRQF>aPiQPQ*bPQPRQ&

    N

    #4>(cPiQPRQF8.3)&

    aPiQPRQ>#4&

    N

    N

    coutEEKLn Triangle after rotation isALnK &

    line(aP8QP8Q,aP4QP8Q,aP8QP4Q,aP4QP4Q)&

    line(aP8QP4Q,aP4QP4Q,aP8QP

  • 8/18/2019 CGLab File for college

    34/38

    #!"P!"

  • 8/18/2019 CGLab File for college

    35/38

  • 8/18/2019 CGLab File for college

    36/38

  • 8/18/2019 CGLab File for college

    37/38

  • 8/18/2019 CGLab File for college

    38/38