This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Showing posts with label computer graphics using c program. Show all posts
Showing posts with label computer graphics using c program. Show all posts

Sunday, November 25, 2012

2 DIMENSIONAL SCALING


2 DIMENSIONAL SCALING

#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
int px[10],py[10],tx[10],ty[10];
void disp1();
void disp2();
void scaling();
int i,n=0;
void main()
{
/*reqest auto detection */
   int gdriver = DETECT, gmode, errorcode;
   /* initialize graphics and local variables */
   initgraph(&gdriver, &gmode, "");

   /* read result of initialization */
   errorcode = graphresult();
   /* an error occurred */
   if (errorcode != grOk)
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);
   }

   setcolor(getmaxcolor());
   printf("\n\n Enter the no of vertics:");
   scanf("%d",&n );
   printf("\n\nEnter the value for coordinate:");
   for(i=0;i<n;i++)
  {
            scanf("%d",&px[i]);
scanf("%d",&py[i]);
  }
  clrscr();
  disp1();
  scaling();
  clrscr();
  disp2();
  getch();
  closegraph();
}
void disp1()
{
for(i=0;i<n-1;i++)
line(px[i],py[i],px[i+1],py[i+1]);
line(px[n-1],py[n-1],px[0],py[0]);
getch();
}
void disp2()
{
for(i=0;i<n-1;i++)
line(tx[i],ty[i],tx[i+1],ty[i+1]);
line(tx[n-1],ty[n-1],tx[0],ty[0]);
getch();
}
void scaling()
{
int sx,sy;
printf("Enter the scaling vector:");
scanf("%d %d",&sx,&sy);
for(i=0;i<n;i++)
            {
                        tx[i]=px[i]*sx;
 ty[i]=py[i]*sy;
 }
 }

























                                    2-DIMENSIONAL SCALING


OUTPUT:


Enter the number of vertices: 3
Enter the x and y coordinates : 100
150
150
150
150
100
Enter scale factors (2 values ) :
2
2





2 DIMENSIONAL ROTATION


2 DIMENSIONAL ROTATION


#include<math.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
float px[10],py[10],tx[10],ty[10];
void disp1();
void disp2();
void translation();
void scaling();
void rotation();
int vertices;
void main()
{
            int gd=DETECT,gm,i;
            initgraph(&gd,&gm,"");
            if(graphresult()!=grOk)
            {
                        printf("Graphics error");
                         getch();
                         return;
            }
            printf("Enter number of vertices : ");
            scanf("%d",&vertices);
            printf("Enter the x and y coordinates : ");
            for(i=0;i<vertices;i++)
            {
                         scanf("%f%f",&px[i],&py[i]);
            }
            disp1();
            rotation();
            disp2();
            getch();
            return;
}
void disp1()
{
            int i;
            for(i=0;i<vertices-1;i++)
            {
                        line(px[i],py[i],px[i+1],py[i+1]);
                        line(px[vertices-1],py[vertices-1],px[0],py[0]);
            }
}

void disp2()
{
            int i;
            for(i=0;i<vertices-1;i++)
            {
                        line(tx[i],ty[i],tx[i+1],ty[i+1]);
                        line(tx[vertices-1],ty[vertices-1],tx[0],ty[0]);
            }
}
void rotation()
{
            float a,rx,ry,dir,s;
            int i;
            printf("Enter the rotation angle : ");
            scanf("%f",&a);
            printf("Enter the rotation point coordinates : ");
            scanf("%f%f",&rx,&ry);
            printf("Enter the direction of rotation 1 for clockwise and  2 for anticlockwise:");
            scanf("%f",&dir);
            if(dir==1)
                        s=1;
            else
                        s=-1;
            for(i=0;i<vertices;i++)
            {
                         tx[i]=(rx + (px[i]-rx)*cos(a) - (py[i]-ry)*sin(a)*s);
                         ty[i]=(ry + (px[i]-rx)*sin(a)*s + (py[i]-ry)*cos(a));
            }
            getch();
}


















2 DIMENSIONAL ROTATION

OUTPUT:-

Enter the number of vertices:3

Enter the coordinate 1: 100 200

Enter the coordinate 2: 200 200

Enter the coordinate 3: 200 200

Enter the rotation angle: 90

Enter the rotation point: 200 200

Enter the direction (1.Clockwise 2.Anti Clockwise) : 1






LINE CLIPPING USING COHEN SUTHERLAND ALGORITHM


LINE CLIPPING USING COHEN SUTHERLAND ALGORITHM

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#define FALSE 0
#define TRUE 1
#define LEFTEDGE 0x1
#define RIGHTEDGE 0x2
#define TOPEDGE 0x8
#define BOTTOMEDGE 0x4
#define INSIDE(a) (!a)
#define REJECT(a,b) (a&b)
#define ACCEPT(a,b) (!(a|b))

unsigned char encode(int x,int y,int minx,int maxx,int miny,int maxy)
{
            unsigned char code=0X00;
            if(x<minx)
                        code=code|LEFTEDGE;
            if(x>maxx)
                        code=code|RIGHTEDGE;
            if(y<miny)
                        code=code|BOTTOMEDGE;
            if(y>maxy)
                        code=code|TOPEDGE;
            return(code);
}
void swappts(int *x1,int *y1,int *x2,int *y2)
{
            int tx,ty;
            tx=*x1;
            ty=*y1;
            *x1=*x2;
            *y1=*y2;
            *x2=tx;
            *y2=ty;
}
void swapcode(unsigned char *c1,unsigned char *c2)
{
            unsigned char temp;
            temp=*c1;
*c1=*c2;
*c2=temp;
}



void clipline(int minx,int maxx,int miny,int maxy,int x1,int y1,int x2,int y2)
{
            unsigned char code1,code2;
            int done=FALSE;
            int draw=FALSE;
            float m;
            while(!done)
            {
                        code1=encode(x1,y1,minx,maxx,miny,maxy);
                        code2=encode(x2,y2,minx,maxx,miny,maxy);
                        if(ACCEPT(code1,code2))
                        {
                                    done=TRUE;
                                    draw=TRUE;
                        }
                        else if(REJECT(code1,code2))
                                    done=TRUE;
                        else
                        {
                                    if(INSIDE(code1))
                                    {
                                                swappts(&x1,&y1,&x2,&y2);
                                                swapcode(&code1,&code2);
                                    }
                                    if(x2!=x1)
                                                m=(y2-y1)/(x2-x1);
                                    if(code1&LEFTEDGE)
                                    {
                                                y1+=(minx-x1)*m;
                                                x1=minx;
                                    }
                                    else if(code1&RIGHTEDGE)
                                    {
                                                y1+=(maxx-x1)*m;
                                                x1=maxx;
                                    }
                                    else if(code1&BOTTOMEDGE)
                                    {
                                                if(x2!=x1)
                                                            x1+=(miny-y1)/m;
                                                y1=miny;
                                    }
                                    else if(code1&TOPEDGE)
                                    {
                                                if(x2!=x1)
                                                x1+=(maxy-y1)/m;
                                                y1=maxy;
                                    }
                        }
            }
            if(draw)
                        line(x1,y1,x2,y2);
}

void main()
{
            int x1,x2,y1,y2,minx,miny,maxx,maxy;
            int gd=DETECT,gm;
            initgraph(&gd,&gm,"");
            printf("Enter the minimum and maximum values of x:");
            scanf("%d%d",&minx,&maxx);
            printf("Enter the minimum and maximum values of y: ");
            scanf("%d%d",&miny,&maxy);
            printf("Enter the first enpoint of line: ");
            scanf("%d%d",&x1,&y1);
            printf("Enter the second endpoint of line: ");
            scanf("%d%d",&x2,&y2);
            printf("BEFORE CLIPPING");
            rectangle(minx,maxy,maxx,miny);
            line(x1,y1,x2,y2);
            getch();
            clrscr();
            printf("AFTER CLIPPING");
            rectangle(minx,maxy,maxx,miny);
            clipline(minx,maxx,miny,maxy,x1,y1,x2,y2);
            getch();
            closegraph();
}



















OUTPUT:-
           
            Enter the minimum and maximum values of x: 300  400

            Enter the minimum and maximum values of y: 300  400

            Enter the first endpoint  of line: 275  300

            Enter the second endpoint of line: 350  420

            BEFORE CLIPPING
           

                                                                               


 AFTER CLIPPING                            


                                                                                                                                     
                                                                       
                                                      

         


LINE CLIPPING USING LIANG BARSKY ALGORITHM


LINE CLIPPING USING LIANG BARSKY ALGORITHM

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#define ROUND(a)((int)(a+0.5))
int cliptest(float p,float q,float *u1,float *u2)
{
            float r;
            int retval=1;
            if(p<0.0)
            {
                        r=q/p;
                        if(r>*u2)
                                    retval=0;
                        if(r>*u1)
                                    *u1=r;
            }
            else if(p>0.0)
            {
                        r=q/p;
                        if(r<*u1)
                                    retval=0;
                        if(r<*u2)
                                    *u2=r;
            }
            else if(q<0.0)
                        retval=0;
            return(retval);
}
void clipline(int minx,int miny,int maxx,int maxy,int x1,int y1,int x2,int y2)
{
            float u1=0.0,u2=1.0,dx=x2-x1,dy;
            if(cliptest(-dx,x1-minx,&u1,&u2))
                        if(cliptest(dx,maxx-x1,&u1,&u2))
                        {
                                    dy=y2-y1;
                                    if(cliptest(-dy,y1-miny,&u1,&u2))
                                                if(cliptest(dy,maxy-y1,&u1,&u2))
                                                {
                                                            if(u2<1.0)
                                                            {
                                                                        x2=x1+u2*dx;
                                                                        y2=y1+u2*dy;
                                                            }
                                                            if(u1>0.0)
                                                            {
                                                                        x1+=u1*dx;
                                                                        y1+=u1*dy;
                                                            }
                                                            line(x1,y1,x2,y2);
                                                }
                        }
}
void main()
{
            int gdriver=DETECT,gmode,x1,y1,x2,y2,minx,miny,maxx,maxy;
            initgraph(&gdriver,&gmode,"");
            clrscr();
            printf("Enter the min & max x values: ");
            scanf("%d %d",&minx,&maxx);
            printf("Enter the min & max y values: ");
            scanf("%d %d",&miny,&maxy);
            printf("Enter the first endpoint: ");
            scanf("%d %d",&x1,&y1);
            printf("Enter the second endpoint: ");
            scanf("%d %d",&x2,&y2);
            clrscr();
            printf("Before Clipping");
            line(x1,y1,x2,y2);
            rectangle(minx,maxy,maxx,miny);
            getch();
            clrscr();
            printf("After Clipping");
            clipline(minx,miny,maxx,maxy,x1,y1,x2,y2);
            rectangle(minx,maxy,maxx,miny);
            getch();
}

















OUTPUT:-

            Enter the min & max x values:: 300  400

            Enter the min & max x values:: 300  400

            Enter the first endpoint  : 275  300

            Enter the second endpoint : 350  420

            Before Clipping
           

                                                                               


 After Clipping