Computer Graphics Program source codes with full description.

Study of Various C Graphics Functions

Implementation and Using mouse in DOS

Implementation of DDA line algorithm with source code in C/C++.

Implementation of Bresenham Line algorithm with source code in C/C++.

Implementation of Midpoint Line algorithm with source code in C/C++.

Implementation of Bresenham Circle algorithm with source code in C/C++.

Implementation of Mid-point Circle algorithm with source code in C/C++.

Implementation of Mid-point Ellipse algorithm with source code in C/C++.

Implementation of Polygon Filling using Scan Fill with source code in C/C++.

Implementation of Polygon Filling using Flood Fill with source code in C/C++.

Implementation of Polygon Filling using Boundary Fill Algorithms.

Implementation of algorithm of 2D Transformation of an Object with source code in C/C++.

Implementation of Line Clipping using Cohen- Sutherland algorithm with source code in C/C++.

Implementation of Line Clipping using Liang-Barky algorithm with source code in C/C++.

Implementation of Polygon clipping using Sutherland-hodgeman algorithm with source code in C/C++.

Search

Friday, 1 July 2011

Implement Line Clipping using Cohen- Sutherland algorithm

Source Code:



#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#define MAX 20

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

enum { FALSE, TRUE };
typedef unsigned int outcode;

outcode compute_outcode(int x, int y,
int xmin, int ymin, int xmax, int ymax)
{
    outcode oc = 0;

    if (y > ymax)
oc |= TOP;
    else if (y < ymin)
oc |= BOTTOM;


    if (x > xmax)
oc |= RIGHT;
    else if (x < xmin)
oc |= LEFT;

    return oc;
}

void cohen_sutherland (double x1, double y1, double x2, double y2,
double xmin, double ymin, double xmax, double ymax)
{
    int accept;
    int done;
    outcode outcode1, outcode2;

    accept = FALSE;
    done = FALSE;

    outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax, ymax);
    outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax, ymax);
    do
    {
if (outcode1 == 0 && outcode2 == 0)
{
   accept = TRUE;
   done = TRUE;
}
else if (outcode1 & outcode2)
{
   done = TRUE;
}
else
{
   double x, y;
   int outcode_ex = outcode1 ? outcode1 : outcode2;
   if (outcode_ex & TOP)
   {
x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1);
y = ymax;
   }

   else if (outcode_ex & BOTTOM)
   {
x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1);
y = ymin;
   }
   else if (outcode_ex & RIGHT)
   {
y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1);
x = xmax;
   }
   else
   {
y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);
x = xmin;
   }
   if (outcode_ex == outcode1)
   {
x1 = x;
y1 = y;
outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax, ymax);
   }
   else
   {
x2 = x;
y2 = y;
outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax, ymax);
   }
}
    } while (done == FALSE);

    if (accept == TRUE)
line (x1, y1, x2, y2);
}



void main()
{
    int n;
    int i, j;
    int ln[MAX][4];
    int clip[4];
    int gd = DETECT, gm;

   /* printf ("Enter the number of lines to be clipped:");
    scanf ("%d", &n);

    printf ("Enter the x- and y-coordinates of the line-endpoints:\n");
    for (i=0; i<n; i++)
for (j=0; j<4; j++)
   scanf ("%d", &ln[i][j]);
     */
    printf ("Enter the coordinates of the clipping window:\n");
    for (i=0; i<4; i++)
scanf ("%d", &clip[i]);

    initgraph (&gd, &gm, "..//bgi");
    setcolor(GREEN);
gotoxy(0,2);
printf("before clipping");
setcolor(RED);
    rectangle (clip[0], clip[1], clip[2], clip[3]);
setcolor(WHITE);
line(250,100,250,450);
line(300,100,300,450);
line(350,100,350,450);
line(100,250,450,250);
line(100,300,450,300);
line(100,350,450,350);
line(150,350,350,150);
line(150,450,450,150);
line(250,450,450,250);
line(250,150,450,350);
line(150,150,450,450);
line(150,250,350,450);
    /*for (i=0; i<n; i++)
    {
line (ln[i][0], ln[i][1], ln[i][2], ln[i][3]); } */
    getch();
    cleardevice();
setcolor(RED);
gotoxy(0,3);
printf("after clipping");
    rectangle (clip[0], clip[1], clip[2], clip[3]);
setcolor(WHITE);
    //for (i=0; i<n; i++)
    //{
cohen_sutherland (250,100,250,450,clip[0], clip[1], clip[2], clip[3]);
cohen_sutherland (300,100,300,450,clip[0], clip[1], clip[2], clip[3]);
cohen_sutherland (350,100,350,450,clip[0], clip[1], clip[2], clip[3]);
cohen_sutherland (100,250,450,250,clip[0], clip[1], clip[2], clip[3]);
cohen_sutherland (100,300,450,300,clip[0], clip[1], clip[2], clip[3]);
cohen_sutherland (100,350,450,350,clip[0], clip[1], clip[2], clip[3]);
cohen_sutherland (150,350,350,150,clip[0], clip[1], clip[2], clip[3]);
cohen_sutherland (150,450,450,150,clip[0], clip[1], clip[2], clip[3]);
cohen_sutherland (250,450,450,250,clip[0], clip[1], clip[2], clip[3]);
cohen_sutherland (250,150,450,350,clip[0], clip[1], clip[2], clip[3]);
cohen_sutherland (150,150,450,450,clip[0], clip[1], clip[2], clip[3]);
cohen_sutherland (150,250,350,450,clip[0], clip[1], clip[2], clip[3]);


   // }
getch();
    closegraph();
}

click here to download the source code



Theory/logic:

This algorithm is used to clip the line against givan rectangle window. As you proceed around the window, extending each edge and defining an inside half-space and an outside half-space, nine regions are created - the eight "outside" regions and the one "inside" region. Each of the nine regions associated with the window is assigned a 4-bit code to identify the region. Each bit in the code is set to either a 1(true) or a 0(false). If the region is to the left of the window, the first bit of the code is set to 1. If the region is to the top of the window, the second bit of the code is set to 1. If to the right, the third bit is set, and if to the bottom, the fourth bit is set. The 4 bits in the code then identify each of the nine regions as shown below.

For any endpoint ( x , y ) of a line, the code can be determined that identifies which region the endpoint lies. The code's bits are set according to the following conditions:

The sequence for reading the codes' bits is LRBT (Left, Right, Bottom, Top).
Once the codes for each endpoint of a line are determined, the logical AND operation of the codes determines if the line is completely outside of the window. If the logical AND of the endpoint codes is not zero, the line can be trivally rejected. For example, if an endpoint had a code of 1001 while the other endpoint had a code of 1010, the logical AND would be 1000 which indicates the line segment lies outside of the window. On the other hand, if the endpoints had codes of 1001 and 0110, the logical AND would be 0000, and the line could not be trivally rejected.
The logical OR of the endpoint codes determines if the line is completely inside the window. If the logical OR is zero, the line can be trivally accepted. For example, if the endpoint codes are 0000 and 0000, the logical OR is 0000 - the line can be trivally accepted. If the endpoint codes are 0000 and 0110, the logical OR is 0110 and the line can not be trivally accepted.

Data Structure: none.

Advantage:
àEasy to implement.
à First check is performed to find whether line is to be accepted or rejected, so no need to do any calculations if line is to be rejected completely.

Disadvantage:
à It is limited to rectangle window only.
à Intersection point calculations are required.

Application : In window to viewport transformation, when the region is to be
                       Clipped, then this algorithm is used to clip the lines.
I/P & O/P: Before Clipping (Input) :

How Algorithm works?
  1. Consider the line segment AD.
Point A has an outcode of 0000 and point D has an outcode of 1001. The logical AND of these outcodes is zero; therefore, the line cannot be trivally rejected. Also, the logical OR of the outcodes is not zero; therefore, the line cannot be trivally accepted. The algorithm then chooses D as the outside point (its outcode contains 1's). By our testing order, we first use the top edge to clip AD at B. The algorithm then recomputes B's outcode as 0000. With the next iteration of the algorithm, AB is tested and is trivially accepted and displayed.
  1. Consider the line segment EI
Point E has an outcode of 0100, while point I's outcode is 1010. The results of the trivial tests show that the line can neither be trivally rejected or accepted. Point E is determined to be an outside point, so the algorithm clips the line against the bottom edge of the window. Now line EI has been clipped to be line FI. Line FI is tested and cannot be trivially accepted or rejected. Point F has an outcode of 0000, so the algorithm chooses point I as an outside point since its outcode is1010. The line FI is clipped against the window's top edge, yielding a new line FH. Line FH cannot be trivally accepted or rejected. Since H's outcode is 0010, the next iteration of the algorthm clips against the window's right edge, yielding line FG. The next iteration of the algorithm tests FG, and it is trivially accepted and display.
After Clipping (Output) :
After clipping the segments AD and EI, the result is that only the line segment AB and FG can be seen in the window.



Conclusion: Cohen Sutherland algorithm is easy to implement algorithm to clip the given line against rectangle window.

No comments:

Post a Comment

Copyright Text

Copyright @ LDRP Student Community, webmaster Rahul Bhadauriya