Monday, 29 December 2014

TRANSLATION

Program for Translation

#include <stdio.h>
#include <stdlib.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void draw2d(int,int [],int [],int,int);
void main()
{
int gd=DETECT,gm;
int x[20],y[20],tx=0,ty=0,i,fs;
initgraph(&gd,&gm,"c:\\TurboC3\\BGI");
printf("No of sides : ");
scanf("%d",&fs);
printf("Co-ordinates : ");
for(i=0;i<fs;i++)
{
printf("(x%d,y%d)",i,i);
scanf("%d%d",&x[i],&y[i]);
}
draw2d(fs,x,y,tx,ty);
printf("translation (x,y) : ");
scanf("%d%d",&tx,&ty);
draw2d(fs,x,y,tx,ty);
getch();
}
void draw2d(int fs,int x[20],int y[20],int tx,int ty)
{
int i;
for(i=0;i<fs;i++)
{
if(i!=(fs-1))
line(x[i]+tx,y[i]+ty,x[i+1]+tx,y[i+1]+ty);
else
line(x[i]+tx,y[i]+ty,x[0]+tx,y[0]+ty);
}
}
OUTPUT:



SHEARING

Program for Shearing

#include <stdio.h>
#include <stdlib.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void draw2d(int,int [],int [],int,int);
void main()
{
int gd=DETECT,gm;
int x[20],y[20],x1[20],y1[20],tx=0,ty=0,i,fs,a;
initgraph(&gd,&gm,"c:\\TurboC3\\BGI");
printf("No of sides : ");
scanf("%d",&fs);
printf("Co-ordinates : ");
for(i=0;i<fs;i++)
{
printf("(x%d,y%d)",i,i);
scanf("%d%d",&x[i],&y[i]);
}
draw2d(fs,x,y,tx,ty);
printf("translation (x,y) : ");
scanf("%d%d",&tx,&ty);
while(1)
{
clrscr();
cleardevice();
printf("1. X Shear\n\n2. Y Shear\n\n3. Exit\n\n");
draw2d(fs,x,y,0,0);
switch(getche())
{
case '1':
printf("X Shear Value : ");
scanf("%d",&a);
for(i=0;i<fs;i++)
{
x1[i]=x[i]+(a*(y[i]-ty));
y1[i]=y[i];
}
break;
case '2':
printf("Y Shear Value : ");
scanf("%d",&a);
for(i=0;i<fs;i++)
{
x1[i]=y[i];
y1[i]=y[i]+(a*(x[i]-tx));
}
break;
case '3':
closegraph();
exit(0);
}
draw2d(fs,x1,y1,tx,ty);
getch();
}
}
void draw2d(int fs,int x[20],int y[20],int tx,int ty)
{
int i;
for(i=0;i<fs;i++)
{
if(i!=(fs-1))
line(x[i]+tx,y[i]+ty,x[i+1]+tx,y[i+1]+ty);
else
line(x[i]+tx,y[i]+ty,x[0]+tx,y[0]+ty);
}
}
OUTPUT:




SCALING

Program for Scaling

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>

int x1,y1,x2,y2,x3,y3,mx,my;
void draw();
void scale();

void main()
{
   int gd=DETECT,gm;
   int c;
   initgraph(&gd,&gm,"c:\\TurboC3\\BGI");
   printf("Enter the 1st point for the triangle:");
   scanf("%d%d",&x1,&y1);
   printf("Enter the 2nd point for the triangle:");
   scanf("%d%d",&x2,&y2);
   printf("Enter the 3rd point for the triangle:");
   scanf("%d%d",&x3,&y3);
   draw();
   scale();
}

void draw()
{
   line(x1,y1,x2,y2);
   line(x2,y2,x3,y3);
   line(x3,y3,x1,y1);
}
void scale()
{
   int x,y,a1,a2,a3,b1,b2,b3;
   int mx,my;
   printf("Enter the scalling coordinates");
   scanf("%d%d",&x,&y);
   mx=(x1+x2+x3)/3;
   my=(y1+y2+y3)/3;
   cleardevice();
   a1=mx+(x1-mx)*x;
   b1=my+(y1-my)*y;
   a2=mx+(x2-mx)*x;
    b2=my+(y2-my)*y;
    a3=mx+(x3-mx)*x;
   b3=my+(y3-my)*y;
   line(a1,b1,a2,b2);
   line(a2,b2,a3,b3);
   line(a3,b3,a1,b1);
   draw();
   getch();
}
OUTPUT:



ROTATION

Program for Rotation

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>

void TriAngle(int x1,int y1,int x2,int y2,int x3,int y3);
void Rotate(int x1,int y1,int x2,int y2,int x3,int y3);

void main()
{
    int gd=DETECT,gm;
    int x1,y1,x2,y2,x3,y3;
    initgraph(&gd,&gm,"c:\\TurboC3\\BGI");
    printf("Enter the 1st point for the triangle:");
    scanf("%d%d",&x1,&y1);
    printf("Enter the 2nd point for the triangle:");
    scanf("%d%d",&x2,&y2);
    printf("Enter the 3rd point for the triangle:");
    scanf("%d%d",&x3,&y3);
    TriAngle(x1,y1,x2,y2,x3,y3);
    getch();
    cleardevice();
    Rotate(x1,y1,x2,y2,x3,y3);
     setcolor(1);
    TriAngle(x1,y1,x2,y2,x3,y3);
    getch();
}

void TriAngle(int x1,int y1,int x2,int y2,int x3,int y3)
{
   line(x1,y1,x2,y2);
   line(x2,y2,x3,y3);
   line(x3,y3,x1,y1);
}

void Rotate(int x1,int y1,int x2,int y2,int x3,int y3)
{
    int x,y,a1,b1,a2,b2,a3,b3,p=x2,q=y2;
    float Angle;
    printf("Enter the angle for rotation:");
    scanf("%f",&Angle);
    cleardevice();
    Angle=(Angle*3.14)/180;
    a1=p+(x1-p)*cos(Angle)-(y1-q)*sin(Angle);
    b1=q+(x1-p)*sin(Angle)+(y1-q)*cos(Angle);
    a2=p+(x2-p)*cos(Angle)-(y2-q)*sin(Angle);
    b2=q+(x2-p)*sin(Angle)+(y2-q)*cos(Angle);
    a3=p+(x3-p)*cos(Angle)-(y3-q)*sin(Angle);
    b3=q+(x3-p)*sin(Angle)+(y3-q)*cos(Angle);
     printf("Rotate");
    TriAngle(a1,b1,a2,b2,a3,b3);
}
OUTPUT:



REFLECTION

Program For Reflection

#include <stdio.h>
#include <stdlib.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void draw2d(int,int [],int [],int,int);
void main()
{
int gd=DETECT,gm;
int x[20],y[20],x1[20],y1[20],tx=0,ty=0,i,fs;
initgraph(&gd,&gm,"c:\\TurboC3\\BGI");
printf("No of sides : ");
scanf("%d",&fs);
printf("Co-ordinates : ");
for(i=0;i<fs;i++)
{
printf("(x%d,y%d)",i,i);
scanf("%d%d",&x[i],&y[i]);
}
draw2d(fs,x,y,tx,ty);
printf("translation (x,y) : ");
scanf("%d%d",&tx,&ty);
while(1)
{
clrscr();
cleardevice();
printf("1. X Reflection\n\n2. Y Reflection\n\n3. Exit");
draw2d(fs,x,y,0,0);
switch(getche())
{
case '1':
for(i=0;i<fs;i++)
{
x1[i]=(tx+((x[i]-tx)*cos(M_PI))-((y[i]-ty)*sin(M_PI)));
y1[i]=y[i];
}
break;
case '2':
for(i=0;i<fs;i++)
{
x1[i]=x[i];
y1[i]=(tx+((y[i]-ty)*cos(M_PI))+((x[i]-tx)*sin(M_PI)));
}
break;
case '3':
closegraph();
exit(0);
}
draw2d(fs,x1,y1,tx,ty);
getch();
}
}
void draw2d(int fs,int x[20],int y[20],int tx,int ty)
{
int i;
for(i=0;i<fs;i++)
{
if(i!=(fs-1))
line(x[i]+tx,y[i]+ty,x[i+1]+tx,y[i+1]+ty);
else
line(x[i]+tx,y[i]+ty,x[0]+tx,y[0]+ty);
}
}
OUTPUT:



Program to Draw a Circle using Midpoint Algorithm

Program to Draw a Circle using Midpoint Algorithm

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

void circleBres(int xc, int yc, int r);
void drawCircle(int xc, int yc, int x, int y);

void main()
{

int xc, yc, r;
        int gd = DETECT, gm;
clrscr();
initgraph(&gd, &gm, "c:\\TurboC3\\BGI");
printf("Enter center coordinates of circle: ");
scanf("%d %d", &xc, &yc);
printf("Enter radius of circle: ");
scanf("%d", &r);
circleBres(xc, yc, r);
getch();
}

void circleBres(int xc, int yc, int r)
{

int x = 0, y = r;
int d = 1 - r;
while (x < y)
{
drawCircle(xc, yc, x, y);
if (d >= 0)
      {
x++;
y--;
d=d+2*(x-y)+5;
}
  else
    {
x++;
d = d + 2 * x + 3;
     }
        drawCircle(xc, yc, x, y);
delay(50);
}
}

void drawCircle(int xc, int yc, int x, int y)
{

putpixel(xc+x, yc+y, RED);

putpixel(xc-x, yc+y, RED);

putpixel(xc+x, yc-y, RED);

putpixel(xc-x, yc-y, RED);

putpixel(xc+y, yc+x, RED);

putpixel(xc-y, yc+x, RED);

putpixel(xc+y, yc-x, RED);

putpixel(xc-y, yc-x, RED);
}
OUTPUT:



Digital Differential Analyzer (DDA) Algorithm

Program to draw a line using DDA Algorithm

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

int main()
{
   int x1,x2,dx,p,y1,y2,i=0,dy,xmax,ymax,m;
   float xmid,ymid;
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;

   /* initialize graphics mode */
   initgraph(&gdriver, &gmode, "c:\\TurboC3\\BGI");

   /* read result of initialization */
   errorcode = graphresult();

   if (errorcode != grOk)  /* an error occurred */
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);             /* return with error code */
   }
   line(getmaxx()/2,0,getmaxx()/2,getmaxy());
   line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
   xmax= getmaxx()/2;
   ymax= getmaxy()/2;
   printf("mid point %d \t %d", xmax,ymax);
   printf("\n enter the value of x1 and y1:");
   scanf("%d %d",&x1,&y1);
   printf("\n enter the values of x2 and y2:");
   scanf("%d %d",&x2,&y2);
   if (abs(x2-x1)>abs(y2-y1))
m=abs(x2-x1);
   else
m=abs(y2-y1);
   dx=(x2-x1)/m;
   dy=(y2-y1)/m;
   printf("\n value of dx and dy :%d %d",dx,dy);
   putpixel(x1+319.5,-y1+234.5,2);
   for(i=0;i<m;i++)
   {
x1=x1+dx;
y1=y1+dy;
   putpixel(x1+314.5,-y1+239.5,2);
   }
   getch();
   closegraph();
   return 0;
}
OUTPUT: