Sie sind auf Seite 1von 10

Cohen sutherland line clipping

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT, gm;
float i,xmax,ymax,xmin,ymin,x1,y1,x2,y2,m;
float start[4],end[4],code[4];
clrscr();
initgraph(&gd,&gm,"");
printf("\n\tEnter the bottom-left coordinate of viewport: ");
scanf("%f %f",&xmin,&ymin);
printf("\n\tEnter the top-right coordinate of viewport: ");
scanf("%f %f",&xmax,&ymax);
printf("\n\tEnter the coordinates for starting point of line: ");
scanf("%f %f",&x1,&y1);printf("\n\tEnter the coordinates for ending point of line: ");
scanf("%f %f",&x2,&y2);
for(i=0;i<4;i++)
{
start[i]=0;
end[i]=0;
}
m=(y2-y1)/(x2-x1);
if(x1<xmin)

start[0]=1;
if(x1>xmax)
start[1]=1;
if(y1>ymax)
start[2]=1;
if(y1<ymin)
start[3]=1;
if(x2<xmin)
end[0]=1;
if(x2>xmax)
end[1]=1;
if(y2>ymax)
end[2]=1;
if(y2<ymin)
end[3]=1;
for(i=0;i<4;i++)
code[i]=start[i]&&end[i];
if((code[0]==0)&&(code[1]==0)&&(code[2]==0)&&(code[3]==0))
{

if((start[0]==0)&&(start[1]==0)&&(start[2]==0)&&(start[3]==0)&&(end[0]==0)&&(end[1]==0
)&&(end[2]==0)&&(end[3]==0))
{
cleardevice();
printf("\n\t\tThe line is totally visible\n\t\tand not a clipping candidate");

rectangle(xmin,ymin,xmax,ymax);line(x1,y1,x2,y2);
getch();
}
else
{
cleardevice();
printf("\n\t\t\t\tLine is partially visible");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);getch();
if((start[2]==0)&&(start[3]==1)){x1=x1+(yminy1)/m;y1=ymin;}if((end[2]==0)&&(end[3]==1))
{
x2=x2+(ymin-y2)/m;
y2=ymin;}
if((start[2]==1)&&(start[3]==0))
{
x1=x1+(ymax-y1)/m;
y1=ymax;

}
if((end[2]==1)&&(end[3]==0))
{
x2=x2+(ymax-y2)/m;
y2=ymax;
}

if((start[1]==0)&&(start[0]==1))
{
y1=y1+m*(xmin-x1);
x1=xmin;
}
if((end[1]==0)&&(end[0]==1))
{
y2=y2+m*(xmin-x2);
x2=xmin;
}
if((start[1]==1)&&(start[0]==0))
{
y1=y1+m*(xmax-x1);
x1=xmax;
}
if((end[1]==1)&&(end[0]==0))
{
y2=y2+m*(xmax-x2);
x2=xmax;
}
clrscr();
cleardevice();
printf("\n\t\tAfter clippling:");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);

getch();
}
}
else
{
clrscr();
cleardevice();
printf("\nLine is invisible");
rectangle(xmin,ymin,xmax,ymax);
}getch();
closegraph();
}

OUTPUT:
Enter the bottom-left coordinate of viewport: 50 50
Enter the top-right coordinate of viewport:200 200
Enter the coordinates for starting point of line:25 25
Enter the coordinates for ending point of line:220 220

WINDOW TO VIEWPORT MAPPING


AIM:
To write a C program to perform Window to Viewport transformation.
Algorithm
Step1:Draw a world coordinate area selected for display called as window. This window defines
what is to beviewed.

Step 2:Draw an area on a display device to which a window is mapped called as Viewport. The
viewport defineswhere it is to be displayed.
Step 3:Now perform the mapping of a part of a world-coordinate scene to device coordinates
referred as viewingtransformation.
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
main()
{
float sx,sy;
int w1,w2,w3,w4,x1,x2,x3,x4,y1,y2,y3,y4,v1,v2,v3,v4;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:/tc/bgi");
printf("Enter the Co-ordinates x1,y1,x2,y2,x3,y3\n");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
cleardevice();
w1=5;
w2=5;
w3=635;
w4=465;
rectangle(w1,w2,w3,w4);line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
getch();
v1=425;

v2=75;
v3=550;
v4=250;
sx=(float)(v3-v1)/(w3-w1);
sy=(float)(v4-v2)/(w4-w2);
rectangle(v1,v2,v3,v4);
x1=v1+floor(((float)(x1-w1)*sx)+0.5);
x2=v1+floor(((float)(x2-w1)*sx)+0.5);
x3=v1+floor(((float)(x3-w1)*sx)+0.5);
y1=v2+floor(((float)(y1-w2)*sy)+0.5);
y2=v2+floor(((float)(y2-w2)*sy)+0.5);
y3=v2+floor(((float)(y3-w2)*sy)+0.5);
line(x1,y1,x2,y2);line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
return 0;
}
EX NO 5 PROJECTIONS OF 3D IMAGES
AIM
To write a C program to show the perspective projection of a 3D image.
Algorithm
Step1: Get the coordinates to draw the cube.
Step 2: Read the reference point.
Step 3: Read the view plane.

Step 4: For a perspective projection object positions are transformed to the view plane along
lines that converge to apoint called the projection reference point.
Step 5: The projected view of an object is determined by calculating the intersection point of the
projection lineswith the view plane.
Program
#include <stdio.h>
#include <stdlib.h>
#include<graphics.h>
#include<conio.h>
void draw3d(int s,int x[20],int y[20],int d);
void main()
{
int gd=DETECT,gm;
int x[20],y[20],i,s,d;
initgraph(&gd,&gm,"");
printf("Enter the No of sides : ");
scanf("%d",&s);
for(i=0;i<s;i++)
{
printf("(x%d,y%d) :",i,i);
scanf("%d%d",&x[i],&y[i]);
}
printf("Depth :");
scanf("%d",&d);
draw3d(s,x,y,d);
getch();

setcolor(14);
for(i=0;i<s-1;i++)
{
line(x[i]+200,y[i],x[i+1]+200,y[i+1]);
}
line(x[i]+200,y[i],x[0]+200,y[0]);
getch();
//top view
for(i=0;i<s-1;i++)
{
line(x[i],300,x[i+1],300);
line(x[i],300+d*2,x[i+1],300+d*2);
line(x[i],300,x[i],300+d*2);
line(x[i+1],300,x[i+1],300+d*2);
}
getch();//side view
for(i=0;i<s-1;i++)
{
line(10,y[i],10,y[i+1]);
line(10+d*2,y[i],10+d*2,y[i+1]);
line(10,y[i],10+d*2,y[i]);
line(10,y[i+1],10+d*2,y[i+1]);
}
getch();
closegraph();

}
void draw3d(int s,int x[20],int y[20],int d)
{
int i,j,k=0;for(j=0;j<2;j++)
{
for(i=0;i<s-1;i++)
line(x[i]+k,y[i]-k,x[i+1]+k,y[i+1]-k);
line(x[i]+k,y[i]-k,x[0]+k,y[0]-k);
k=d;
}
for(i=0;i<s;i++)line(x[i],y[i],x[i]+d,y[i]-d);
}

Das könnte Ihnen auch gefallen