Friday, 18 August 2017

CG - 8 - Curve Generation using Interpolation and Bezier Curve

Curve Generation: Design any object such as flower, waves using following curves geneartion techniques

2.Curve Generation using Bezier Curve


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

void bezier (int x[4], int y[4])
{
 int gd = DETECT, gm;
 int i;
 double t;

 initgraph (&gd, &gm,"..\\bgi");
 for (t = 0.0; t < 1.0; t += 0.0005)
 {
  double xt =pow(1-t,3)* x[0]+3*t* pow (1-t, 2) * x[1] +
  3 *pow(t, 2) * (1-t) * x[2] +pow(t, 3) * x[3];

  double yt =pow(1-t, 3) * y[0] + 3 * t *pow(1-t, 2) * y[1] +
  3 *pow(t, 2) * (1-t) * y[2] +pow(t, 3) * y[3];

  putpixel (xt, yt, WHITE);
 }

 for(i=0; i<4; i++)
 putpixel (x[i], y[i], YELLOW);

 getch();
 closegraph();
 return;
}

void main()
{
 int x[4], y[4];
 int i;

 printf ("Enter the x- and y-coordinates of the four control points.\n");
 for(i=0; i<4; i++)
 scanf("%d%d", &x[i], &y[i]);
 bezier (x, y);
}

CG - 7 - 3D Projection using General Parallel Projection

3D Projection - Display of 3D object such as Cube using General Parallel Projection
#include<stdio.h>
#include<graphics.h>
#include<math.h>

void input(int x[],int y[],int z[],int no)
{
 int i;
 printf("Enter the co-ordinates\n");
 for(i=0;i<no;i++)
 {
  printf("P[%d]\t X%d:\t",i+1,i+1);
  scanf("%d",&x[i]);
  printf("P[%d]\t Y%d:\t",i+1,i+1);
  scanf("%d",&y[i]);
  printf("P[%d]\t Z%d:\t",i+1,i+1);
  scanf("%d",&z[i]);
 }
}


void printFigure_ortho(int x[],int y[],int z[],int no)
{
 int i;
 if(no==1)
 {
  putpixel(x[0],y[0],YELLOW);
 }
 else
 {
  for(i=0;i<no-1;++i)
  {
     line(x[i],y[i],x[i+1],y[i+1]);
  }

 }
 if(no>2)
 {
  line(x[0],y[0],x[no-1],y[no-1]);
 }
}


void getOblique(int x[],int y[],int z[],int no,float angle1,int xp[],int yp[],int choice)
{
 int i;
 for(i=0;i<no;++i)
 {
  if(choice==1)
  {
   xp[i]=x[i]+z[i]/tan(45)*cos(angle1);
   yp[i]=y[i]+z[i]/tan(45)*sin(angle1);
  }
  if(choice==2)
  {
   xp[i]=x[i]+z[i]/tan(63.4)*cos(angle1);
   yp[i]=y[i]+z[i]/tan(63.4)*sin(angle1);
  }
 }
}

void printFigure(int xp[],int yp[],int no)
{
 int i;
 if(no==1)
 {
  putpixel(xp[0],yp[0],YELLOW);
 }
 else
 {
  for(i=0;i<no-1;++i)
  {
     line(xp[i],yp[i],xp[i+1],yp[i+1]);
  }

 }
 if(no>2)
 {
  line(xp[0],yp[0],xp[no-1],yp[no-1]);
 }
}

int main()
{
 int gd=DETECT,gm;
 int x[10],y[10],z[10],no,xp[10],yp[10],choice;
 float angle1;
 clrscr();
 printf("Enter your choice:\n");
 printf("1.Point\n2.Line\n3.Triangle\n4.Square\n");
 printf("Or enter no of vertices for any polygon:\n");
 scanf("%d",&no);
 printf("Enter the angle PHI:\t");
 scanf("%f",&angle1);

 input(x,y,z,no);

 initgraph(&gd,&gm,"c:\\tc\\bgi");
 printf("\nOrthographics Projectio(WHITE):");

 printFigure_ortho(x,y,z,no);

 printf("Oblique Projection(YELLOW).Enter your choice:");
 printf("\n1.Cavalier Projection\t2.Cabinet Projection");
 scanf("%d",&choice);

 getOblique(x,y,z,no,angle1,xp,yp,choice);

 setcolor(YELLOW);
 printFigure(xp,yp,no);

 getch();
 return 0;
}

CG - 6 - 2D Clipping - Cohen Sutherland and Hogman Sutherland

2D Clipping

1.Line Clipping - Cohen Sutherland Outcode Method
#include<stdio.h>
#include<conio.h>
/*Header file for built-in graphics functions such as plot() and initgraph()*/
#include<graphics.h>
#include<math.h>

#define TRUE 1
#define FALSE 0
typedef unsigned int outcode;
outcode CompOutCode(float x,float y);
enum  {  TOP = 0x1,
BOTTOM = 0x2,
RIGHT = 0x4,
LEFT = 0x8
};
int xmin,xmax,ymin,ymax;

/*This function uses 4 bit codes for both the endpoints to find if the line needs to be clipped and if yes, it finds the intersection point and displays the clipped line */
void clip(float x0,float y0,float x1,float y1)
{
    outcode outcode0,outcode1,outcodeOut;
    int accept = FALSE,done = FALSE;
    outcode0 = CompOutCode(x0,y0);
    outcode1 = CompOutCode(x1,y1);
    do
    {
        if(!(outcode0|outcode1))
        {
            accept = TRUE;
            done = TRUE;
        }
        else if(outcode0 & outcode1)
            done = TRUE;
        else
        {
            float x,y;
            outcodeOut = outcode0?outcode0:outcode1;
            if(outcodeOut & TOP)
            {

                x = x0+(x1-x0)*(ymax-y0)/(y1-y0);
                y = ymax;
            }
            else if(outcodeOut & BOTTOM)
            {
                x = x0+(x1-x0)*(ymin-y0)/(y1-y0);
                y = ymin;
            }
            else if(outcodeOut & RIGHT)
            {
                y = y0+(y1-y0)*(xmax-x0)/(x1-x0);
                x = xmax;
            }
            else
            {
                y = y0+(y1-y0)*(xmin-x0)/(x1-x0);
                x = xmin;
            }
            if(outcodeOut==outcode0)
            {
                x0 = x;
                y0 = y;
                outcode0 = CompOutCode(x0,y0);
            }
            else
            {
                x1 = x;
                y1 = y;
                outcode1 = CompOutCode(x1,y1);
            }
        }
    }while(done==FALSE);
    if(accept)
        line(x0,y0,x1,y1);/*line(x1,y1,x2,y2) draws line on screen with (x1,y1) and (x2,y2) specifying the endpoint of line*/
    /*outtextxy(x,y,"string") prints string on screen starting with the pixel specified by(x,y)*/
    outtextxy(200,20,"LINE AFTER CLIPPING");
    /*rectangle(x1,y1,x2,y2) draws rectangle on screen with (x1,y1) and (x2,y2) specifying coordinates of top-left and bottom-right vertex respectively.*/
    rectangle(xmin,ymin,xmax,ymax);
}

/* This function checks a point(x,y) with four boundaries to see whether it lies outside or inside and returns result in form of a 4 bit code*/
outcode CompOutCode(float x,float y)
{
    outcode code = 0;
    if(y>ymax)
        code|=TOP;
    else if(y<ymin)
        code|=BOTTOM;
    if(x>xmax)
        code|=RIGHT;
    else if(x<xmin)
        code|=LEFT;
    return code;
}

int main( )
{
    int x1,y1,x2,y2;
    int gd = DETECT,gm ;
    initgraph(&gd, &gm, "c:\\tc\\bgi");/*loads the graphic driver and puts system into graphics mode*/

    /*
    syntax for initgraph() is:
    void initgraph(int *graphdriver, int *graphmode, char *pathtodriver);
    *graphdriver is an integer that specifies the graphics driver to be used.DETECT requests auto-detect for the driver.
    *graphmode is an integer that specifies the initial graphics mode.If *graphdriver equals DETECT,*graphmode is set by initgraph to the highest resolution available for the detected driver.
    */

    printf("\t\t\tCOHEN SUTHERLAND LINE CLIPPING ALGORITHM\n\n\tEnter the endpoints of line");
    scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
    printf("\n\tEnter the rectangular coordinates of clipping window");
    scanf("%d %d %d %d",&xmin,&ymin,&xmax,&ymax);
    /*outtextxy(x,y,"string") prints string on screen starting with the pixel specified by(x,y)*/
    outtextxy(200,100,"LINE BEFORE CLIPPING");
    /*line(x1,y1,x2,y2) draws line on screen with (x1,y1) and (x2,y2) specifying the endpoint of line*/
    line(x1,y1,x2,y2);
    /*rectangle(x1,y1,x2,y2) draws rectangle on screen with (x1,y1) and (x2,y2) specifying coordinates of top-left and bottom-right vertex respectively.*/
    rectangle(xmin,ymin,xmax,ymax);
    getch();
    cleardevice();
    clip(x1,y1,x2,y2);
    getch();
    return 0;
}

2.Sutherland Hogman Polygon Clipping

#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <process.h>
#define TRUE 1
#define FALSE 0


typedef unsigned int outcode;
outcode CompOutCode(float x,float y);

enum  
{  
 TOP = 0x1,
 BOTTOM = 0x2,
 RIGHT = 0x4,
 LEFT = 0x8
};

float xmin,xmax,ymin,ymax;

void clip(float x0,float y0,float x1,float y1)
{

 outcode outcode0,outcode1,outcodeOut;
 int accept = FALSE,done = FALSE;
 outcode0 = CompOutCode(x0,y0);
 outcode1 = CompOutCode(x1,y1);
 do
 {
      if(!(outcode0|outcode1))
      {
          accept = TRUE;
          done = TRUE;
      }
      else
       if(outcode0 & outcode1)
           done = TRUE;
       else
       {
           float x,y;
           outcodeOut = outcode0?outcode0:outcode1;
           if(outcodeOut & TOP)
           {
                x = x0+(x1-x0)*(ymax-y0)/(y1-y0);
                y = ymax;
           }
           else if(outcodeOut & BOTTOM)
           {
                x = x0+(x1-x0)*(ymin-y0)/(y1-y0);
                y = ymin;
           }
           else if(outcodeOut & RIGHT)
           {
                y = y0+(y1-y0)*(xmax-x0)/(x1-x0);
                x = xmax;
           }
           else
           {
                y = y0+(y1-y0)*(xmin-x0)/(x1-x0);
                x = xmin;
           }
           if(outcodeOut==outcode0)
           {
                x0 = x;
                y0 = y;
                outcode0 = CompOutCode(x0,y0);
           }
           else
           {
                x1 = x;
                y1 = y;
                outcode1 = CompOutCode(x1,y1);
             }
       }
 }while(done==FALSE);

 if(accept)
      line(x0,y0,x1,y1);

 outtextxy(150,20,"POLYGON AFTER CLIPPING");

 rectangle(xmin,ymin,xmax,ymax);
}

outcode CompOutCode(float x,float y)
{
    outcode code = 0;
    if(y>ymax)
        code|=TOP;
    else if(y<ymin)
            code|=BOTTOM;
    if(x>xmax)
        code|=RIGHT;
    else if(x<xmin)
        code|=LEFT;
    return code;
}

void main( )
{
 float x1,y1,x2,y2;
 
 /* request auto detection */

 int gdriver = DETECT, gmode, n,poly[14],i;
 clrscr( );

 printf("Enter the no of sides of polygon:");
 scanf("%d",&n);
 printf("\nEnter the coordinates of polygon\n");
 for(i=0;i<2*n;i++)
 {
      scanf("%d",&poly[i]);
 }
 poly[2*n]=poly[0];
 poly[2*n+1]=poly[1];
 printf("Enter the rectangular coordinates of clipping window\n");
 scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);

 /* initialize graphics and local variables */
 
 initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

 outtextxy(150,20,"POLYGON BEFORE CLIPPING");
 drawpoly(n+1,poly);
 rectangle(xmin,ymin,xmax,ymax);
 getch( );
 cleardevice( );
 for(i=0;i<n;i++)
  clip(poly[2*i],poly[(2*i)+1],poly[(2*i)+2],poly[(2*i)+3]);
 getch( );
 restorecrtmode( );
}

CG - 5 - 2D Transformation - Reflection about X and Y axis and Arbitrary Axis

2D Transformation - Reflection about X and Y axis and Arbitrary Axis


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int a,a1,b,b1,c,c1,xt,ch;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
a=getmaxx();
a1=getmaxy();
b=a/2;
b1=a1/2;

line(b,0,b,a1);
line(0,b1,a,b1);
line(400,200,600,200);
line(400,200,400,100);
line(400,100,600,200);

printf("1.origin\n");
printf("2.x-axis\n");
printf("3.y-axis\n");
printf("4.exit\n");

do
{
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
      c=400-b;c1=200-b1;
      line(b-c,b1-c1,b-c-200,b1-c1);
      line(b-c,b1-c1,b-c,b1-c1+100);
      line(b-c,b1-c1+100,b-c-200,b1-c1);
      break;
case 2:
      c=400-b;c1=200-b1;
      line(b+c,b1-c1,b+c+200,b1-c1);
      line(b+c,b1-c1,b+c,b1-c1+100);
      line(b+c,b1-c1+100,b+c+200,b1-c1);
      break;


case 3:
      c=400-b;c1=200-b1;
      line(b-c,b1+c1,b-c-200,b1+c1);
      line(b-c,b1+c1,b-c,b1+c1-100);
      line(b-c,b1+c1-100,b-c-200,b1+c1);
      break;
}

}while(ch<4);
getch();
closegraph();
}
Another Program
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void accept(int a[30][3],int n)
{
 int i;
 printf("\nEnter the coordinates of vertices:\n");
 for(i=0;i<n;i++)
 {
  scanf("%d%d",&a[i][0],&a[i][1]);
 }
}

 void mul(int a[30][3],int s[3][3],int m[30][3],int n)
{
 int i,j,k;
 for(i=0;i<n;i++)
 {
  for(j=0;j<3;j++)
  {
   m[i][j]=0;
   for(k=0;k<3;k++)
   m[i][j]+=(a[i][k]*s[k][j]);
  }
       }
}

void display(int a[30][3],int m[30][3],int n)
{
 int i,mx,my;
 cleardevice();
 mx=getmaxx()/2;
 my=getmaxy()/2;
 setcolor(1);
 line(0,my,getmaxx(),my);
 line(mx,0,mx,getmaxy());
 for(i=10;i<=mx;i=i+10)
 {
  putpixel(mx+i,my+1,4);
  putpixel(mx+i,my-1,4);
  putpixel(mx-i,my+1,4);
  putpixel(mx-i,my-1,4);
  putpixel(mx+1,my+i,4);
  putpixel(mx+1,my-i,4);
  putpixel(mx-1,my+i,4);
  putpixel(mx-1,my-i,4);
 }
 for(i=0;i<n-1;i++)
 {
  setcolor(2);
  line(mx+a[i][0],my-a[i][1],mx+a[i+1][0],my-a[i+1][1]);
  setcolor(5);
  line(mx+m[i][0],my-m[i][1],mx+m[i+1][0],my-m[i+1][1]);
 }
  setcolor(2);
  line(mx+a[i][0],my-a[i][1],mx+a[0][0],my-a[0][1]);
  setcolor(5);
  line(mx+m[i][0],my-m[i][1],mx+m[0][0],my-m[0][1]);
}
void main()
{
 int gd=DETECT,gm,n,a[30][3],ch,s[3][3],m[30][3],i,j;
 initgraph(&gd,&gm,"c:\\tc\\bgi");
 printf("\nEnter the edges of polygon\n");
 scanf("%d",&n);
 accept(a,n);
 do
 {
  printf("\nEnter your choice: \n1.Reflection about x-axis \n2.Reflection about y-axis \n3.Reflection about line y=x \n4.Reflection  about y=-x \n5.Reflection about origin\n6.Exit\n");
  scanf("%d",&ch);
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    s[i][j]=0;
  } s[2][2]=1;
  switch(ch)
  {
   case 1: cleardevice();
    s[0][0]=1;
    s[1][1]=-1;
    mul(a,s,m,n);
    display(a,m,n);
   break;
   case 2: cleardevice();
    s[0][0]=-1;
    s[1][1]=1;
    mul(a,s,m,n);
    display(a,m,n);
    break;
   case 3: cleardevice();
    s[0][1]=1;
    s[1][0]=1;
    mul(a,s,m,n);
    display(a,m,n);
   break;
   case 4: cleardevice();
    s[0][1]=-1;
    s[1][0]=-1;
    mul(a,s,m,n);
    display(a,m,n);
   break;
   case 5: cleardevice();
    s[0][0]=-1;
    s[1][1]=-1;
    mul(a,s,m,n);
    display(a,m,n);
   break;
  }
 }while(ch!=6);
 getch();
 closegraph();
}

CG - 4 - 2D Transformation-Translation -Scaling-Rotation

2D Transformation

1. Simple Translation
//2D Translation: 
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
void main()
{
int graphdriver=DETECT,graphmode,errorcode;
int i;
int x2,y2,x1,y1,x,y;
printf("Enter the 2 line end points:");
printf("x1,y1,x2,y2");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");
line(x1,y1,x2,y2);
printf("Enter translation co-ordinates ");
printf("x,y");
scanf("%d%d",&x,&y);
x1=x1+x;
y1=y1+y;
x2=x2+x;
y2=y2+y;
printf("Line after translation");
line(x1,y1,x2,y2);
getch();
closegraph();
}
2D Rotation
2D Rotation:

#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
void main()
{
int graphdriver=DETECT,graphmode,errorcode;
int i;
int x2,y2,x1,y1,x,y,xn,yn;
double r11,r12,r21,r22,th;
clrscr();
printf("Enter the 2 line end points:");
printf("x1,y1,x2,y2");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");
line(x1,y1,x2,y2);
printf("\n\n\n[ Enter the angle");
scanf("%lf",&th);
r11=cos((th*3.1428)/180);
r12=sin((th*3.1428)/180);
r21=(-sin((th*3.1428)/180));
r22=cos((th*3.1428)/180);
//printf("%lf  %lf  %lf  %lf",r11,r12,r21,r22);
xn=((x2*r11)-(y2*r12));
yn=((x2*r12)+(y2*r11));
line(x1,y1,xn,yn);
getch();
closegraph();
}

2D Scaling
//2D Scaling:

#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
void main()
{
int graphdriver=DETECT,graphmode,errorcode;
int i;
int x3,y3,x2,y2,x1,y1,x,y;
printf("Enter the 2 line end points:");
printf("x1,y1,x2,y2,x3,y3");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");
cleardevice();
rectangle(x1,y1,x2,y2);
printf("Enter scaling co-ordinates ");
printf("x,y");
scanf("%d%d",&x,&y);
x1=(x1*x);
y1=(y1*y);
x2=(x2*x);
y2=(y2*y);
printf("Line after scaling");

rectangle(x1,y1,x2,y2);
getch();
closegraph();
}
2. Scaling, Rotation, Translation
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
void rotation()
{
int graphdriver=DETECT,graphmode,errorcode;
int i;
int x2,y2,x1,y1,x,y,xn,yn;
double r11,r12,r21,r22,th;
clrscr();
printf("Enter the 2 line end points:");
printf("x1,y1,x2,y2");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");
line(x1,y1,x2,y2);
printf("\n\n\n[ Enter the angle");
scanf("%lf",&th);
r11=cos((th*3.1428)/180);
r12=sin((th*3.1428)/180);
r21=(-sin((th*3.1428)/180));
r22=cos((th*3.1428)/180);
//printf("%lf  %lf  %lf  %lf",r11,r12,r21,r22);
xn=((x2*r11)-(y2*r12));
yn=((x2*r12)+(y2*r11));
line(x1,y1,xn,yn);
getch();
closegraph();
}
void scalling()
{
int graphdriver=DETECT,graphmode,errorcode;
int i;
int x2,y2,x1,y1,x,y;
printf("Enter the 2 line end points:");
printf("x1,y1,x2,y2");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");
line(x1,y1,x2,y2);
printf("Enter scaling co-ordinates ");
printf("x,y");
scanf("%d%d",&x,&y);
x1=(x1*x);
y1=(y1*y);
x2=(x2*x);
y2=(y2*y);
printf("Line after scaling");
line(x1,y1,x2,y2);
getch();
closegraph();
}
void translation()
{
int graphdriver=DETECT,graphmode,errorcode;
int i;
int x2,y2,x1,y1,x,y;
printf("Enter the 2 line end points:");
printf("x1,y1,x2,y2");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");
line(x1,y1,x2,y2);
printf("Enter translation co-ordinates ");
printf("x,y");
scanf("%d%d",&x,&y);
x1=x1+x;
y1=y1+y;
x2=x2+x;
y2=y2+y;
printf("Line after translation");
line(x1,y1,x2,y2);
getch();
closegraph();
}

void main()
{
 int ch;
 printf("\n1.Rotation\n2.Translation\n3.Scalling\4.Exit\nEnter your choice:");
 scanf("%d",&ch);
 do{
 switch(ch){
  case 1:rotation();
   break;
  case 2:translation();
   break;
  case 3:scalling();
   break;
  case 4:exit(0);
   break;
  }
  }while(ch==4);
}

CG- 3 Polygon filling using Flood Fill and Scan line Algorithm in C

Polygon Filling

1.Non recursive Seed Fill/Flood Fill 

 Flood Filling Algorithm 8 connected
/* FloodFill of Polygon with eight connected region */
#include<stdio.h>
#include<graphics.h>
void main()
{int gd,gm;

/* Initialise graphics mode
---------------------------------- */
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\bgi");

rectangle(50,50,100,100);
flood(55,55,4,15);
getch();
closegraph();
}

flood(seed_x,seed_y,foreground_col,background_col)
{
 if (getpixel(seed_x,seed_y)!= background_col && getpixel(seed_x,seed_y)!= foreground_col)
  {
   putpixel(seed_x,seed_y,foreground_col);
   flood(seed_x+1,seed_y,foreground_col,background_col);
   flood(seed_x-1,seed_y,foreground_col,background_col);
   flood(seed_x,seed_y+1,foreground_col,background_col);
   flood(seed_x,seed_y-1,foreground_col,background_col);
   flood(seed_x+1,seed_y+1,foreground_col,background_col);
   flood(seed_x-1,seed_y-1,foreground_col,background_col);
   flood(seed_x+1,seed_y-1,foreground_col,background_col);
   flood(seed_x-1,seed_y+1,foreground_col,background_col);
  }
return 0;
}

FLOOD FILL USING 4 Connected Region

#include<stdio.h>
#include<graphics.h>
void main()
{
int gd,gm;

/* Initialise graphics mode
---------------------------------- */
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\bgi");

rectangle(50,50,100,100);
flood(55,55,4,15);
getch();
closegraph();
}

flood(seed_x,seed_y,foreground_col,background_col)
{
 if (getpixel(seed_x,seed_y)!= background_col && getpixel(seed_x,seed_y)!= foreground_col)
  {
   putpixel(seed_x,seed_y,foreground_col);                 delay(1);
   flood(seed_x+1,seed_y,foreground_col,background_col);   delay(1);
   flood(seed_x-1,seed_y,foreground_col,background_col);   delay(1);
   flood(seed_x,seed_y+1,foreground_col,background_col);   delay(1);
   flood(seed_x,seed_y-1,foreground_col,background_col);   delay(1);
  }
 return 0;
}
2.Scan Line Algorithm
#include <stdio.h>
#include <conio.h>
#include <graphics.h>

main()
{
 int n,i,j,k,gd,gm,dy,dx;  //variable declaration
 int x,y,temp;
 int a[20][2],xi[20];   //2D arrary declaration for a.. 20 is the max size.
 float slope[20];   //1D array declaration for slope

 clrscr();
 printf("\n\n\tEnter the no. of edges of polygon : ");
 scanf("%d",&n);
 printf("\n\n\tEnter the cordinates of polygon :\n\n\n ");

 for(i=0;i<n;i++)
 {
  printf("\t X=%d Y=%d : ",i,i);   //accepting cordinates
  scanf("%d %d",&a[i][0],&a[i][1]);
 }

 a[n][0]=a[0][0];    //assign a[0][0] value to a[n][0]
 a[n][1]=a[0][1];    //assign a[0][1] value to a[n][1]
 detectgraph(&gd,&gm);
 initgraph(&gd,&gm,"c:\\tc\\bgi");


 /*- draw polygon -*/

 for(i=0;i<n;i++)
 {
  line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]); //draw lines
 }
 getch();

 for(i=0;i<n;i++)
 {
  dy=a[i+1][1]-a[i][1];   // suppose dy=y2-y1
  dx=a[i+1][0]-a[i][0];   //suppose dx=x2-x1

  if(dy==0)
   slope[i]=1.0;
  if(dx==0)
   slope[i]=0.0;
  if((dy!=0)&&(dx!=0))    /*- calculate inverse slope -*/
  {
   slope[i]=(float) dx/dy;  // typecast to float
  }
 }

 for(y=0;y< 480;y++)    // maximum range of y length resolution is 480 
 {
  k=0;
  for(i=0;i<n;i++)
  {
   if( ((a[i][1]<=y)&&(a[i+1][1]>y))||  //check value is greater or less than y
    ((a[i][1]>y)&&(a[i+1][1]<=y)))
   {
    xi[k]=(int)(a[i][0]+slope[i]*(y-a[i][1])); // find x value array using this formula
    k++;
   }
  }

  for(j=0;j<k-1;j++)    /*- Arrange x-intersections in order -*/
   for(i=0;i<k-1;i++)
   {
    if(xi[i]>xi[i+1]) //sort x intersection in xi[k] in order using swapping
    {
     temp=xi[i];
     xi[i]=xi[i+1];
     xi[i+1]=temp;
    }
   }
  setcolor(35);
  for(i=0;i<k;i+=2)   // draw lines to fill polygon
  {
   line(xi[i],y,xi[i+1],y);
   getch();
  }
 }
 return 0;
}

CG -2 Circle Drawing using DDA, Midpoint and Bresenham's Algorithm

Circle Drawing using DDA, Midpoint and Bresenham's Algorithm

DDA Algorithm
/* Refer page 59 from Computer Graphics by A.P. Godse */
#include<stdio.h>
#include<graphics.h>
#include<math.h>
main()
{
float x1,y1,x2,y2,startx,starty,epsilon;
int gd,gm,i,val;
int r;
clrscr();

/* Read two end points of line
---------------------------------- */ 
printf("Enter the radius of a circle :");
scanf("%d",&r);

/* Initialise graphics mode
---------------------------------- */
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");

/* Initialise starting point
-------------------------------- */
x1=r*cos(0);
y1=r*sin(0);
startx = x1;
starty = y1;

/*Calculations for epsilon 
-----------------------------*/
i=0;
do
{
val = pow(2,i);
i++;
}while(val<r);
epsilon = 1/pow(2,i-1);


do
   {
    x2= x1 + y1*epsilon;
    y2 = y1 - epsilon*x2;
    putpixel(200+x2,200+y2,15);

/* Reinitialise the current point 
---------------------------------- */
    x1=x2;
    y1=y2;
    delay(1000);   /* Delay is purposely inserted to see
     observe the line drawing process */
    }
  while( (y1 - starty ) < epsilon || (startx - x1) > epsilon);
getch();
closegraph();

}

Bresenham's Algorithm
# 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,4);
  putpixel(xc-x,yc-y,4);
  putpixel(xc+x,yc+y,4);
  putpixel(xc-x,yc+y,4);
  putpixel(xc+y,yc-x,5);
  putpixel(xc-y,yc-x,5);
  putpixel(xc+y,yc+x,5);
  putpixel(xc-y,yc+x,5);
 }
 getch();
 closegraph();
}

Midpoint Algorithm
# include<stdio.h>
// C program for implementing
// Mid-Point Circle Drawing Algorithm
#include<stdio.h>
 
// Implementing Mid-Point Circle Drawing Algorithm
void midPointCircleDraw(int x_centre, int y_centre, int r)
{
    int x = r, y = 0;
     
    // Printing the initial point on the axes 
    // after translation
    printf("(%d, %d) ", x + x_centre, y + y_centre);
     
    // When radius is zero only a single
    // point will be printed
    if (r > 0)
    {
        printf("(%d, %d) ", x + x_centre, -y + y_centre);
        printf("(%d, %d) ", y + y_centre, x + x_centre);
        printf("(%d, %d)\n", -y + y_centre, x + x_centre);
    }
     
    // Initialising the value of P
    int P = 1 - r;
    while (x > y)
    { 
        y++;
         
        // Mid-point is inside or on the perimeter
        if (P <= 0)
            P = P + 2*y + 1;
             
        // Mid-point is outside the perimeter
        else
        {
            x--;
            P = P + 2*y - 2*x + 1;
        }
         
        // All the perimeter points have already been printed
        if (x < y)
            break;
         
        // Printing the generated point and its reflection
        // in the other octants after translation
        printf("(%d, %d) ", x + x_centre, y + y_centre);
        printf("(%d, %d) ", -x + x_centre, y + y_centre);
        printf("(%d, %d) ", x + x_centre, -y + y_centre);
        printf("(%d, %d)\n", -x + x_centre, -y + y_centre);
         
        // If the generated point is on the line x = y then 
        // the perimeter points have already been printed
        if (x != y)
        {
            printf("(%d, %d) ", y + y_centre, x + x_centre);
            printf("(%d, %d) ", -y + y_centre, x + x_centre);
            printf("(%d, %d) ", y + y_centre, -x + x_centre);
            printf("(%d, %d)\n", -y + y_centre, -x + x_centre);
        }
    } 
}
 
// Driver code
int main()
{
    // To draw a circle of radius 3 centred at (0, 0)
    midPointCircleDraw(0, 0, 3);
    return 0;
}

CG - 1.1- Line Drawing using DDA Algorithm

Line Drawing using DDA Algorithm with patterns such as simple,dash,dash-dot,thick

Algorithm for DDA(Digital Differential Algorithm) 

 Read the lines and points (x1,y1) and (x2,y2) such that they are not equal.(If they are equal then plot those points and exit)

D(x)= | x2-x1 and | D( y)=| y2-y1 | 

Find out the length (if D(x) >= D(y), then length equal to D(x)). 

D(x)=(x2-x1/length),     D(x)=(x2-x1/length) 

x=x1+0.5(sign(D(x))) y=y1+0.5(sign(D(y))

(Here sign function make algorithm work in all quadrants, if it returns -1, 0, 1 depending on whether its argument is less than zero, equal to zero or greater than zero) 

Plot int x,int y; 
 i=1; 
 while(i<=length) 
 { 
          x=x+D(x); y=y+D(y); 
         plot int x,int y;
          i++; 
 }
 STOP.

//program for DDA

#include <stdio .h="">
#include <dos .h="">
#include <graphics .h="">

void lineDDA(int, int, int, int);

void main()
{
 int x1, y1, xn, yn;
 int gd = DETECT, gm;
 initgraph(&gd, &gm, "c:\\tc\\bgi");
 printf("Enter the starting coordinates of line: ");
 scanf("%d %d", &x1, &y1);
 printf("Enter the ending coordinates of line: ");
 scanf("%d %d", &xn, &yn);
 lineDDA(x1, y1, xn, yn);
 getch();
}

void lineDDA(int x1, int y1, int xn, int yn)
{
 int dx, dy, m, i;
 m = (yn-y1)/(xn-x1);
 for (i=x1; i<=xn; i++)
 {
  if (m <= 1)
  {
   dx = 1;
   dy = m * dx;
  }
  else
  {
   dy = 1;
   dx = dy / m;
  }
 x1 = x1 + dx;
 y1 = y1 + dy;
 putpixel(x1, y1, RED);
 delay(20);
 }
}
</graphics></dos></stdio>
Output:

CG - 1.2 -Bresenham Algorithm for Line

Line Drawing using Bresenham Algorithm with patterns such as simple,dash,dash-dot,thick


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

  void main()
  {
    int dx,dy,x,y,p,x1,y1,x2,y2;
    int gd,gm;

    clrscr();

    printf("\n\n\tEnter the co-ordinates of first point : ");
    scanf("%d %d",&x1,&y1);
    printf("\n\n\tEnter the co-ordinates of second point : ");
    scanf("%d %d",&x2,&y2);

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

    p = 2 * (dy) - (dx);

    x = x1;
    y = y1;

    detectgraph(&gd,&gm);
    initgraph(&gd,&gm,"c:\\tc\\bgi");
    putpixel(x,y,WHITE);

    while(x <= x2)
    {
      if(p < 0)
      {
        x=x+1;
        y=y;
        p = p + 2 * (dy);
      }
      else
      {
        x=x+1;
        y=y+1;
        p = p + 2 * (dy - dx);
     }
     putpixel(x,y,WHITE);
   }
   getch();
   closegraph();
}

Output:

How to ping in Packet Tracer

This Video will show you how to ping in Packet Tracer