0% found this document useful (0 votes)
10 views

Practical 12

Uploaded by

Shubham Mulik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Practical 12

Uploaded by

Shubham Mulik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical 12

#include <graphics.h>
#include <conio.h>
#include <stdio.h>

// Define codes for the 9 regions (4 edges + 4 corners + 1 inside)


#define INSIDE 0 // 0000
#define LEFT 1 //0001
#define RIGHT 2 //0010
#define BELOW 4 // 0100
#define ABOVE 8 // 1000

// Function to compute the region code for a point


int computeCode(float x, float y, float x_min, float x_max, float y_min, float y_max)
{
int code = INSIDE:
if (x < x_min) // to the left of clip window
code |= LEFT;
else if (x > x_max) // to the right of clip window
code = RIGHT;
if (y <y_min) // below the clip window
code | BELOW;
else if (y > y_max) // above the clip window
code |= ABOVE;
return code;
}
// Function to perform Cohen-Sutherland line clipping
void cohenSutherlandLineClip(float x0, float yo, float x1, float y1, floatx_min, float x_max,
float y_min, float y_max)
{
int code0 = computeCode(x0, y0, x_min, x_max, y_min, y_max);
int code1 computeCode(x1, y1, x_min, x_max, y_min, y_max);
int accept = 0;

while (1)
{
if ((code0 == 0) && (code1 == 0))
{
// Both points inside the clipping window
accept = 1;
break;
}
else if (code0 & code1)
{
break;
} else {
// Calculate the intersection point
int code_out;
float x, y;
if (code0 != 0)
code_out = code0;
else
code_out code1;
// Find the intersection point
if (code_out & ABOVE)
{ x = x0+ (x1-x0) (y_max-y0)/(y1-y0); y = y_max;
} else if (code_out & BELOW)
{ x = x0+ (x1-x0) (y_miny0)/(y1-y0);
y = y_min; }
else if (code_out & RIGHT)
{ y=y0+(y1-y0) (x_max-x0)/(x1-x0);
x = x_max;
} else if (code_out & LEFT)
{
y=y0+(y1-y0) (x_minx0)/(x1-x0);
x = x_min;
}
// Update the point outside the clipping window
if (code_out == code0) {
x0 = x;
yo = y;
code0 = computeCode(x0, y0, x_min, x_max, y_min, y_max);
} else {
x1 = x;
y1 = y; code1 = computeCode(x1, y1, x_min, x_max, y_min, y_max);
}
}
}
if (accept)
{
// Draw the accepted line
line((int)x0, (int)y0, (int)x1, (int)y1);
}
}

void main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

float x_min = 100, y\ m in = 100 . x_max = 400, y\ m ax = 300 ;


rectangle((int)x_min, (int)y_min, (int)x_max, (int) y_max);
float x0 = 50, y * 0 = 50 , x * 1 = 450 , y * 1 = 350 ;
cohenSutherlandLineClip(x0, y0, x1, y1, x_min, x_max, y_min, y_max);
getch();
closegraph();
}

You might also like