top of page

Computer Graphics Lab

CSE:3016

Faculty In charge : Prof. Rajesh Kanna

Home: Welcome
Home: Blog2
Search

DIGITAL ASSIGNMENT 1 : Liang Barsky Clipping Explained With Cpp Code

Writer's picture: theBloggerstheBloggers

Updated: Apr 2, 2019







#include <conio.h>

#include <iostream>

#include <graphics.h>

using namespace std;

int main(){

int gdriver = DETECT, gmode;

initgraph(&gdriver,&gmode,"C:\TC\BGI");

setcolor(BLUE);

int xl,yl,xh,yh;

std::cout<<"Enter left, top, right, bottom co-ordinates of the window:\n ";

std::cin>>xl>>yl>>xh>>yh;

rectangle(xl,yl,xh,yh);

int x1,y1,x2,y2;

std::cout<<"Enter endpoints of the line(x1,y1,x2,y2): \n";

std::cin>>x1>>y1>>x2>>y2;

line(x1,y1,x2,y2);

//getch();

int p[4],q[4],i,accept=1;

float u[4],umin=0,umax=1;

p[0] = -(x2-x1);

p[1] = (x2-x1);

p[2] = -(y2-y1);

p[3] = (y2-y1);

q[0] = x1-xl;

q[1] = xh-x1;

q[2] = y1-yl;

q[3] = yh-y1;


for(i=0;i<4;i++){

if(p[i]!=0){

u[i] = (float)q[i]/p[i];

if(p[i]<0 && u[i]>umin) //Line is entering and therefore check for umin

umin = u[i];

else if(p[i]>0 && u[i]<umax) //Line is exiting and therefore check for umax

umax = u[i];

}

else if(p[i]==0 && q[i]<0) //Line is invisible as pk=0 & qk<0

accept=0;

}

std::cout<<"After clipping:";

if(accept==1 && umax>umin)

{

int x3,y3,x4,y4;

x3 = x1 + (x2-x1)*umin;

y3 = y1 + (y2-y1)*umin;

x4 = x1 + (x2-x1)*umax;

y4 = y1 + (y2-y1)*umax;

cleardevice();

rectangle(xl,yl,xh,yh);

setcolor(WHITE);

line(x3,y3,x4,y4);

}

getch();

closegraph();

return 0;

}

27 views0 comments

Recent Posts

See All

Comments


Home: Blog Feed

Meet theBloggers !

This Blog was created by students of Computer Graphics and Design Class (CSE3016) with sole purpose of relaying information of the different activities performed throughout the course period.

theBloggers
Ibra Nafis - 17BCE1230

Bhavya S Chauhan - 17BCE1270

Home: About

© Proudly created with Wix.com

bottom of page