0% found this document useful (0 votes)
126 views10 pages

Computer Graphics Assignment - 6: Write A Program To Implement NLN Line Clipping Algorithm

The document describes a C program that implements the NLN line clipping algorithm to clip a line segment against a rectangle. It takes user input for the coordinates of the rectangle and the line segment. Based on which region the starting point of the line falls in, it calls the appropriate clipline function that calculates the clipped coordinates and draws the clipped line segment.

Uploaded by

Anshul Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views10 pages

Computer Graphics Assignment - 6: Write A Program To Implement NLN Line Clipping Algorithm

The document describes a C program that implements the NLN line clipping algorithm to clip a line segment against a rectangle. It takes user input for the coordinates of the rectangle and the line segment. Based on which region the starting point of the line falls in, it calls the appropriate clipline function that calculates the clipped coordinates and draws the clipped line segment.

Uploaded by

Anshul Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Computer Graphics

Assignment - 6
By- Anshul Garg
(LIT2019054)

Ques:- Write a program to implement NLN line clipping algorithm


Code:
# include <stdio.h>
# include <math.h>
# include <graphics.h>
# include <conio.h>

int xx;
int yy;
int xm;
int ym;

void main(){
int x1, y1, x2, y2;
int gdriver = DETECT, gmode, errorcode;
int findRegionP1(int, int);
void clipline1(int, int, int, int);
void clipline2(int, int, int, int);
void clipline3(int, int, int, int);
int ch;
float m;
clrscr();
printf("\nEnter the xx:->");
scanf("%d", &xx);
printf("\nEnter the yy:->");
scanf("%d", &yy);
printf("\nEnter the xm:->");
scanf("%d", &xm);
printf("\nEnter the ym:->");
scanf("%d", &ym);

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


setcolor(15);
rectangle(xx, yy, xm, ym);
printf("Enter the x1:->");
scanf("%d", &x1);
printf("Enter the y1:->");
scanf("%d", &y1);
printf("Enter the x2:->");
scanf("%d", &x2);
printf("Enter the y2:->");
scanf("%d", &y2);
setcolor(12);
getch();
setcolor(3);
line(x1, y1, x2, y2);
getch();
ch=first_end_point_region(x1, y1);
switch(ch){
case 1:
clipline1(x1, y1, x2, y2);
break;
case 2:
clipline2(x1, y1, x2, y2);
break;
case 3:
clipline3(x1, y1, x2, y2);
break;
default:
printf("\nInvalid select of the option: ");
}
getch();
}

int first_end_point_region(int x, int y){


if(x>=xx && x<=xm && y>=yy && y<=ym)
return 1;
else if(x<xx && y>=yy && y<=ym)
return 2;
else if(x<=xx && y<=yy)
return 3;
else return 0;
}

void clipline1(int x1, int y1, int x2, int y2){


int draw=1;
float m, m1, m2, m3, m4;
int nx1, ny1, nx2, ny2;
m=((float)(y2-y1))/(x2-x1);
m1=((float)(yy-y1))/(xx-x1);
m2=((float)(yy-y1))/(xm-x1);
m3=((float)(ym-y1))/(xm-x1);
m4=((float)(ym-y1))/(xx-x1);
nx1=x1;
ny1=y1;

if(((abs(m)>=m1 && x2<x1) || (abs(m)>abs(m2) && x2>x1)) && y1>y2){


if(y2>yy){
nx2=x2;
ny2=y2;
}
else{
ny2=yy;
nx2=x1+(yy-y1)/m;
}
}
else if(m>m2 && m<m3 && x2>=xm){
if(x2<xm){
nx2=x2;
ny2=y2;
}
else{
nx2=xm;
ny2=y1+(xm-x1)*m;
}
}
else if((abs(m)>=m3 && x2>x1) || (abs(m)>abs(m4) && x2<x1)){
if(y2<ym){
nx2=x2;
ny2=y2;
}
else{
ny2=ym;
nx2=x1+(ym-y1)/m;
}
}
else if(m>m4 && m<m1){
if(x2>xx){
nx2=x2;
ny2=y2;
}
else{
nx2=xx;
ny2=y1+(xx-x1)*m;
}
}
else
draw=0;
getch();
cleardevice();
setcolor(18);
rectangle(xx, yy, xm, ym);
if(draw){
setcolor(12);
setcolor(5);
line(nx1, ny1, nx2, ny2);
}
}

void clipline2(int x1, int y1, int x2, int y2){


int draw=1;
float m, m1, m2, m3, m4;
int nx1, ny1, nx2, ny2;

m=((float)(y2-y1))/(x2-x1);
m1=((float)(yy-y1))/(xx-x1);
m2=((float)(yy-y1))/(xm-x1);
m3=((float)(ym-y1))/(xm-x1);
m4=((float)(ym-y1))/(xx-x1);

if(m>m1 && m<m2){


if(y2>yy){
nx1=xx;
ny1=y1+m*(xx-x1);
nx2=x2;
ny2=y2;
}
else{
nx1=xx;
ny1=y1+m*(xx-x1);
ny2=yy;
nx2=x1+(yy-y1)/m;
}
}
else if(m>m2 && m<m3){
if(x2<xm){
nx1=xx;
ny1=y1+m*(xx-x1);
nx2=x2;
ny2=y2;
}
else{
nx1=xx;
ny1=y1+m*(xx-x1);
nx2=xm;
ny2=y1+(xm-x1)*m;
}
}
else if(m>m3 && m<m4){
if(y2<ym){
nx1=xx;
ny1=y1+m*(xx-x1);
nx2=x2;
ny2=y2;
}
else
{
nx1=xx;
ny1=y1+m*(xx-x1);
ny2=ym;
nx2=x1+(ym-y1)/m;
}
}
else
draw=0;
getch();
cleardevice();
setcolor(18);
rectangle(xx, yy, xm, ym);
if(draw)
{
setcolor(12);
setcolor(5);
line(nx1, ny1, nx2, ny2);
}
}
void clipline3(int x1, int y1, int x2, int y2){
int draw=1;
float m, m1, m2, m3, m4, tm1, tm2;
int nx1, ny1, nx2, ny2;
int flag, t;
tm1=((float)(yy-y1))/(xx-x1);
tm2=((float)(ym-yy))/(xm-xx);

m=((float)(y2-y1))/(x2-x1);
m1=((float)(yy-y1))/(xm-x1);
m2=((float)(ym-y1))/(xm-x1);
m3=((float)(yy-y1))/(xx-x1);
m4=((float)(ym-y1))/(xx-x1);

if(tm1<tm2){
flag=2;
t=m2;
m2=m3;
m3=t;
}
else
flag=1;

if(m>m1 && m<m2){


if(x2>xm && y2>yy){
ny1=yy;
nx1=x1+(yy-y1)/m;
nx2=xm;
ny2=y1+m*(xm-x1);
}
else if(y2>yy && x2<xm){
ny1=yy;
nx1=x1+(yy-y1)/m;
ny2=y2;
nx2=x2;
}
}
else if(m>m2 && m<m3){
if(flag==1){
if(y2>=ym){
ny1=yy;
nx1=x1+(yy-y1)/m;
nx2=x1+(ym-y1)/m;
ny2=ym;
}
else if(y2>=yy){
ny1=yy;
nx1=x1+(yy-y1)/m;
nx2=x2;
ny2=y2;
}
}
else{
if(x2>=xm){
nx1=xx;
ny1=y1+m*(xx-x1);
nx2=xm;
ny2=y1+m*(xm-x1);
}
else if(x2>=xx){
nx1=xx;
ny1=y1+m*(xx-x1);
nx2=x2;
ny2=y2;
}
}
}
else if(m>m3 && m<m4){
if(y2>=ym){
nx1=xx;
ny1=y1+m*(xx-x1);
nx2=x1+(ym-y1)/m;
ny2=ym;
}
else if(y2>=yy){
nx1=xx;
ny1=y1+m*(xx-x1);
ny2=y2;
nx2=x2;
}
}
else
draw=0;
getch();
cleardevice();
setcolor(18);
rectangle(xx, yy, xm, ym);
if(draw){
setcolor(12);
setcolor(5);
line(nx1, ny1, nx2, ny2);
}
}

Output:

You might also like