Skip to main content

Cohen Sutherland Line Clipping Algorithm in C Programming

#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;

    initgraph (&gd, &gm, "");
    setbkcolor(WHITE);
    setcolor(BLUE);

    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 x- and y-coordinates of the left-top and right-:");
    printf ("bottom corners\nof the clip window:\n");
    for (i=0; i<4; i++)
 scanf ("%d", &clip[i]);

 cleardevice();

    rectangle (clip[0], clip[1], clip[2], clip[3]);
    for (i=0; i < n; i++)
 line (ln[i][0], ln[i][1], ln[i][2], ln[i][3]);
    getch();
    cleardevice();
    rectangle (clip[0], clip[1], clip[2], clip[3]);
    for (i=0; i < n; i++)
    {
 cohen_sutherland (ln[i][0], ln[i][1], ln[i][2], ln[i][3],
     clip[0], clip[1], clip[2], clip[3]);
 getch();
    }
    closegraph();
}

OUTPUT

Comments

Popular posts from this blog

First Come First Serve (FCFS) Page replacement algorithm in C Programming

#include #include int fsize; int frm[15]; void display(); void main() { int pg[100],nPage,i,j,pf=0,top=-1,temp,flag=0; clrscr(); printf("\n Enter frame size:"); scanf("%d",&fsize); printf("\n Enter number of pages:"); scanf("%d",&nPage); for(i=0;i OUTPUT Enter frame size:3 Enter number of pages:12 Enter page[1]:1 Enter page[2]:2 Enter page[3]:3 Enter page[4]:4 Enter page[5]:1 Enter page[6]:2 Enter page[7]:5 Enter page[8]:1 Enter page[9]:2 Enter page[10]:3 Enter page[11]:4 Enter page[12]:5 page | Frame content -------------------------------------- 1 | 1 -1 -1 2 | 1 2 -1 3 | 1 2 3 4 | 4 2 3 1 | 4 1 3 2 | 4 1 2 5 | 5 1 2 1 | 5 1 2 2 | 5 1 2 3 | 5 3 2 4 | 5 3 4 5 | 5 3 4 ---------------------------...

Deadlock Prevention using Banker’s Algorithm in C Programming

#include #include void main() { int allocated[15][15],max[15][15],need[15][15],avail[15],tres[15],work[15],flag[15]; int pno,rno,i,j,prc,count,t,total; count=0; clrscr(); printf("\n Enter number of process:"); scanf("%d",&pno); printf("\n Enter number of resources:"); scanf("%d",&rno); for(i=1;i OUTPUT Enter number of process:5 Enter number of resources:3 Enter total numbers of each resources:10 5 7 Enter Max resources for each process: for process 1:7 5 3 for process 2:3 2 2 for process 3:9 0 2 for process 4:2 2 2 for process 5:4 3 3 Enter allocated resources for each process: for process 1:0 1 0 for process 2:3 0 2 for process 3:3 0 2 for process 4:2 1 1 for process 5:0 0 2 available resources: 2 3 0 Allocated matrix Max need 0 1 0| 7 5 3| 7 4 3 3 0 2| 3 2 2| 0 2 0 3 0 2| 9 0 2| 6 0 0 2 1 1| ...

MVT (Multiprogramming Variable Task) in C Programming

#include #include void main() { int i,os_m,nPage,total,pg[25]; clrscr(); printf("\nEnter total memory size:"); scanf("%d",&total); printf("\nEnter memory for OS:"); scanf("%d",&os_m); printf("\nEnter no. of pages:"); scanf("%d",&nPage); for(i=0;i =pg[i]) { printf("\n Allocate page %d",i+1); total=total-pg[i]; } else printf("\n page %d is not allocated due to insufficient memory.",i+1); } printf("\n External Fragmentation is:%d",total); getch(); } OUTPUT Enter total memory size:1024 Enter memory for OS:256 Enter no. of pages:4 Enter size of page[1]:128 Enter size of page[2]:512 Enter size of page[3]:64 Enter size of page[4]:512 Allocate page 1 Allocate page 2 Allocate page 3 page 4 is not allocated due to insufficient memory. External Fragmentation is:64