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.

Sunday, November 25, 2012

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                            


                                                                                                                                     
                                                                       
                                                      




DRAW A CIRCLE USING MIDPOINT ALGORITHM


DRAW A CIRCLE USING MIDPOINT ALGORITHM


#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<dos.h>

void main()
{
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;
   int x1,x2,x3,x4,xc,yc,r,y1,y2;
   char msg[15];
   /* 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);
   }
   printf("Enter the value for xc,yc,r:");
   scanf("%d%d%d",&xc,&yc,&r);
   circlemidpoint(xc,yc,r);
   getch();
   closegraph();
}
void circlemidpoint(int xc,int yc,int r)
{
int x=0,y=r,p=1-r;
void circleplotpoints(int,int,int,int);
circleplotpoints(xc,yc,x,y);
while(x<y)
{
x++;
if(p<0)
p+=2*x+1;
else
{
y--;
p+=2*(x-y)+1;
}
circleplotpoints(xc,yc,x,y);
}
}
 void circleplotpoints(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,YELLOW);
putpixel(xc-x,yc+y,YELLOW);
putpixel(xc+x,yc-y,GREEN);
putpixel(xc-x,yc-y,GREEN);
putpixel(xc+y,yc+x,GREEN);
putpixel(xc-y,yc+x,RED);
putpixel(xc+y,yc-x,RED);
putpixel(xc-y,yc-x,RED);
}


































OUTPUT:-



Enter the value for xc, yc, r:
250
150
125


         




DRAW A LINE USING DDA ALGORITHM


DRAW A LINE USING DDA ALGORITHM

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<dos.h>
#define ROUND(a)((int)(a+0.5))
int main(void)
{
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;
   int xa,xb,ya,yb;
  // int y=0,x=0,i;
   char msg[25];
   /* 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);
  }
 printf("Enter the value for x1,y1:\n");
 scanf("%d%d",&xa,&ya);
 printf("Enter the value for x2,y2:\n");
 scanf("%d%d",&xb,&yb);
 linedda(xa,ya,xb,yb);
 getch();
 closegraph();
  return 0;
}


void linedda(int xa,int ya,int xb,int yb)
{
int dx=xb-xa,dy=yb-ya,steps,k;
float xincr,yincr,x=xa,y=ya;
if(abs(dx)>abs(dy))
steps=abs(dy);
else
steps=abs(dy);
xincr=dx/(float)steps;
yincr=dy/(float)steps;
putpixel(ROUND(x),ROUND(y),GREEN);
for(k=0;k<steps;k++)
{
x+=xincr;
y+=yincr;
putpixel(ROUND(x),ROUND(y),GREEN);
}
}







































OUTPUT:-


Enter the value for x1,y1:
100
100
Enter the value for x2,y2:
250
300