Anna University Results November - December 2011 available with your GPA only for Credit System

Exam Results 2012

Exam Results 2012 Inspirational Quotes
Showing posts with label Computer Graphics Lab Programs - CS2405. Show all posts
Showing posts with label Computer Graphics Lab Programs - CS2405. Show all posts

Wednesday, August 3, 2011

Implementation of Bresenhams algorithm - Line Updated



 1.a.) Implementation of Bresenhams algorithm - Line Updated


Program :


// This program will work for all input ....  0<m<1 and m>1 
// where 'm' is slope


#include "stdio.h"
#include "conio.h"
#include "math.h"
#include "graphics.h"
main()
{
int gd=DETECT,gm;
int xa,xb,ya,yb;
int dx,dy,x,y,xend,p,dxt,dyt,slope_pos,yend;
initgraph(&gd,&gm,"H:\\TC\\BGI");
printf("Enter the First End points(xa,ya):\n");
scanf("%d%d",&xa,&ya);
printf("Enter the Second End points(xb,yb):\n");
scanf("%d%d",&xb,&yb);


dxt=xb-xa;
dyt=yb-ya;
dx=abs(xb-xa);
dy=abs(yb-ya);


slope_pos=(dxt>0);
if(dyt<0)
slope_pos=!slope_pos;


if(dx>dy)   // slope => 0<m<1
{
p=2*dy-dx;
if(xa>xb)
{
x=xb;
y=yb;
xend=xa;
}
else
{
x=xa;
y=ya;
xend=xb;
}


    while(x<xend)
      {
      x=x+1;
      if(p<0)
      {
      p=p+2*dy;
      }
      else
      {
      if(slope_pos)
      y=y+1;
      else
      y=y-1;
      p=p+2*(dy-dx);
      }
      putpixel(x,y,6);
      }
      }
      else   // slope => m>1
      {
p=2*dx-dy;
if(ya>yb)
{
x=xb;
y=yb;
yend=ya;
}
else
{
x=xa;
y=ya;
yend=yb;
}
    while(y<yend)
      {
      y=y+1;
      if(p<0)
      {
      p=p+2*dx;
      }
      else
      {
      if(slope_pos)
      x=x+1;
      else
      x=x-1;
      p=p+2*(dx-dy);
      }
      putpixel(x,y,6);
      }
      }
    putpixel(xa,ya,4);
    putpixel(xb,yb,4);
   getch();
   return(0);
}


Output :


Enter The Two Left Endpoints(xa,ya):       50    60

Enter The Two Right Endpoints(xb,yb):    150 360



If you find any flaws in this inform to me .....


Saturday, July 9, 2011

Computer Graphics Lab Programs - CS2405

Implementation of Bresenhams algorithm - Ellipse

 1.c.) Implementation of Bresenhams algorithm - Ellipse  

Program : 

#include "stdio.h"
#include "conio.h"
#include "math.h"
#include "graphics.h"
main()
{
                    int gd=DETECT,gm;
                    int xcenter,ycenter,rx,ry;
                    int p,x,y,px,py,rx1,ry1,rx2,ry2;
                    initgraph(&gd,&gm,"h:\\tc\\bgi");
                    printf("Enter The Radius Value:\n");
                    scanf("%d%d",&rx,&ry);
                    printf("Enter The xcenter and ycenter Values:\n");
                    scanf("%d%d",&xcenter,&ycenter);
                    ry1=ry*ry;
                    rx1=rx*rx;
                    ry2=2*ry1;
                    rx2=2*rx1;

/* REGION 1 */

x=0;
                    y=ry;
                    plotpoints(xcenter,ycenter,x,y);
                    p=(ry1-rx1*ry+(0.25*rx1));
                    px=0;
                    py=rx2*y;
                    while(px<py)
                    {
                              x=x+1;
                             px=px+ry2;
                             if(p>=0)
                                       y=y-1;
                                       py=py-rx2;


                               if(p<0)
                                       p=p+ry1+px;
                              else
                                        p=p+ry1+px-py;
                    plotpoints(xcenter,ycenter,x,y);

/* REGION 2*/
                  
p=(ry1*(x+0.5)*(x+0.5)+rx1*(y-1)*(y-1)-rx1*ry1);
                   while(y>0)
                   {
                             y=y-1;
                             py=py-rx2;
                              if(p<=0)
                             {
                                       x=x+1;
                                       px=px+ry2;
                              }
                              if(p>0)
                                       p=p+rx1-py;
                             else
                                      p=p+rx1-py+px;
                     plotpoints(xcenter,ycenter,x,y);
                   }
               }
                    getch();
                    return(0);
            }

int plotpoints(int xcenter,int ycenter,int x,int y)
{
                    putpixel(xcenter+x,ycenter+y,6);
                    putpixel(xcenter-x,ycenter+y,6);
                    putpixel(xcenter+x,ycenter-y,6);
                    putpixel(xcenter-x,ycenter-y,6);
                    return;
}

Output :

Enter The Radius Value(Rx,Ry)           :     10      30
                   
Enter The xcenter and ycenter Values  :     300    150



Implementation of Bresenhams algorithm - Circle

 1.b.) Implementation of Bresenhams algorithm - Circle  

Program : 

#include "stdio.h"

#include "conio.h"
#include "math.h"
#include "graphics.h"
          main()
          {
                    int gd=DETECT,gm;
                    int xcenter,ycenter,radius;
                    int p,x,y;
                    initgraph(&gd,&gm,"h:\\tc\\bgi");
                    x=0;
                    printf("\n Enter The Radius Value: ");
                    scanf("%d",&radius);
                    y=radius;
                    printf("\n Enter The xcenter and ycenter Values: ");
                    scanf("%d%d",&xcenter,&ycenter);
                    plotpoints(xcenter,ycenter,x,y);
                    p=1-radius;
                    while(x<y)
                    {
                             if(p<0)
                                       x=x+1;
                             else
                              {
                                       x=x+1;
                                       y=y-1;
                             }
                             if(p<0)
                                       p=p+2*x+1;
                             else
                                       p=p+2*(x-y)+1;
                     plotpoints(xcenter,ycenter,x,y);
                   }
            getch();
            return(0);
          }


          int plotpoints(int xcenter,int ycenter,int x,int y) 
           {
                    putpixel(xcenter+x,ycenter+y,1);
                    putpixel(xcenter-x,ycenter+y,1);
                    putpixel(xcenter+x,ycenter-y,1);
                    putpixel(xcenter-x,ycenter-y,1);
                    putpixel(xcenter+y,ycenter+x,1);
                    putpixel(xcenter-y,ycenter+x,1);
                    putpixel(xcenter+y,ycenter-x,1);
                    putpixel(xcenter-y,ycenter-x,1);
                    return;
          }

Output :

Enter The Radius Value                       :     80
                   

Enter The xcenter and ycenter Values :      230    260


Implementation of Bresenhams algorithm - Line

 1.a.) Implementation of Bresenhams algorithm - Line  


Program : 


// Constraint : Slope must be 0 < m < 1



#include "stdio.h"

#include "conio.h"
#include "math.h"
#include "graphics.h"
main()
{
                    int gd=DETECT,gm;
                    int xa,xb,ya,yb;
                    int dx,dy,x,y,xend,p;
                    initgraph(&gd,&gm,"h:\\tc\\bgi");
                    printf("\n Enter The Two Left Endpoints(xa,ya): ");
                    scanf("%d%d",&xa,&ya);
                    printf("\n Enter The Two Right Endpoints(xb,yb): ");
                    scanf("%d%d",&xb,&yb);
                    dx=abs(xa-xb);
                    dy=abs(ya-yb);
                    p=2*dy-dx;
                    if(xa>xb)
                    {
                             x=xb;
                             y=yb;
                             xend=xa;
                    }
          else
{       
                             x=xa;
                              y=ya;
                              xend=xb;
}
      putpixel(x,y,6);


    while(x<xend)

      {
      x=x+1;
      if(p<0)
      {
      p=p+2*dy;
      }
      else                
      {
      y=y+1;
      p=p+2*(dy-dx);                
      }
      putpixel(x,y,6);
      }
   getch();
   return(0);
}

Output :


Enter The Two Left Endpoints(xa,ya):       234    124

Enter The Two Right Endpoints(xb,yb):    578    321