CG FILE (2)

download CG FILE (2)

of 74

Transcript of CG FILE (2)

  • 8/8/2019 CG FILE (2)

    1/74

    1

    SUBMITED TO:- SUBMITED BY: -

    Mrs. Gurpreet Kaur SATYA PRAKASH YADAV

    DEPARTMENT OF CSE ROLL NO:-0715310050

    CS 3rd YAER

  • 8/8/2019 CG FILE (2)

    2/74

    2

    INDEX

    S.NO. EXPERIMENT NAME DATE REMARKS

    1 Implementation of line generation usingdda

    algorithm

    01/09/2009

    2 Implementation of line generation usingbresenham algorithm

    08/09/2009

    3 Implementation of circle generationusing

    Bresenham algorithm

    08/09/2009

    4 Implementation of polygonfilling usingboundryfill and floodfill algorithm

    22/09/2009

    5 Implementation of2-D transformation 20/10/2009

    6 Implementation of line clipping usingcohen Sutherland algorithm

    27/10/2009

    7 Implementation of polygon clipping usingcohen Sutherland algorithm

    27/10/2009

    8 Implementation of curve generation

    using

    Bezier curve

    03/10/2009

    9 Implementation of backface removal

    algorithm10/11/2009

  • 8/8/2019 CG FILE (2)

    3/74

    3

    EXPERIMENTNO.1

    Introduction:-

    In Computer graphics, a hard- or software implementation of a Digital Differential Analyzer (DDA) is

    used for linear interpolation of variables over an interval between start and end point. DDAs are used

    for rasterization of lines, triangles and polygons. In its simplest implementation the DDA algorithm

    interpolates values in interval [(xstart, ystart) ... (xend, yend)] by computing for each xi the equations

    xi = xi1+1, yi = yi1 + y/x, where x = xend xstart and y = yend ystart.

    Algorithm:-

    Step 1:[Determine The Dx & Dy]

    Dx=Xb-Xa

    Dy=Yb-Ya

    Step 2:[Determine Slope is Gentle or Sharp]

    If |Dx|>|Dy| then

    Gentle Slope

    M=Dy/Dx

    Set The Starting Point

  • 8/8/2019 CG FILE (2)

    4/74

    4

    If Dx>0 Then

    C=CELING(Xb)

    D=Yb+M*(C-Xb)

    F=FLOOR(Xa)

    R=FLOOR(D+0.5)

    H=R-D+M

    IF M>0 Then

    Positive Gentle Slope

    M1=M-1

    Now Step Through The Columns

    WHILE C

  • 8/8/2019 CG FILE (2)

    5/74

    5

    Program no.1

    #include

    #include

    #include

    #include

    #include

    #include

    void main()

    {

    clrscr();

    int x1,y1,x2,y2,l,dx,dy;

    float x,y,xinc,yinc;

    int gdriver=DETECT,gmode;

    initgraph(&gdriver,&gmode,"c:\\tc\\bgi ");

    coutx1;

    couty1;

    coutx2;

    cout

  • 8/8/2019 CG FILE (2)

    6/74

    6

    cin>>y2;

    putpixel(x1,y1,RED);

    dx=x2-x1;

    dy=y2-y1;

    if(abs(dy)>=abs(dx))

    l=abs(dy);

    else

    l=abs(dx);

    xinc=dx/l;

    yinc=dy/l;

    x=x1;

    y=y1;

    for(int i=1;i

  • 8/8/2019 CG FILE (2)

    7/74

    7

    Output

  • 8/8/2019 CG FILE (2)

    8/74

    8

    EXPERIMENT:-N0.2

    Introduction:-

    The Bresenham line algorithm is an algorithm which determines which points

    in an n-dimensional raster should be plotted in order to form a close

    approximation to a straight line between two given points. It is commonly

    used to draw lines on a computer screen, as it uses only integer addition,

    subtraction and bit shifting, all of which are very cheap operations in standard

    computer architectures. It is one of the earliest algorithms developed in the

    field of computer graphics. A minor extension to the original algorithm also

    deals with drawing circles.

    Algorithm:-

    function line(x0, x1, y0, y1)

    int deltax := x1 - x0

    int deltay := y1 - y0

    real error := 0

    real deltaerr := deltay / deltax // Assume deltax != 0 (line is not vertical),

    // note that this division needs to be done in a way that preserves the

    fractional part

    int y := y0

    for x from x0 to x1

    plot(x,y)

    error := error + deltaerr

    if abs(error) 0.5 then

    y := y + 1

    error := error - 1.0

  • 8/8/2019 CG FILE (2)

    9/74

    9

    Program No 2:-

    Program to implement Bresenhams Line Drawing Algorithm

    #include

    #include

    #include

    #include

    void main()

    {

    clrscr();

    int gdriver=DETECT,gmode;

    initgraph(&gdriver,&gmode,"c:\\tc\\bgi");

    float x1,x2,y1,y2,dx,dy,temp;

    float x,y,xend,yend;

    coutx1>>y1>>x2>>y2;

    dx=abs(x2-x1);

    dy=abs(y2-y1);

    if(dy>dx)

    {

    temp=dy;

    dy=dx;

  • 8/8/2019 CG FILE (2)

    10/74

    10

    dx=temp;

    }

    float p=dx-(2*dy);

    if(x1

  • 8/8/2019 CG FILE (2)

    11/74

    11

    else

    {

    p=p-2*dy;

    putpixel (ceil(x),ceil(y),1);

    }}

    getch();

    }

    output

  • 8/8/2019 CG FILE (2)

    12/74

    12

    Exeperiment No.3:-

    Introduction:-Circles have the property of being highly symetrical, which is handy when it comes to drawing them

    on a display screen.

    |y (This diagram is supposed to be a circle, try viewing

    | it in 50 line mode).

    \ ..... /

    . | . We know that there are 360 degrees in a circle. First we

    . \ | / . see that a circle is symetrical about the x axis, so

    . \|/ . only the first 180 degrees need to be calculated. Next

    --.---+---.-- we see that it's also symetrical about the y axis, so now

    . /|\ . x we only need to calculate the first90 degrees. Finally

    . / | \ . we see that the circle is also symetrical about the 45

    . | . degree diagonal axis, so we only need to calculate the

    / ..... \ first45 degrees.

    |

    |

    Bresenham's circle algorithm calculates the locations of the pixels in the first45 degrees. It assumes

    that the circle is centered on the origin. So for every pixel (x,y) it calculates we draw a pixel in each of

    the 8 octants of the circle :

    Algorithm:-PutPixel(CenterX + X, Center Y + Y)

    PutPixel(CenterX + X, Center Y - Y)

    PutPixel(CenterX - X, Center Y + Y)

    PutPixel(CenterX - X, Center Y - Y)

  • 8/8/2019 CG FILE (2)

    13/74

    13

    PutPixel(CenterX + Y, Center Y + X)

    PutPixel(CenterX + Y, Center Y - X)

    PutPixel(CenterX - Y, Center Y + X)

    PutPixel(CenterX - Y, Center Y - X)

    So let's get into the actual algorithm. Given a radius for the circle we perform this initialisation:

    d := 3 - (2 * RADIUS)

    x := 0

    y := RADIUS

    Now for each pixel we do the following operations:

    Draw the 8 circle pixels

    if d < 0 then

    d := d + (4 * x) + 6

    else

    begin

    d := d + 4 * (x - y) + 10

    y := y - 1;

    end;

    And we keep doing this until x = y. Note that the values added to the decision variable in this

    algorithm (x and y) are constantly changing, so we cannot precalculate them. The muliplicationshowever are by 4, and we can accomplish this by shifting left twice.

  • 8/8/2019 CG FILE (2)

    14/74

    14

    PROGRAMNO. 3

    //bresenham circle

    #include

    #include

    #include

    #include

    #include

    void main()

    {clrscr();

    float x,y,p,h,k,r;

    int gdriver=DETECT,gmode;

    initgraph(&gdriver,&gmode," ");

    coutr;

    couth>>k;

    x=0;

    y=r;

    p=3-(2*r);

    while(x

  • 8/8/2019 CG FILE (2)

    15/74

    15

    putpixel(x+h,y+k,RED);

    putpixel(y+h,x+k,RED);

    putpixel(-x+h,y+k,RED);

    putpixel(-y+h,x+k,RED);

    putpixel(-x+h,-y+k,RED);

    putpixel(-y+h,-x+k,RED);

    putpixel(x+h,-y+k,RED);

    putpixel(y+h,-x+k,RED);

    if(p

  • 8/8/2019 CG FILE (2)

    16/74

    16

    OUTPUT

  • 8/8/2019 CG FILE (2)

    17/74

    17

    Exeperiment No.4

    1.BoundaryFill

    Introduction:-

    Start at a point inside the figure and paint with a particular color.

    Filling continues until a boundary color is encountered.

    Thjere are two ways to do this:

    1. Four-connected fill where we propagate : left right up down

    Procedure Four_Fill (x, y, fill_col, bound_col: integer);

    var

    curr_color: integer;

    begin

    curr_color := inquire_color(x, y)

    if (curr_color bound color) and

    (curr_color fill_col) then

    begin

    set_pixel(x, y, fill_col)

    Four_Fill (x+1, y, fill_col, bound_col);

  • 8/8/2019 CG FILE (2)

    18/74

    18

    Four_Fill (x-1, y, fill_col, bound_col);

    Four_Fill (x, y+1, fill_col, bound_col);

    Four_Fill( x, y-1, fill_col, bound_col);

    end;

    There is the following problem with four_fill:

    Thisd leads to the 2. Eight-connected fill algorithm where we test all eight adjacent pixels.

  • 8/8/2019 CG FILE (2)

    19/74

    19

    So we add the calls:

    eight_fill (x+1, y-1, ...........)

    eight_fill (x+1, y+1, ..........)

    eight_fill (x-1, y-1, ............)

    eight_fill (x-1, y+1, ...........)

    Note: above 4-fill and 8-fill algorithms involve heavy duty recursion which may consumememory and time. Better algorithms are faster, but more complex. They make use of pixel

    runs (horizontal groups of pixels).

    PROGRAM NO. 4

    #include

    #include

    #include

    #include

    void fill_right(x,y)

    int x , y ;

    {

    if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))

    {

    putpixel(x,y,RED);

  • 8/8/2019 CG FILE (2)

    20/74

    20

    fill_right(++x,y);

    x = x - 1 ;

    fill_right(x,y-1);

    fill_right(x,y+1);

    }

    delay(1);

    }

    void fill_left(x,y)

    int x , y ;

    {

    if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))

    {

    putpixel(x,y,RED);

    fill_left(--x,y);

    x = x + 1 ;

    fill_left(x,y-1);

    fill_left(x,y+1);

    }

    delay(1);

    }

  • 8/8/2019 CG FILE (2)

    21/74

    21

    void main()

    {

    int x,y,n,i;

    int gd=DETECT,gm;

    clrscr();

    initgraph(&gd,&gm,"c:\\tc\\bgi");

    /*- draw object -*/

    line (50,50,200,50);

    line (200,50,200,300);

    line (200,300,50,300);

    line (50,300,50,50);

    /*- set seed point -*/

    x = 100; y = 100;

    fill_right(x,y);

  • 8/8/2019 CG FILE (2)

    22/74

    22

    fill_left(x-1,y);

    getch();

    }

    Output

  • 8/8/2019 CG FILE (2)

    23/74

    23

    2.floodFill

    Introduction:-

    he flood fill algorithm takes three parameters: a start node, a target color, and a replacement

    color. The algorithm looks for all nodes in the array which are connected to the start node bya path of the target color, and changes them to the replacement color. There are many ways

    in which the flood-fill algorithm can be structured, but they all make use of a queue or stack

    data structure, explicitly or implicitly. One implicitly stack-based (recursive) flood-fill

    implementation (for a two-dimensional array) goes as follows:

    Flood-fill (node, target-color, replacement-color):

    1. If the color ofnode is not equal to target-color, return.

    2. If the color ofnode is equal to replacement-color, return.

    3. Set the color ofnode to replacement-color.

    4. Perform Flood-fill (one step to the west ofnode, target-color, replacement-color).

    Perform Flood-fill (one step to the east ofnode, target-color, replacement-color).

    Perform Flood-fill (one step to the north ofnode, target-color, replacement-color).

    Perform Flood-fill (one step to the south ofnode, target-color, replacement-color).

    5. Return.

    PROGRAM NO. 4

    #include

    #include

    #include

    #include

    void fill_right(x,y)

    int x , y ;

    {

  • 8/8/2019 CG FILE (2)

    24/74

    24

    if(getpixel(x,y) == 0)

    {

    delay(1);

    putpixel(x,y,RED);

    fill_right(++x,y);

    x = x - 1 ;

    fill_right(x,y-1);

    fill_right(x,y+1);

    }

    }

    void fill_left(x,y)

    int x , y ;

    {

    if(getpixel(x,y) == 0)

    {

    delay(1);

    putpixel(x,y,RED);

    fill_left(--x,y);

    x = x + 1 ;

    fill_left(x,y-1);

  • 8/8/2019 CG FILE (2)

    25/74

    25

    fill_left(x,y+1);

    }

    }

    void main()

    {

    int x , y ,a[10][10];

    int gd, gm ,n,i;

    //detectgraph(&gd,&gm);

    //initgraph(&gd,&gm,"c:\\tc\\bgi");

    printf("\n\n\tEnter the no. of edges of polygon : ");

    scanf("%d",&n);

    printf("\n\n\tEnter the cordinates of polygon :\n\n\n ");

    for(i=0;i

  • 8/8/2019 CG FILE (2)

    26/74

    26

    a[n][0]=a[0][0];

    a[n][1]=a[0][1];

    printf("\n\n\tEnter the seed pt. : ");

    scanf("%d%d",&x,&y);

    detetctgraph(&gd,&gm);

    initgraph(&gd,&gm,"c:\\tc\\bgi");

    cleardevice();

    setcolor(WHITE);

    for(i=0;i

  • 8/8/2019 CG FILE (2)

    27/74

    27

    /*SAMPLE INPUT*/

    /*Enter the number of edges of polygon 4

    X0 Y0 = 50 50

    X1 Y1 = 200 50

    X2 Y2 = 200 300

    X3 Y3 = 50 300

    Enter the seed point 100 100*/

    Output:-

  • 8/8/2019 CG FILE (2)

    28/74

    28

    Experiment no.5

    Introduction:-2

    Transformaions are a fundamental part of computer graphics.

    Transformations are used to position objects, to shape objects, to changeviewing positions, and even to change how something is viewed (e.g. the type

    of perspective that is used).

    0

    PROGRAM NO. 5

    #include

    #include

    #include

    #include

    #include

    #define max 20

    int ct,n;

    float t[max][3],c[max][3],r[max][3];

    void drawaxis()

    {

    int i;

    setcolor(BLACK);

    line(0,240,640,240);

    line(320,0,320,480);

    for(i=0;i

  • 8/8/2019 CG FILE (2)

    29/74

    29

    {

    line(320+c[i][0],240-c[i][1],320+c[i+1][0],240-c[i+1][1]);

    }

    line(320+c[n-1][0],240-c[n-1][1],320+c[0][0],240-c[0][1]);

    }

    void getdata()

    {

    struct polyg

    {

    float x,y;

    }s[20];

    int i;

    printf("\n\n\t\tGive total no of Verices : ");

    scanf("%d",&n);

    if(ct==0)

    {

    ct=1;

    }

    if(n>20)

    {

    printf("\n\n\t\t ENter vertices less then 20.");

    }

    else

  • 8/8/2019 CG FILE (2)

    30/74

    30

    {

    for(i=0;i

  • 8/8/2019 CG FILE (2)

    31/74

    31

    line(320+t[i][0],240-t[i][1],320+t[i+1][0],240-t[i+1][1]);

    }

    line(320+t[n-1][0],240-t[n-1][1],320+t[0][0],240-t[0][1]);

    }

    }

    void translate(float tx,float ty)

    {

    float p[3][3]={1,0,0,0,1,0,0,0,1},sum;

    int i,j,k;

    p[2][0]=tx;

    p[2][1]=ty;

    for(i=0;i

  • 8/8/2019 CG FILE (2)

    32/74

    32

    }

    void scale(float sx,float sy)

    {

    int i,j,k,sum;

    float p[3][3]={1,0,0,0,1,0,0,0,1};

    p[0][0]=sx;

    p[1][1]=sy;

    for(i=0;i

  • 8/8/2019 CG FILE (2)

    33/74

    33

    float p[3][3]={0,0,0,0,0,0,0,0,1},sum;

    p[0][0]=cos(x);

    p[0][1]=sin(x);

    p[1][0]=sin(x);

    p[1][1]=cos(x);

    for(i=0;i

  • 8/8/2019 CG FILE (2)

    34/74

    34

    p[0][1]=sin(x);

    p[1][0]=sin(x);

    p[1][1]=cos(x);

    for(i=0;i

  • 8/8/2019 CG FILE (2)

    35/74

    35

    sum=0;

    for(k=0;k

  • 8/8/2019 CG FILE (2)

    36/74

    36

    tmat[0][0]=-1;

    multy(tmat);

    break;

    case 2:

    tmat[1][1]=-1;

    multy(tmat);

    break;

    case 3:

    tmat[0][0]=-1;

    tmat[1][1]=-1;

    multy(tmat);

    break;

    case 4:

    tmat[0][0]=0;

    tmat[0][1]=1;

    tmat[1][0]=1;

    tmat[1][1]=0;

    multy(tmat);

    break;

    case 5:

    tmat[0][0]=0;

    tmat[0][1]=-1;

    tmat[1][0]=-1;

  • 8/8/2019 CG FILE (2)

    37/74

    37

    tmat[1][1]=0;

    multy(tmat);

    break;

    }

    getch();

    }

    void shear()

    {

    int opt;

    float tmat[3][3]={1,0,0,0,1,0,0,0,1},a,b;

    printf("\n\n\t\t1.Y-Shear");

    printf("\n\t\t2.X-Shear");

    printf("\n\n\n\n\t\t Enter your choice : ");

    scanf("%d",&opt);

    switch(opt)

    {

    case 1:

    printf("\n\n\t\t Enter value of a : ");

    scanf("%f",&a);

    tmat[0][1]=a;

    multy(tmat);

    break;

    case 2:

  • 8/8/2019 CG FILE (2)

    38/74

    38

    printf("\n\n\t\tEnter value of b : ");

    scanf("%f",&b);

    tmat[1][0]=b;

    multy(tmat);

    break;

    }

    getch();

    }

    void main()

    {

    clrscr();

    float tx,ty,sx,sy,x;

    int gmode,gdrive=DETECT,ch;

    initgraph(&gdrive,&gmode,"C:\\tc\\bgi");

    getdata();

    while(1)

    {

    clrscr();

    drawaxis();

    display(t);

    printf("\n\t\t1.Translate");

    printf("\n\t\t2.Scaling");

    printf("\n\t\t3.Clock");

  • 8/8/2019 CG FILE (2)

    39/74

    39

    printf("\n\t\t4.AntiClock");

    printf("\n\t\t5.Reflection");

    printf("\n\t\t6.Shearing");

    printf("\n\t\t7.Exit");

    printf("\n\n\n\t\t Enter your choice : ");

    scanf("%d",&ch);

    switch(ch)

    {

    case 1:

    printf("\n\n\t\tEnter the x distance : ");

    scanf("%f",&tx);

    printf("\n\n\t\tEnter the y distance : ");

    scanf("%f",&ty);

    translate(tx,ty);

    display(r);

    break;

    case 2:

    printf("\n\n\t\tEnter the Horizonatal Value : ");

    scanf("%f",&sx);

    printf("\n\n\t\tEnter the Vertical value : ");

    scanf("%f",&sy);

    scale(sx,sy);

    display(r);

  • 8/8/2019 CG FILE (2)

    40/74

    40

    break;

    case 3:

    printf("\n\n\t\tEnter the Angle : ");

    scanf("%f",&x);

    clock(x);

    display(r);

    break;

    case 4:

    printf("\n\n\t\tEnter the Angle : ");

    scanf("%f",&x);

    anti(x);

    display(r);

    break;

    case 5:

    reflect();

    display(r);

    break;

    case 6:

    shear();

    display(r);

    break;

    case 7:

    exit(0);

  • 8/8/2019 CG FILE (2)

    41/74

    41

    }

    getch();

    }

    }

    OUTPUT:-

  • 8/8/2019 CG FILE (2)

    42/74

    42

    Exeperiment No.6 line clipping(using cohen sutherland)

    Introduction:-

    Every line endpoint is assigned a 4 bit Region code. The appropriate bit is set

    depending on the location of the endpoint with respect to that windowcomponent as shown below:

    Endpoint Left of window then

    set bit 1

    Endpoint Right of window

    then set bit2

    Endpoint Below window then

    set bit 3

    Endpoint Above window thenset bit4

    Example:

    P1 -> 0001, P 2 -> 1000

    P3 -> 0001, P4 -> 0100

    P5 -> 0000, P6 -> 0010

    P7 -> 0001, P8 -> 0001

    Can determine the bit code by testing the endpoints with window as follows:

    If x is less than Xwmin then set bit 1

    If x is greater than Xwmax then set bit2

    If y is less than Ywmin then set bit 3

    If y is greater than Ywmax then set bit4

    Note: can use 4 element Boolean matrix and set C[Left] = true / false (1/0). If

    both endpoints = 0000 (in window) then display line. If both endpoints have a

    bit set in same position (P7, P8) then the line is completely outside the

    window and is rejected.

    So: can do logical AND of region codes and reject if result is 0000

    Can do logical OR of region codes and accept if result = 0000

  • 8/8/2019 CG FILE (2)

    43/74

    43

    For the rest of the lines we must check for intersection with window. May still

    be outside, e.g. P3 - P4 in the above image. If point is to Left of window then

    compute intersection with Left window boundary. Do the same for Right,

    Bottom, and Top. Then recompute region code restest. So the algorithm is as

    follows:

    Compute region code for endpoints

    Check for trivial accept or reject

    If step 2 unsuccessful then compute window intersections in order: Left, Right,

    Bottom, Top (only do 1)

    Repeat steps 1, 2,3 until done.

    Example

    I. 1) P1 = 0001 2) no 3) P1 = P'1

    P2 = 1000

    II. 1) P'1= 0000 2) no 3) P2 = P' 2

    P2 = 1000

    III. 1) P'1 = 0000 2) Yes - accept & display

    P'2 = 0000

    Look at how to compute the line intersections

    for P'1: m = dy/dx = (y1 - y'1)/(x1 - x'1) P1(x1, y1)

    P'1(x'1, y'1)

    or y'1 = y1 + m( x'1 - x1)

    but for Left boundary x'1 = Xwmin

    for Right boundary x'1 = Xwmax

    Similarly for Top / Bottom, e.g. P'3

    x'3 = x3 + (y'3 - y3) / m

  • 8/8/2019 CG FILE (2)

    44/74

    44

    for Top y'3 = Ywmax for Bottom y'3 = Ywmin

    PROGRAM NO. 6

    #include

    #include

    #include

    typedef unsigned int outcode;

    enum { TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 };

    void lineclip(x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax )

    float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax;

    {

    int gd,gm;

    outcode code0,code1,codeout;

    int accept = 0, done=0;

    code0 = calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);

    code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);

  • 8/8/2019 CG FILE (2)

    45/74

    45

    do{

    if(!(code0 | code1))

    { accept =1 ; done =1; }

    else

    if(code0 & code1) done = 1;

    else

    {

    float x,y;

    codeout = code0 ? code0 : code1;

    if(codeout & TOP)

    {

    x = x0 + (x1-x0)*(ywmax-y0)/(y1-y0);

    y = ywmax;

    }

    else

    if( codeout & BOTTOM)

    {

    x = x0 + (x1-x0)*(ywmin-y0)/(y1-y0);

    y = ywmin;

    }

    else

    if ( codeout & RIGHT)

    {

  • 8/8/2019 CG FILE (2)

    46/74

    46

    y = y0+(y1-y0)*(xwmax-x0)/(x1-x0);

    x = xwmax;

    }

    else

    {

    y = y0 + (y1-y0)*(xwmin-x0)/(x1-x0);

    x = xwmin;

    }

    if( codeout == code0)

    {

    x0 = x; y0 = y;

    code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);

    }

    else

    {

    x1 = x; y1 = y;

    code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);

    }

    }

    } while( done == 0);

    if(accept) line(x0,y0,x1,y1);

  • 8/8/2019 CG FILE (2)

    47/74

    47

    rectangle(xwmin,ywmin,xwmax,ywmax);

    getch();

    }

    int calcode (x,y,xwmin,ywmin,xwmax,ywmax)

    float x,y,xwmin,ywmin,xwmax,ywmax;

    {

    int code =0;

    if(y> ywmax)

    code |=TOP;

    else if( y xwmax)

    code |= RIGHT;

    else if ( x< xwmin)

    code |= LEFT;

    return(code);

    }

  • 8/8/2019 CG FILE (2)

    48/74

    48

    main()

    {

    float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;

    int gd=DETECT,gm;

    clrscr();

    //initgraph(&gd,&gm,"c:\\tc\\bgi");

    printf("\n\n\tEnter the co-ordinates of Line :");

    printf("\n\n\tX1 Y1 : ");

    scanf("%f %f",&x1,&y1);

    printf("\n\n\tX2 Y2 : ");

    scanf("%f %f",&x2,&y2);

    printf("\n\tEnter the co_ordinates of window :\n ");

    printf("\n\txwmin , ywmin : ");

    scanf("%f %f",&xwmin,&ywmin);

  • 8/8/2019 CG FILE (2)

    49/74

    49

    printf("\n\txwmax , ywmax : ");

    scanf("%f %f",&xwmax,&ywmax);

    initgraph(&gd,&gm,"c:\\tc\\bgi");

    clrscr();

    line(x1,y1,x2,y2);

    rectangle(xwmin,ywmin,xwmax,ywmax);

    getch();

    clrscr();

    lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );

    getch();

    closegraph();

    }

  • 8/8/2019 CG FILE (2)

    50/74

    50

    Output:-

  • 8/8/2019 CG FILE (2)

    51/74

    51

    Exeperiment No.7 polygon clipping (using cohen sutherland)

    Introduction:-

    an algorithm performs a clipping of a polygon against each window edge in

    turn. It accepts an ordered sequence of verices v1, v2, v3, ..., vn and puts out a

    set of vertices defining the clipped polygon.

    This figure represents a polygon (the large, solid, upward pointing arrow)

    before clipping has occurred.

    The following figures show how this algorithm works at each edge, clipping

    the polygon.

    Clipping against the left side of the clip window.

    Clipping against the top side of the clip window.

    Clipping against the right side of the clip window.

    Clipping against the bottom side of the clip window.

  • 8/8/2019 CG FILE (2)

    52/74

    52

    Algorithm:-

    How To Calculate Intersections

    Assume that we're clipping a polgon's edge with vertices at (x1,y1) and (x2,y2)

    against a clip window with vertices at (xmin, ymin) and (xmax,ymax).

    The location (IX, IY) of the intersection of the edge with the left side of the

    window is:

    IX = xmin

    IY = slope*(xmin-x1) + y1, where the slope = (y2-y1)/(x2-x1)

    The location of the intersection of the edge with the right side of the window

    is:

    IX = xmax

    IY = slope*(xmax-x1) + y1, where the slope = (y2-y1)/(x2-x1)

    The intersection of the polygon's edge with the top side of the window is:

    IX = x1 + (ymax - y1) / slope

    IY = ymax

    Finally, the intersection of the edge with the bottom side of the window is:

    IX = x1 + (ymin - y1) / slope

    IY = ymin

  • 8/8/2019 CG FILE (2)

    53/74

    53

    Program no.7:-

    #include

    #include

    #include

    #include

    #include

    #include

    typedef struct edge

    {

    int x1,y1;

    int outcode[4];

    }EDGE;

    EDGE ed[20];

    int xl, yl;

    int xh,yh;

    int n;

    void acceptlines()

    {

    int i,j;

    printf ("enter no of lines\n",n);

    scanf ("%d",&n);

  • 8/8/2019 CG FILE (2)

    54/74

    54

    for(j=0;j

  • 8/8/2019 CG FILE (2)

    55/74

    55

    }

    int visb_check(EDGE p1,EDGE p2)

    {

    int j,temp=0;

    EDGE Etemp;

    for(j=0;j

  • 8/8/2019 CG FILE (2)

    56/74

    56

    temp=0;

    for(j=0;j

  • 8/8/2019 CG FILE (2)

    57/74

    57

    ed.outcode[1]=0 ;

    if(ed.x1=0;i--)

    {

    if(outp1.outcode[i]==0&&outp2.outcode[i]==0)

  • 8/8/2019 CG FILE (2)

    58/74

    58

    continue;

    else if(outp2.outcode[i]==1)

    goto new_code1;

    else

    goto new_code2;

    }

    setcolor(RED);

    line(outp1.x1,outp1.y1,outp2.x1, outp2.y1);

    return;

    new_code1:

    switch(i)

    {

    case 2 :

    tempx=xh;

    tempy=((xh-outp1.x1)*(outp1.y1-outp2.y1)/(outp1.x1-outp2.x1))+outp1.y1;

    break;

    case 3:

    tempx=xl;

    tempy=((xl-outp1.x1)*(outp1.y1-outp2.y1)/(outp1.x1-outp2.x1))+outp1.y1;

    break;

    case 0 :

    tempy=yh;

    tempx=((yh-outp1.y1)*(outp1.x1-outp2.x1)/(outp1.y1-outp2.y1))+outp1.x1;

    break;

    case 1 :

    tempy=yl;

  • 8/8/2019 CG FILE (2)

    59/74

    59

    tempx=((yl-outp1.y1)*(outp1.x1-outp2.x1)/(outp1.y1-outp2.y1))+outp1.x1;

    break;

    }

    outp2.x1=tempx;

    outp2.y1=tempy;

    outp2=outcode(outp2);

    goto up;

    new_code2:

    switch(i)

    {

    case 2 :

    tempx=xh;

    tempy=((xh-outp2.x1)*(outp2.y1-outp1.y1)/(outp2.x1-outp2.x1))+outp2.y1;

    break;

    case 3 :

    tempx=xl;

    tempy=((xl-outp2.x1)*(outp2.y1-outp1.y1)/(outp2.x1-outp1.x1))+outp2.y1;

    break;

    case 0 :

    tempy=yh;

    tempx=((yh-outp2.y1)*(outp2.x1-outp1.x1)/(outp2.y1-outp1.y1))+outp2.x1;

    break;

    case 1 :

    tempy=yh;

    tempx=((yl-outp2.y1)*(outp2.x1-outp1.x1)/(outp2.y1-outp1.y1))+outp2.x1;

    break;

  • 8/8/2019 CG FILE (2)

    60/74

    60

    }

    outp1.x1=tempx;

    outp1.y1=tempy;

    outp1=outcode(outp1);

    goto up;

    }

    }

    void main()

    {

    int gd=DETECT,gm,i;

    initgraph(&gd,&gm,"c:\\tc\\bgi");

    printf("Cohen Sutherland Line clipping algorithm\n");

    acceptwindow();

    acceptlines();

    cleardevice();

    outtextxy(20,10,"*********Before clip*********");

    displaywindow();

    displaylines();

    getch();

    cleardevice();

    outtextxy(20,10,"***********after clip*******");

    displaywindow();

    for(i=0;i

  • 8/8/2019 CG FILE (2)

    61/74

    61

    EXEPERIMENT NO:-8 CUBIC BEZIER CURVE

    INTRODUCTION:-

    In the mathematical field ofnumericalanalysis, a Bzier curve is a parametric

    curve important in computergraphics and related fields. Generalizations of

    Bzier curves to higher dimensions are called Bziersurfaces, of which the

    Bziertriangle is a special case.

    In vectorgraphics, Bzier curves are an important tool used to model smooth

    curves that can be scaled indefinitely. "Paths," as they are commonly referred

    to in image manipulation programs such as Inkscape,AdobeIllustrator,Adobe

    Photoshop, and GIMP are combinations of Bzier curves patched together.

    Paths are not bound by the limits ofrasterizedimages and are intuitive to

    modify

    PROGRAM NO. 8

    #include

    #include

    #include

    int x,y,z;

    void main()

    {

    float u;

    int gd,gm,ymax,i,n,c[4][3];

  • 8/8/2019 CG FILE (2)

    62/74

    62

    //initgraph(&gd,&gm,"c:\\tc\\bgi");

    for(i=0;i

  • 8/8/2019 CG FILE (2)

    63/74

    63

    ymax = 480;

    setcolor(13);

    for(i=0;i

  • 8/8/2019 CG FILE (2)

    64/74

    64

    }

    }

    getch();

    }

    bezier(u,n,p)

    float u;int n; int p[4][3];

    {

    int j;

    float v,b;

    float blend(int,int,float);

    x=0;y=0;z=0;

    for(j=0;j

  • 8/8/2019 CG FILE (2)

    65/74

    65

    int k;

    float v,blend;

    v=C(n,j);

    for(k=0;k

  • 8/8/2019 CG FILE (2)

    66/74

    66

    for(k=1;k

  • 8/8/2019 CG FILE (2)

    67/74

    67

    OUTPUT:-

  • 8/8/2019 CG FILE (2)

    68/74

    68

    EXPERIMENT NO. 9

    Z BUFERINTRODUCTION:-

    The Z-buffer algorithm is a convenient algorithm for rendering images

    properly according to depth. To begin with, a buffer containing the closest

    depth at each pixel location is created parallel to the image buffer. Each

    location in this depth buffer is initialized to negative infinity. Now, the

    zIntersect and dzPerScanline fields are added to each edge record in the

    polyfill algorithm. For each point that is to be rendered, the depth of the point

    against the depth of the point at the desired pixel location. If the point depth is

    greater than the depth at the current pixel, the pixel is colored with the new

    color and the depth buffer is updated. Otherwise, the point is not rendered

    because it is behind another object.

    PROGRAM NO. 9

    #include

    #include

    #include

    #include

    #define TRUE 1

    #define FALSE 0

    struct wcpt3

    {

    float x;

    float y;

    float z;

  • 8/8/2019 CG FILE (2)

    69/74

    69

    };

    struct plane

    {

    float A;

    float B;

    float C;

    float D;

    };

    plane get_coeff(wcpt3 pt1, wcpt3 pt2

    , wcpt3 pt3)

    {

    plane temp;

    temp.A=(pt2.z-pt3.z)*(pt1.y-pt2.y)*(pt1.z-pt2.z)*(pt2.y-pt3.y);

    temp.B=(pt2.x-pt3.x)*(pt1.z-pt2.z)*(pt1.x-pt2.x)*(pt2.z-pt3.z);

    temp.C=(pt2.y-pt3.y)*(pt1.x-pt2.x)*(pt1.y-pt2.y)*(pt2.x-pt3.x);

    temp.D=-pt1.x*(pt2.y*pt3.z*pt2.z*pt3.y)+pt1.y*(pt2.x*pt3.z-pt2.z*pt3.x)-

    pt1.z*(pt2.x*pt3.y-pt2.y*pt3.x);

    return temp;

    }

    float depth(plane surface,float x,float y)

    {

    float z=0;

    if(surface.C!=0)

  • 8/8/2019 CG FILE (2)

    70/74

    70

    z=(-surface.A*x-surface.B*y-surface.D)/surface.C;

    return z;

    }

    int side_check(wcpt3 ptA,wcpt3 ptB,wcpt3 ptC,float x,float y)

    {

    float fxyC,fxy;

    fxyC=ptC.x*(ptA.y-ptB.y)-ptC.y*(ptA.x-ptB.x)-

    ptA.x*(ptA.y=ptB.y)+ptA.y*(ptA.x-ptB.x);

    fxy=x*(ptA.y-ptB.y)-y*(ptA.x-ptB.x)-ptA.x*(ptA.y-ptB.y)+ptA.y*(ptA.x-ptB.x);

    if(((fxyC0)))

    return TRUE;

    else

    return FALSE;

    }

    int inside_triangle_check(wcpt3 pt1,wcpt3 pt2,wcpt3 pt3,float x,float y)

    {

    int

    check=side_check(pt1,pt2,pt3,x,y)&&side_check(pt2,pt3,pt1,x,y)&&side_check

    (pt3,pt1,pt2,x,y);

    if(check==TRUE)

    return TRUE;

    else

    return FALSE;

  • 8/8/2019 CG FILE (2)

    71/74

    71

    }

    void main(void)

    {

    int gd=DETECT,gm;

    initgraph(&gd,&gm,"c:\\tc\\bgi");

    // clrscr();

    wcpt3 ptA,ptB,ptC,ptD;

    plane surface[10];

    int clr_background=BLACK,clr,numsurf,colorxy[4],surf_clr[10];

    float depth_background=0,depthxy[4],x,y,z;

    //clrscr();

    ptA.x=200; ptA.y=200; ptA.z=200;

    ptB.x=600; ptB.y=200; ptB.z=200;

    ptC.x=400; ptC.y=200; ptC.z=600;

    ptD.x=400; ptD.y=400; ptD.z=400;

  • 8/8/2019 CG FILE (2)

    72/74

    72

    surface[0]=get_coeff(ptA,ptC,ptD); surf_clr[0]=BLUE;

    surface[1]=get_coeff(ptC,ptB,ptD); surf_clr[1]=GREEN;

    surface[2]=get_coeff(ptB,ptA,ptD); surf_clr[2]=CYAN;

    surface[3]=get_coeff(ptA,ptC,ptB); surf_clr[3]=RED;

    for(x=0;x

  • 8/8/2019 CG FILE (2)

    73/74

    73

    z=depth_background;

    clr=clr_background;

    for(numsurf=0;numsurfz)

    {

    z=depthxy[numsurf];

    clr=surf_clr[numsurf];

    }

    }

    putpixel(x,y,clr);

    }

    getch();

    restorecrtmode();

    }

    }

  • 8/8/2019 CG FILE (2)

    74/74

    74

    OUTPUT