Sunday, November 21, 2010

Compuetr Graphics 2010




Sr.
No.
INDEX
 
  
    NAME OF PROGRAM

DATE

REMARK

1.
Write a program to draw a line using
Slope Intercept method .



 2.

 Write a program to draw a line using   DDA algorithm .




 3.
Write a program to draw a line using  Bresenham’s algorithm .




 4.

 Write a program to draw a circle using mid point algorithm.
 



 5.
Write a program to draw a circle using DDA algorithm .




 6.
Write a program to draw a semi circle using Bresenham’s algorithm .




 7.
Write a program to draw a ellipse .




 8.
Write a program to implement triangle translation .




 9.
Write a program to implement triangle rotation .




 10.
Write a program to implement triangle scaling .





Submitted By                                                                     Faculty’s Signature




                                                                                                                                                Program to Draw a Line using Slope Intercept method.

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

void main ( )
{

float x1,y1,x2,y2,dy,dx,x,y,c,m;
int i,j;
int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
printf("\n enter x1");
scanf("%f",&x1);
printf(" \n enter y1");
scanf("%f",&y1);
printf("\n enter x2");
scanf("%f",&x2);
printf("\n enter y2");
scanf("%f",&y2);
dy=y2-y1;
dx=x2-x1;
m=dy/dx;  //slope
c=y1-(m*x1);
if(x1<x2);
{
i=x1;
j=x2;

}
else
{
i=x2;
j=x1;
for(x=i;x<=j;x++)
{
y=m*x+c;
putpixel(abs(x),abs(y),red);
delay(10);
}
getch();
}







Program to Draw a Line using DDA algorithm.

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

void main() 
{ 
int gd, gm; 
float x, y, dx, dy, len; 
int x1, y1, x2, y2, i; 
detectgraph(&gd, &gm); 
initgraph(&gd, &gm,""); 

printf("\n Enter the coordinates of line : "); 
scanf("%d %d %d %d", &x1, &y1, &x2, &y2); 

dx = abs(x2-x1); 
dy = abs(y2-y1); 

if (dx >= dy) len = dx; 
else len = dy; 

dx = (x2-x1)/len; 
dy = (y2-y1)/len; 

x = x1 + 0.5; 
y = y1 + 0.5; 

i = 1; 
while (i <= len) 
{ 
putpixel(x, y, 6); 
x = x+dx; 
y = y+dy; 
i++; 
} 

getch(); 
} 

  
Program to Draw a Line using Bresenham’s algo.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
      int gd = DETECT, gm;
      int dx, dy, p, end;
      float x1, x2, y1, y2, x, y;
      initgraph(&gd, &gm, "c:\tc\bgi");
      printf("Enter Value of X1: ");
      scanf("%f", &x1);
      printf("Enter Value of Y1: ");
      scanf("%f", &y1);
      printf("Enter Value of X2: ");
      scanf("%f", &x2);
      printf("Enter Value of Y2: ");
      scanf("%f", &y2);
      dx = abs(x1 - x2);
      dy = abs(y1 - y2);
      p = 2 * dy - dx;
      if(x1 > x2)
      {
            x = x2;
            y = y2;
            end = x1;
      }
      else
      {
            x = x1;
            y = y1;
            end = x2;
      }
      putpixel(x, y, 10);
      while(x < end)
      {
            x = x + 1;
            if(p < 0)
            {
                  p = p + 2 * dy;
            }
            else
            {
                  y = y + 1;
                  p = p + 2 * (dy - dx);
            }
            putpixel(x, y, 10);
      }
      getch();
      closegraph();
}

Program to Draw a Circle using
Mid Point (Bresenham’s) algo.

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

void main()
{
int gd=DETECT,gm;
int r,x,y,p,xc=320,yc=240;

initgraph(&gd,&gm,"C:\\TC\\BGI");
cleardevice();


printf("Enter the radius ");
scanf("%d",&r);


x=0;
y=r;
putpixel(xc+x,yc-y,1);

p=3-(2*r);

for(x=0;x<=y;x++)
{
if (p<0)
{
y=y;
p=(p+(4*x)+6);
}
else
{
y=y-1;
p=p+((4*(x-y)+10));
}
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,2);
putpixel(xc+x,yc+y,3);
putpixel(xc-x,yc+y,4);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc-x,6);
putpixel(xc+y,yc+x,7);
putpixel(xc-y,yc+x,8);
}
getch();
closegraph();
}
Program to Draw a Semi Circle using Bresenham’s algo.

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

void main()
{
int gd=DETECT,gm;
int r,x,y,p,xc=320,yc=240;

initgraph(&gd,&gm,"C:\\TC\\BGI");
cleardevice();


printf("Enter the radius ");
scanf("%d",&r);


x=0;
y=r;
putpixel(xc+x,yc-y,1);

p=3-(2*r);

for(x=0;x<=y;x++)
{
if (p<0)
{
y=y;
p=(p+(4*x)+6);
}
else
{
y=y-1;

p=p+((4*(x-y)+10));
}

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

}
getch();
closegraph();
}


Program to Draw a Circle using DDA algo.

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

void DD_Line(int x1,int y1,int x2,int y2);

int round (double);

void main( )
{
int driver=9,mode=2;
initgraph(&driver,&mode,"C:\\TC");
outtextxy(250,10,"DD_Line Algo");
DD_Line(50,50,100,50);
getch();
}

 void DD_Line(int x1,int y1,int x2,int y2)
{
float dx,dy,y,m,x;
int xtra1,xtra2,mark=2;
if(x1>x2)
{
xtra1=y1;y1=y2;y2=xtra1;
xtra2=x1;x1=x2;x2=xtra2;
printf("%d %d %d %d",x1,y1,x2,y2);
}
dx=x2-x1;
dy=y2-y1;
if (fabs(dx)>fabs(dy))
{
m=dy/dx;
y=y1;
for(x=x1;x<x2;x++)
{
putpixel(x,round(y),mark);
y=y+m;mark=5;
}
}
else
{
m=dx/dy;
x=x1;mark=3;
if(y1<y2)
{
for(y=y1;y<y2;y++)
{
putpixel(round(x),y,mark);
x=x+m;mark=6;
}
}
else
{
for(y=y1;y>y2;y--)
{
putpixel(round(x),y,mark);
x=x-m;mark=6;
}
}
}
}

int round(double y)
{
double fraction,integer;
fraction=modf(y,&integer);
if(fraction>=0.5)
return ((int)integer+1);
else
return ((int)integer);
}


Program to Draw a Ellipse using Bresenham’s algo.

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void putelpix(float xc,float yc,float x,float y)
{
        putpixel((int)(xc+x),(int)(yc+y),15);
                putpixel((int)(xc-x),(int)(yc-y),15);
                putpixel((int)(xc+x),(int)(yc-y),15);
                putpixel((int)(xc-x),(int)(yc+y),15);
}
void mdpt(float xc,float yc,float rx,float ry)
{
                float x=0,y=ry,p1,p2,t1,t2;
                int k;
                p1=ry*ry-rx*rx*ry+0.25*rx*rx;
        putelpix(xc,yc,x,y);
                for(k=0;(2*ry*ry*x)<=(2*rx*rx*y);k++)
                {
                                t1=2*ry*ry*x+2*ry*ry;
                                t2=2*rx*rx*y-2*rx*rx;
                                if(p1<0)
                                p1=p1+t1+ry*ry;
                                else
                                {
                                                p1=p1+t1-t2+ry*ry;
                                                y--;
                                }
                                x++;
                                putelpix(xc,yc,x,y);

                }
                p2=ry*ry*(x+.5)*(x+.5)+rx*rx*(y-1)*(y-1)-rx*rx*ry*ry;
                putelpix(xc,yc,x,y);
                for(k=0;y>=0;k++)
                {
                                t1=2*ry*ry*x+2*ry*ry;
                                t2=2*rx*rx*y-2*rx*rx;
                                if(p2>0)
                                p2=p2-t2+rx*rx;
                                else
                                {
                                                p2=p2+t1-t2+rx*rx;
                                                x++;
                                }
                                y--;
                                putelpix(xc,yc,x,y);

                }
}
void main()
{
                int gd=DETECT,gm;
                float xc,yc,rx,ry;
                initgraph(&gd,&gm,"");
                printf("Enter center coordinates x,y and rx,ry
");
                scanf("%f%f%f%f",&xc,&yc,&rx,&ry);
                mdpt(xc,yc,rx,ry);
                getch();
                closegraph();
}



Program to Translate a Triangle.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gdriver=DETECT,gmode;
int x1,y1,x2,y2,x3,y3,tx,ty;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");

printf("\n enter the  1st end point co-ordinate of triangle:");
scanf("%d%d",&x1,&y1,);

printf("\n enter the  2nd end point co-ordinate of triangle:");
scanf("%d%d",&x2,&y2);

printf("\n enter the  3rd end point co-ordinate of triangle:");
scanf("%d%d",&x3,&y3);


printf("\n the given triangle is:" )
drawline(x1,y1,x2,y2);//function of drawing line using bresenhem algo
drawline(x2,y2,x3,y3);
drawline(x1,y1,x3,y3);

printf("\n enter translation parameter:");
scanf("%d%d",&tx,&ty);

printf("\n the translated triangle is:")


drawline(x1+tx,y1+ty,x2+tx,y2+ty);
drawline(x2+tx,y2+ty,x3+tx,y3+ty);
drawline(x1+tx,y1+ty,x3+tx,y3+ty);

getch();
closegraph();                    
}


Program to Rotate a Triangle.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gdriver=DETECT,gmode;
int x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,choice;
int x,y;
float a;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
printf("enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("enter the co-ordinates:");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
line(x1,y1,x2,y2);\\line drawing function
line(x2,y2,x3,y3);
line(x1,y1,x3,y3);
printf("enter the angle:" );
scanf("%f",&a);
a=a*(3.14/180);
printf("Enter the coordinates of triangle for rotation");
scanf("%d%d",&x,&y);
if((x==x1)&&(y==y1))
{
x5=x2*cos(a)-y2*sin(a);
y5=x2*sin(a)+y2*cos(a);
x6= x3*cos(a)-y3*sin(a);
y6=x3*sin(a)+y3*cos(a);
line(x,y,x5,y5);
line(x5,y5,x3,y3);
line(x3,y3,x,y);
}
if((x==x2)&&(y==y2))
{
x5=x1*cos(a)-y1*sin(a);
y5=x1*sin(a)+y1*cos(a);
x6= x3*cos(a)-y3*sin(a);
y6=x3*sin(a)+y3*cos(a);
line(x5,y5,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x5,y5);
}
if((x==x3)&&(y==y3))
{
x5=x1*cos(a)-y1*sin(a);
y5=x1*sin(a)+y1*cos(a);
x6= x2*cos(a)-y2*sin(a);
y6=x2*sin(a)+y2*cos(a);
line(x5,y5,x6,y6);
line(x6,y6,x3,y3);
line(x3,y3,x5,y5);
}
break;
case 2:
printf("enter the co-ordinates:");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
line(x1,y1,x1,y2);
line(x1,y2,x3,y2);
line(x3,y2,x3,y1);
line(x3,y1,x1,y1);
printf("enter the angle:");
scanf("%f",&a);
a=a*(3.14/180);
printf("enter the co-ordinates about which roation is performed:");
scanf("%d%d",&x,&y);
if((x==x1)&&(y==y1))
{
x5=x1*cos(a)-y2*sin(a);
y5=x1*sin(a)+y2*cos(a);
x6=x3*cos(a)-y2*sin(a);
y6=x3*sin(a)+y2*cos(a);
x7=x3*cos(a)-y1*sin(a);
y7=x3*sin(a)+y1*cos(a);
line(x,y,x5,y5);
line(x5,y5,x6,y6);
line(x6,y6,x7,y7);
line(x7,y7,x,y);
}
if((x==x1)&&(y==y2))
{
x5=x1*cos(a)-y1*sin(a);
y5=x1*sin(a)+y1*cos(a);
x6=x3*cos(a)-y2*sin(a);
y6=x3*sin(a)+y2*cos(a);
x7=x3*cos(a)-y1*sin(a);
y7=x3*sin(a)+y1*cos(a);
line(x5,y5,x,y);
line(x,y,x6,y6);
line(x6,y6,x7,y7);
line(x7,y7,x5,y5);
}
if((x==x3)&&(y==y2))
{
x5=x1*cos(a)-y1*sin(a);
y5=x1*sin(a)+y1*cos(a);
x6=x1*cos(a)-y2*sin(a);
y6=x1*sin(a)+y2*cos(a);
x7=x3*cos(a)-y1*sin(a);
y7=x3*sin(a)+y1*cos(a);
line(x5,y5,x6,y6);
line(x6,y6,x,y);
line(x,y,x7,y7);
line(x7,y7,x5,y5);
}
if((x==x3)&&(y==y1))
{
x5=x1*cos(a)-y1*sin(a);
y5=x1*sin(a)+y1*cos(a);
x6=x1*cos(a)-y2*sin(a);
y6=x1*sin(a)+y2*cos(a);
x7=x3*cos(a)-y2*sin(a);
y7=x3*sin(a)+y2*cos(a);
line(x5,y5,x6,y6);
line(x6,y6,x7,y7);
line(x7,y7,x,y);
line(x,y,x5,y5);
}
break;
default:
printf("wrong choice:\n");
}
getch();
closegraph();
}





Program to Scale a Triangle.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gdriver=DETECT,gmode;
int x1,y1,x2,y2,x3,y3,sx,sy;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");

printf("\n enter the  1st end point co-ordinate of triangle:");
scanf("%d%d",&x1,&y1,);

printf("\n enter the  2nd end point co-ordinate of triangle:");
scanf("%d%d",&x2,&y2);

printf("\n enter the  3rd end point co-ordinate of triangle:");
scanf("%d%d",&x3,&y3);


printf("\n the given triangle is:" )
drawline(x1,y1,x2,y2);//function of drawing line using bresenhem algo
drawline(x2,y2,x3,y3);
drawline(x1,y1,x3,y3);

printf("enter scaling parameter:");
scanf("%d%d",&sx,&sy);

printf("\n the scaled triangle:");

drawline(x1*sx,y1*sy,x2*sx,y2*sy);
drawline(x2*sx,y2*sy,x3*sx,y3*sy);
drawline(x1*sx,y1*sy,x3*sx,y3*sy);


getch();
closegraph();
}