Pages

Showing posts with label COMPUTER GRAPHICS. Show all posts
Showing posts with label COMPUTER GRAPHICS. Show all posts

Thursday, May 19, 2011

SUTHERLAND HODGEMAN POLYGON CLIPPING

The algorithm begins with an input list of all vertices in the subject polygon. Next, one side of the clip polygon is extended infinitely in both directions, and the path of the subject polygon is traversed. Vertices from the input list are inserted into an output list if they lie on the visible side of the extended clip polygon line, and new vertices are added to the output list where the subject polygon path crosses the extended clip polygon line.

#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <process.h>
#define TRUE 1
#define FALSE 0
typedef unsigned int outcode;
outcode CompOutCode(float x,float y);
enum  {  TOP = 0x1,
BOTTOM = 0x2,
RIGHT = 0x4,
LEFT = 0x8
};
float xmin,xmax,ymin,ymax;
void clip(float x0,float y0,float x1,float y1)
{
outcode outcode0,outcode1,outcodeOut;
int accept = FALSE,done = FALSE;
outcode0 = CompOutCode(x0,y0);
outcode1 = CompOutCode(x1,y1);
do
{
if(!(outcode0|outcode1))
{
accept = TRUE;
done = TRUE;
}
else
if(outcode0 & outcode1)
done = TRUE;
else
{
float x,y;
 
outcodeOut = outcode0?outcode0:outcode1;
if(outcodeOut & TOP)
{
x = x0+(x1-x0)*(ymax-y0)/(y1-y0);
y = ymax;
}
else
if(outcodeOut & BOTTOM)
{
x = x0+(x1-x0)*(ymin-y0)/(y1-y0);
y = ymin;
}
else
if(outcodeOut & RIGHT)
{
y = y0+(y1-y0)*(xmax-x0)/(x1-x0);
x = xmax;
}
else
{
y = y0+(y1-y0)*(xmin-x0)/(x1-x0);
x = xmin;
}
if(outcodeOut==outcode0)
{
x0 = x;
y0 = y;
outcode0 = CompOutCode(x0,y0);
}
else
{
x1 = x;
y1 = y;
outcode1 = CompOutCode(x1,y1);
}
}
}while(done==FALSE);
if(accept)
line(x0,y0,x1,y1);
outtextxy(150,20,"POLYGON AFTER CLIPPING");
 
rectangle(xmin,ymin,xmax,ymax);
}
outcode CompOutCode(float x,float y)
{
outcode code = 0;
if(y>ymax)
code|=TOP;
else
if(y<ymin)
code|=BOTTOM;
if(x>xmax)
code|=RIGHT;
else
if(x<xmin)
code|=LEFT;
return code;
}
void main( )
{
float x1,y1,x2,y2;
/* request auto detection */
int gdriver = DETECT, gmode, n,poly[14],i;
clrscr( );
printf("Enter the no of sides of polygon:");
scanf("%d",&n);
printf("\nEnter the coordinates of polygon\n");
for(i=0;i<2*n;i++)
{
scanf("%d",&poly[i]);
}
poly[2*n]=poly[0];
poly[2*n+1]=poly[1];
printf("Enter the rectangular coordinates of clipping window\n");
scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
 
outtextxy(150,20,"POLYGON BEFORE CLIPPING");
drawpoly(n+1,poly);
rectangle(xmin,ymin,xmax,ymax);
getch( );
cleardevice( );
for(i=0;i<n;i++)
clip(poly[2*i],poly[(2*i)+1],poly[(2*i)+2],poly[(2*i)+3]);
getch( );
restorecrtmode( );
}

/*
OUTPUT:
Enter the no of sides of polygon:5
Enter the coordinates of polygon
50
50
200
100
350
350
80
200
40
80
Enter the rectangular coordinates of clipping window
150
150
300
300*/


Video lecture on Clipping Lines and Polygons...
Click Here to Watch lecture Now..

COHEN SUTHERLAND LINE CLIPPING ALGORITHM

#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
typedef unsigned int outcode;
outcode CompOutCode(float x,float y);
enum  {  TOP = 0x1,
BOTTOM = 0x2,
RIGHT = 0x4,
LEFT = 0x8
};
float xmin,xmax,ymin,ymax;
void clip(float x0,float y0,float x1,float y1)
{
outcode outcode0,outcode1,outcodeOut;
int accept = FALSE,done = FALSE;
outcode0 = CompOutCode(x0,y0);
outcode1 = CompOutCode(x1,y1);
do
{
if(!(outcode0|outcode1))
{
accept = TRUE;
done = TRUE;
}
else
if(outcode0 & outcode1)
done = TRUE;
else
{
float x,y;
outcodeOut = outcode0?outcode0:outcode1;
if(outcodeOut & TOP)
{
 
x = x0+(x1-x0)*(ymax-y0)/(y1-y0);
y = ymax;
}
else
if(outcodeOut & BOTTOM)
{
x = x0+(x1-x0)*(ymin-y0)/(y1-y0);
y = ymin;
}
else
if(outcodeOut & RIGHT)
{
y = y0+(y1-y0)*(xmax-x0)/(x1-x0);
x = xmax;
}
else
{
y = y0+(y1-y0)*(xmin-x0)/(x1-x0);
x = xmin;
}
if(outcodeOut==outcode0)
{
x0 = x;
y0 = y;
outcode0 = CompOutCode(x0,y0);
}
else
{
x1 = x;
y1 = y;
outcode1 = CompOutCode(x1,y1);
}
}
}while(done==FALSE);
if(accept)
line(x0,y0,x1,y1);
outtextxy(200,20,"LINE AFTER CLIPPING");
rectangle(xmin,ymin,xmax,ymax);
}
 
outcode CompOutCode(float x,float y)
{
outcode code = 0;
if(y>ymax)
code|=TOP;
else
if(y<ymin)
code|=BOTTOM;
if(x>xmax)
code|=RIGHT;
else
if(x<xmin)
code|=LEFT;
return code;
}
void main( )
{
float x1,y1,x2,y2;
int gdriver = DETECT, gmode ;
printf("\nEnter the endpoints of line\n");
scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
printf("Enter the rectangular coordinates of clipping window\n");
scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
outtextxy(200,20,"LINE BEFORE CLIPPING");
line(x1,y1,x2,y2);
rectangle(xmin,ymin,xmax,ymax);
getch( );
cleardevice( );
clip(x1,y1,x2,y2);
getch( );
restorecrtmode( );
}


/*                OUTPUT           
Enter the endpoints of line
100
80
470
340
Enter the rectangular coordinates of clipping window
180
150
300
280




*/

Wednesday, May 18, 2011

SHEARING OF LINE

#include <graphics.h>
#include <stdio.h>
#include <conio.h>
void main( )
{
/* request auto detection */
int gdriver = DETECT, gmode;
int x1,y1,x2,y2,a,b;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
printf("Enter the value of line coordinates:");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
printf("Enter the value of x-shearing factor:");
scanf("%d",&a);
printf("Enter the value of y-shearing factor:");
scanf("%d",&b);
cleardevice( );
outtextxy(200,20,"LINE BEFORE SHEARING");
line(x1,y1,x2,y2);
x1 = x1+a*y1;
y1 = b*x1+y1;
x2 = x2+a*y2;
y2 = b*x2+y2;
getch( );
cleardevice( );
outtextxy(200,20,"LINE AFTER SHEARING");
line(x1,y1,x2,y2);
getch( );
closegraph( );
restorecrtmode( );
}
/*   OUTPUT

Enter the value of line coordinates:
20
20
100
100
Enter the value of x-shearing factor:1
Enter the value of y-shearing:1




*/

MIDPOINT ALGORITH FOR DRAWING ELLIPSE

#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void symmetry(int xc,int yc,int x,int y);
void main( )
{
/* request auto detection */
int gdriver = DETECT, gmode ;
int xc,yc,a,b,x,y,fx,fy,p;
int aa = a*a,bb = b*b,aa2 = aa*2,bb2 = bb*2;
clrscr( );
printf("Enter the coordinates of center of ellipse:");
scanf("%d%d",&xc,&yc);
printf("Enter the x-radius and y-radius of ellipse:");
scanf("%d%d",&a,&b);
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
x = 0;  /*starting point*/
y = b;
symmetry(xc,yc,x,y);
fx = 0;  /*initial partial derivatives*/
fy = aa2*y;
p = bb-aa*b+(0.25*aa);  /*compute and round off p1*/
while(fx < fy)  /* |slope|<1 */
{
x++;
fx = fx+bb2;
if(p<0)
p = p+fx+bb;
else
 
{
y--;
fy = fy-aa2;
p = p+fx+bb-fy;
}
symmetry(xc,yc,x,y);
}
p = bb*(x+0.5)*(x+0.5)+aa*(y-1)*(y-1)-aa*bb;
while(y>0)
{
y--;
fy = fy-aa2;
if(p>=0)
p = p-fy+aa;
else
{
x++;
fx = fx+bb2;
p=p+fx-fy+aa;
}
symmetry(xc,yc,x,y);
}
outtextxy(120,20,"ILLUSTRATION OF MIDPOINT ELLIPSE ALGORITHM");
outtextxy(xc-25,yc,"(xc,yc)");
getch( );
closegraph( );
restorecrtmode( );
}
void symmetry(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc-y,WHITE);
}
 / *                                            OUTPUT
Enter the coordinates of center of ellipse:320
240
Enter the x-radius and y-radius of ellipse:100 80











 */

Wednesday, March 23, 2011

RECTANGLE DRAWING ALGORITHM

#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<graphics.h>
void main()
{
int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi ");
errorcode=graphresult();
if(errorcode!=grOk)
{
cout<<"graphics error"<<grapherrormsg(errorcode)<<endl;
cout<<"press any key to start";
getch();
exit(1);
}
int x1,y1,x2,y2;
cout<<"///////////////// RECTANGLE/////////////////";
cout<<"\n enter x - cordinate of upper right side of rectangle";
cin>>x1;
cout<<"\n enter y - cordinate of upper right side of rectangle";
cin>>y1;
cout<<"\n enter x - cordinate of lower left side of rectangle";
cin>>x2;
cout<<"\n enter y - cordinate of lower left side of rectangle";
cin>>y2;
clrscr();
cout<<" DESIRED RECTANGLE";
line(x1,y1,x1,y2);
line(x2,y2,x1,y2);
line(x1,y1,x2,y1);
line(x2,y2,x2,y1);
getch();
closegraph();
}

DRAWING TRIANGLE & ITS TRANSLATION

In this program we will first draw a triangle according to the coordinates given by user and then applying the translation matrix or translation factor we will  draw that triangle with translated coordinates 


#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<graphics.h>
void main()
{
int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi ");
errorcode=graphresult();
if(errorcode!=grOk)
{
cout<<"graphics error"<<grapherrormsg(errorcode)<<endl;
cout<<"press any key to start";
getch();
exit(1);
}
int i,j,k,x1,x2,x3,y1,y2,y3,x,y;
int c[3][3],a[3][3];
cout<<"ENTER COORDINATES OF TRIANGLE";
cout<<"\n 1st coordinate";
cout<<"\n x - coordinate ";
cin>>a[0][0];
cout<<"\n y - coordinate";
cin>>a[1][0];
a[2][0]=1;
cout<<"\n 2nd coordinate";
cout<<"\n x - coordinate";
cin>>a[0][1];
cout<<"\n y - coordinate";
cin>>a[1][1];
a[2][1]=1;
cout<<"\n 3rd coordinate";
cout<<"\n x - coordinate";
cin>>a[0][2];
cout<<"\n y - coordinate";
cin>>a[1][2];
a[2][2]=1;
cout<<"\n\n\n enter the translation coordinate";
cout<<" enter x - coordinate";
cin>>x;
cout<<"enter y - coordinate";
cin>>y;
int t[3][3]={{1,0,x},{0,1,y},{0,0,1}};
for(i=0;i<3;i++)
{
    for(j=0;j<3;j++)
    {
        c[i][j]=0;
        for(k=0;k<3;k++)
        {
            c[i][j]+=t[i][k]*a[k][j];
        }
    }
}
clrscr();
line(a[0][0],a[1][0],a[0][1],a[1][1]);
line(a[0][1],a[1][1],a[0][2],a[1][2]);
line(a[0][0],a[1][0],a[0][2],a[1][2]);
cout<<" BEFORE TRANSFORMATION";
cout<<"\n press any key to get translated triangle";
getch();
clrscr();
line(c[0][0],c[1][0],c[0][1],c[1][1]);
line(c[0][1],c[1][1],c[0][2],c[1][2]);
line(c[0][0],c[1][0],c[0][2],c[1][2]);
cout<<" AFTER TRANSLATION";
getch();
closegraph();
}

Thursday, February 17, 2011

BRESENHAM'S CIRCLE ALGORITHM

This algorithm will dram an arc from 90 degree angle to 45 degree angle.... after that to construct the whole circle we have to take its mirror image in all 8 quardrants..... the other algorithm for drawing a circle is MID POINT ALGORITHM....

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
void main()
{
int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi ");
errorcode=graphresult();
if(errorcode!=grOk)
{
cout<<"graphics error"<<grapherrormsg(errorcode)<<endl; cout<<"press any key to start"; getch(); exit(1); } clrscr(); int r,x,d,y; cout<<"enter the radius of the circle"; cin>>r;
x=0; y=r; d=3-(2*r);
while(x<=y)
{
putpixel(x,y,RED);
if(d<0)
{
d=d+4*x+6;
}
else
{

d=d+4*(x-y)+10;
y--;
}
x++;
}
getch();
closegraph();
}

BRESENHAM'S LINE ALGORITHM

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
void main()
{
int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi ");
errorcode=graphresult();
if(errorcode!=grOk)
{
cout<<"graphics error"<<grapherrormsg(errorcode)<<endl;
cout<<"press any key to start";
getch();
exit(1);
}
clrscr();
int x1,y1,x2,y2,dx,dy,d,loc1,loc2;
cout<<"enter x and y (starting point)";
cin>>x1>>y1;
cout<<"enter x and y (end point)";
cin>>x2>>y2;
dy=y2-y1;
dx=x2-x1;
d=2*dy-dx;
loc1=2*dy;
loc2=2*(dy-dx);
while(x1<=x2)
{
if(d<0)
{
putpixel(x1,y1,BLUE);
d=d+loc1;
}
else
{
putpixel(x1,y1,RED);
d=d+loc2;
y1++;

}
x1++;
}
getch();
closegraph();
}

SIMPLE LINE DRAWING ALGORITHM

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
void main()
{
int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi ");
errorcode=graphresult();
if(errorcode!=grOk)
{
cout<<"graphics error"<<grapherrormsg(errorcode)<<endl;
cout<<"press any key to start";
getch();
exit(1);
}
clrscr();
int x1,y1,x2,y2,x,y; float m,c;
cout<<"enter valve of x and y(starting point)";
cin>>x1>>y1;
cout<<"enter value of x and y (end point)";
cin>>x2>>y2;
x=x1; y=y1;
m=float (y2-y1)/(x2-x1);
c=y1-m*x1;
cout<<"m="<<m;
cout<<"c="<<c;
if(m==0)
{
while(x<=x2)
{putpixel(x,y,RED);
x++;
}
}
if(abs(m)<=1)
{
while(x<=x2)
{
putpixel(x,y,RED);
x++;
y=m*x+c;
}
}
else
{
while (y<=y2)
{
putpixel(x,y,CYAN);
y++;
x=(y-c)/m;
}
}
getch();
closegraph();
}