Friday, 27 February 2015

Sutherland Hodgman Polygon Clipping

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
accept_poly(int p[10][2])
{
int i,v;
 printf("\nEnter no. of vertices in a polygon:");
 scanf("%d",&v);
 for(i=0;i<v;i++)
 {
   printf("\n Enter the %d co-ordinate(x%d,y%d): -->",i+1,i+1,i+1);
   scanf("%d%d",&p[i][0],&p[i][1]);
 }
 return(v);
}

void draw_poly(int p[10][2],int v)
{
int i;
setcolor(BLUE);
for(i=0;i<v;i++)
 {
   line(p[i][0],p[i][1],p[(i+1)%v][0],p[(i+1)%v][1]);
  }
}

int leftclip(int p[10][2],int v,int wxmin)
{
int i,t[10][2];
int k=0;
int x1,y1,x2,y2;
for(i=0;i<v;i++)
{
if(p[i][0] >= wxmin)
{
t[k][0]=p[i][0];
t[k][1]=p[i][1];
k++;
}
if((p[i][0] > wxmin && p[(i+1)%v][0] < wxmin) || (p[i][0] < wxmin && p[(i+1)%v][0] > wxmin))
{
x1=p[i][0];
y1=p[i][1];
x2=p[(i+1)%v][0];
y2=p[(i+1)%v][1];
t[k][1] = y1 + (wxmin-x1) * ((float)(y2-y1)/(x2-x1));
t[k][0] = wxmin;
k++;
}
}
for(i=0;i<k;i++)
{
p[i][0] = t[i][0];
p[i][1] = t[i][1];
}
return(k);
}

int rightclip(int p[10][2],int v,int wxmax)
{
int i,t[10][2];
int k=0;
int x1,y1,x2,y2;
for(i=0;i<v;i++)
{
if(p[i][0] <= wxmax)
{
t[k][0]=p[i][0];
t[k][1]=p[i][1];
k++;
}
if((p[i][0] > wxmax && p[(i+1)%v][0] < wxmax) || (p[i][0] < wxmax && p[(i+1)%v][0] > wxmax))
{
x1=p[i][0];
y1=p[i][1];
x2=p[(i+1)%v][0];
y2=p[(i+1)%v][1];
t[k][1] = y1 + (wxmax-x1) * ((float)(y2-y1)/(x2-x1));
t[k][0] = wxmax;
k++;
}
}
for(i=0;i<k;i++)
{
p[i][0] = t[i][0];
p[i][1] = t[i][1];
}
return(k);
}

int topclip(int p[10][2],int v,int wymin)
{
int i,t[10][2];
int k=0;
int x1,y1,x2,y2;
for(i=0;i<v;i++)
{
if(p[i][1] >= wymin)
{
t[k][0]=p[i][0];
t[k][1]=p[i][1];
k++;
}
if((p[i][1] > wymin && p[(i+1)%v][1] < wymin) || (p[i][1] < wymin && p[(i+1)%v][1] > wymin))
{
x1=p[i][0];
y1=p[i][1];
x2=p[(i+1)%v][0];
y2=p[(i+1)%v][1];
t[k][0] = x1 + (wymin-y1) * ((float)(x2-x1)/(y2-y1));
t[k][1] = wymin;
k++;
}
}
for(i=0;i<k;i++)
{
p[i][0] = t[i][0];
p[i][1] = t[i][1];
}
return(k);
}

int bottomclip(int p[10][2],int v,int wymax)
{
int i,t[10][2];
int k=0;
int x1,y1,x2,y2;
for(i=0;i<v;i++)
{
if(p[i][1] <= wymax)
{
t[k][0]=p[i][0];
t[k][1]=p[i][1];
k++;
}
if((p[i][1] > wymax && p[(i+1)%v][1] < wymax) || (p[i][1] < wymax && p[(i+1)%v][1] > wymax))
{
x1=p[i][0];
y1=p[i][1];
x2=p[(i+1)%v][0];
y2=p[(i+1)%v][1];
t[k][0] = x1 + (wymax-y1) * ((float)(x2-x1)/(y2-y1));
t[k][1] = wymax;
k++;
}
}
for(i=0;i<k;i++)
{
p[i][0] = t[i][0];
p[i][1] = t[i][1];
}
return(k);
}

int main(void)
{
   int gd = DETECT, gm, errorcode;
   int v,wxmin,wymin,wxmax,wymax,p[10][2];
   printf("\nEnter the window co-ordinates:");
   printf("\nWxmin ==> ");
  scanf("%d",&wxmin);
   printf("\nWymin ==> ");
   scanf("%d",&wymin);
   printf("\nWxmax ==> ");
   scanf("%d",&wxmax);
   printf("\nWymax ==> ");
   scanf("%d",&wymax);
   v=accept_poly(p);
   initgraph(&gd, &gm, NULL);
   setcolor(RED);
   rectangle(wxmin,wymin,wxmax,wymax);
   draw_poly(p,v);
   getch();
   v=leftclip(p,v,wxmin);
   cleardevice();
   rectangle(wxmin,wymin,wxmax,wymax);
   draw_poly(p,v);
   getch();
   v=rightclip(p,v,wxmax);
   cleardevice();
   rectangle(wxmin,wymin,wxmax,wymax);
   draw_poly(p,v);
   getch();
   v=topclip(p,v,wymin);
   cleardevice();
   rectangle(wxmin,wymin,wxmax,wymax);
   draw_poly(p,v);
   getch();
   v=bottomclip(p,v,wymax);
   cleardevice();
   rectangle(wxmin,wymin,wxmax,wymax);
   draw_poly(p,v);
   getch();
   closegraph();
   return 0;
}

Thursday, 26 February 2015

Reflection about arbitary Line y=mx+c

#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <math.h>
int n;

void reset (int h[][2])
{
    int i,j,val[50][2];
    printf("\n enter no of sides");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    for(j=0;j<2;j++)
    {
    scanf("%d",&val[i][j]);
    }
    }
    for (i=0; i<n; i++)
    {
    h[i][0] = val[i][0];
    h[i][1] = val[i][1];
    }
}

void draw (int h[][2])
{
    int i;
    setlinestyle (SOLID_LINE, 0, 1);
    for (i=0; i<n-1; i++)
    line (320+h[i][0], 240-h[i][1], 320+h[i+1][0], 240-h[i+1][1]);
    line (320+h[0][0], 240-h[0][1], 320+h[n-1][0], 240-h[n-1][1]);
}

void rotate (int h[][2], float angle)
{
    int i;
    for (i=0; i<n; i++)
    {
    int xnew, ynew;
    xnew = h[i][0] * cos (angle) - h[i][1] * sin (angle);
    ynew = h[i][0] * sin (angle) + h[i][1] * cos (angle);
    h[i][0] = xnew; h[i][1] = ynew;
    }
}

void reflect (int h[][2], int m, int c)
{
    int i;
    float angle;
    for (i=0; i<n; i++)
        h[i][1] -= c;
    angle = M_PI/2 - atan (m);
    rotate (h, angle);
    for (i=0; i<n; i++)
        h[i][0] = -h[i][0];
    angle = -angle;
    rotate (h, angle);
    for (i=0; i<n; i++)
        h[i][1] += c;
}

void ini()
{
    int gd=DETECT,gm;
    initgraph(&gd,&gm,NULL);
}

void dini()
{
    getch();
    closegraph();
}

void main()
{
    int h[50][2],c;
    float m;
    printf ("Enter the values of m and c: ");
    scanf ("%f%d", &m, &c);
    reset (h);               
    ini();
    setlinestyle (DOTTED_LINE, 0, 1);
        line (320, 0, 320, 480);
        line (0, 240, 640, 240);   
    draw (h);
    getch();
    setcolor(RED);
    reflect (h, m, c);
    draw (h);
    dini();
}

Scaling about arbitrary point

#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <math.h>
int n;

void reset (int h[][2])
{
    int i,j,val[50][2];
    printf("\n enter no of sides");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    for(j=0;j<2;j++)
    {
    scanf("%d",&val[i][j]);
    }
    }
    for (i=0; i<n; i++)
    {
    h[i][0] = val[i][0];
    h[i][1] = val[i][1];
    }
}

void draw (int h[][2])
{
    int i;
    setlinestyle (DOTTED_LINE, 0, 1);
    line (320, 0, 320, 480);
    line (0, 240, 640, 240);
    setlinestyle (SOLID_LINE, 0, 1);
    for (i=0; i<n-1; i++)
    line (320+h[i][0], 240-h[i][1], 320+h[i+1][0], 240-h[i+1][1]);
    line (320+h[0][0], 240-h[0][1], 320+h[n-1][0], 240-h[n-1][1]);
}

void scale (int h[][2], int sx, int sy)
{
    int i;
    for (i=0; i<n; i++)
    {
    h[i][0] *= sx;
    h[i][1] *= sy;
    }
}
void translate (int h[][2], int dx, int dy)
{
    int i;
    for (i=0; i<n; i++)
    {
    h[i][0] += dx;
    h[i][1] += dy;
    }
}
void ini()
{
    int gd=DETECT,gm;
    initgraph(&gd,&gm,NULL);
}

void dini()
{
    getch();
    closegraph();
}

void main()
{
    int h[50][2],sx,sy,x,y;
    printf ("Enter the x- and y-scaling factors: ");
    scanf ("%d%d", &sx, &sy);
    printf ("Enter the x- and y-coordinates of the point: ");
    scanf ("%d%d", &x, &y);
    reset (h);               
    ini();
    putpixel(x,y,15);
    translate (h, x, y);// Go to arbitrary point
    draw(h); getch();//Show its arbitrary position
    cleardevice();
    translate(h,-x,-y);//Take it back to origin
    draw(h);
    getch();
    cleardevice();
    scale (h, sx, sy);//Now Scale it
    draw(h);
    getch();
    translate (h, x, y);//Back to Arbitrary point
    cleardevice();
    draw (h);
    putpixel (320+x, 240-y, WHITE);
    dini();
}

Wednesday, 25 February 2015

SEED FILL Algorithm

#include<stdio.h>
#include<graphics.h>
void right_fill(int x, int y);
void left_fill(int x, int y);
int main()
{
 int gd=DETECT, gm,op,i,x,y,n,a[10][3];
   printf("\nEnter no. of vertices : ");
 scanf("%d", &n);
 for(i=0;i<n;i++)
    {
     printf("\nEnter the co-ordinates : ");
     scanf("%d%d", &a[i][0], &a[i][1]);
    }
    printf("\nEnter seeding point : ");
    scanf("%d%d", &x, &y);
    initgraph(&gd, &gm, NULL);
    for(i=0;i<n;i++)
    {
     line(a[i][0], a[i][1], a[(i+1)%n][0], a[(i+1)%n][1]);
    }
    right_fill(x,y);
    left_fill(x-1,y);
    getch();
}
 void right_fill(int x, int y)
 {
  while((getpixel(x,y)!=WHITE) && (getpixel(x,y)!=RED))
  {
   delay(1);
   putpixel(x,y,RED);
   right_fill(++x,y);
   x=x-1;
   right_fill(x,y-1);
   right_fill(x,y+1);
  }
 }
 void left_fill(int x, int y)
 {
  while((getpixel(x,y)!=WHITE) && (getpixel(x,y)!=RED))
  {
   delay(1);
   putpixel(x,y,RED);
   left_fill(--x,y);
   x=x+1;
   left_fill(x,y-1);
   left_fill(x,y+1);
  }
 }

Scan Line Algorithm

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

int main()
{
 int gd=DETECT, gm,op,n,a[10][3];
 int  cnt,temp,i,j,k;
 float ymax,ymin,inter_x[10],yscan;
 int x[10],y[10];
 float m[10],dx,dy;
 printf("Enter no of vertices");
 scanf("%d",&n);
 for(i=0;i<n;i++)
{
  printf("verticesof polygon is ");
  scanf("%d%d",&x[i],&y[i]);
}
x[n]=x[0];
y[n]=y[0];
initgraph(&gd,&gm,NULL);
setcolor(3);
for(i=0;i<n;i++)
{
line(x[i],y[i],x[i+1],y[i+1]);
}
ymax=0;
ymin=480;
 for(i=0;i<n;i++)
 {
   if(y[i]>ymax)
    ymax=y[i];
    if(y[i]<ymin)
    ymin=y[i];
 }
 for(i=0;i<n;i++)
 {
  dx=x[i+1]-x[i];
  dy=y[i+1]-y[i];
   if(dx==0)
   m[i]=0;
   else if(dy==0)
   m[i]=0;
   else
   m[i]=(float)dy/dx;
 }
 for(yscan=ymax;yscan>ymin;yscan--)
 {
    cnt=0;
    for(i=0;i<n;i++)
    {
     if(y[i]>yscan && y[i+1]<=yscan || y[i]<=yscan && y[i+1]>yscan)
     {
       if(m[i]==0)
          inter_x[cnt]=x[i];
       else
           inter_x[cnt]=x[i]+(yscan-y[i])/m[i];
       cnt++;
    }
   }
      for(i=0;i<cnt-1;i++)
      {
      for(j=0;j<cnt-1;j++)
      {
    if(inter_x[j]>inter_x[j+1])
    {
     temp=inter_x[j];
     inter_x[j]=inter_x[j+1];
     inter_x[j+1]=temp;
       }
      }
     }
     for(j=0;j<cnt-1;j=j+2)
     {
      setcolor(4);
      delay(100);
       if(j%4==0)
       line(inter_x[j]+1,yscan,inter_x[j+1]-1,yscan);
     }
 }
setcolor(3);
getch();
}

Tuesday, 24 February 2015

2D Transformation

//C Program for 2D transformation  : Translation ,Scaling , Rotation about origin and arbitrary point


#include<graphics.h>
#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;
int gd=DETECT,gm,ch,exitp=0;
float theta,sx,sy;
int a,b[20],i,c[20],d[20],e[20],tx,ty,ax,ay;
void translate();
void scale();
void rotate();
void quad();
void arotate();

int main()
{
cout<<"enter no points of poly";
cin>>a;
cout<<"\n enter no of cordinates of polygon";
for (i=0;i<a*2;i++)
{
cin>>b[i];
if (i%2==0)
c[i]=b[i]+320;
else
c[i]=240-b[i];
}
c[i]=c[0];
c[i+1]=c[1];
cout<<"\n please enter choice 1:Translation 2:scaling 3:Rotation about origin 4:Rotation about arbitary point";
cin>>ch;

switch(ch)
{
case 1:
cout<<"enter x and y translation";
cin>>tx>>ty;
quad();
translate();
break;

case 2:
cout<<"enter x and y Scaling";
cin>>sx>>sy;
quad();
scale();
break;

case 3:
cout<<"enter theta";
cin>>theta;
quad();
rotate();
break;

case 4:
cout<<"enter theta";
cin>>theta;
cout<<"enter x and y coordinates of arbitary point";
cin>>ax>>ay;
quad();
arotate();
break;
}
getch();
closegraph();
return 0;
}

void quad()
{
initgraph(&gd,&gm,NULL);
line(0,240,640,240);
line(320,0,320,480);
drawpoly(a+1,c);
}
void translate()
{
 for (i=0;i<a*2;i++)
{
if (i%2==0)
c[i]=b[i]+320+tx;
else
c[i]=240-b[i]-ty;
}
c[2*a]=c[0];
c[2*a+1]=c[1];
drawpoly(a+1,c);
}

void scale()
{
 for (i=0;i<a*2;i++)
{
if (i%2==0)
d[i]=(b[i]*sx)+320;
else
d[i]=240-(b[i]*sy);
}
d[2*a]=d[0];
d[2*a+1]=d[1];
drawpoly(a+1,d);
 getch();
}

void rotate()
{
 setcolor(3);
 theta=theta*3.14/180;
 for(i=0;i<a*2;i++)
{
e[i]=b[i]*cos(theta)-b[i+1]*sin(theta)+320;
i++;
e[i]=240-(b[i-1]*sin(theta)+b[i]*cos(theta));
}
e[2*a]=e[0];
e[2*a+1]=e[1];
drawpoly(a+1,e);
}

void arotate()
{
 setcolor(3);
 theta=theta*3.14/180;
 for(i=0;i<a*2;i++)
{
e[i]=ax+(b[i]-ax)*cos(theta)-(b[i+1]-ay)*sin(theta)+320;
i++;
e[i]=240-(ay+((b[i-1]-ax)*sin(theta)+(b[i]-ay)*cos(theta)));
}
e[2*a]=e[0];
e[2*a+1]=e[1];
drawpoly(a+1,e);
}

Saturday, 21 February 2015

Bezier curve to generate sine wave

#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <math.h>  
void bezier (int x[4][3], int y[4][3])
{
int gd = DETECT, gm; int i,j; double t;
   initgraph (&gd, &gm, "");
for(i=0;i<4;i++)
{  
for (t = 0.0; t < 1.0; t += 0.0005)
 {
double xt = pow (1-t, 2) * x[i][0] + 2 * t * pow (1-t, 1) * x[i][1] + pow (t, 2) * x[i][2];
double yt = pow (1-t, 2) * y[i][0] + 2 * t * pow (1-t, 1) * y[i][1] + pow (t, 2) * y[i][2];
putpixel (xt, yt, WHITE);
}
}
setcolor(RED);
for (i=0; i<4; i++)
  {
   for(j=0; j<2; j++)
    {
    line (x[i][j], y[i][j], x[i][j+1], y[i][j+1]);
    }
  }
    line(x[0][0],y[0][0],x[3][2],y[3][2]);  
    getch();
 closegraph();
 return;
 }  
void main()
{
 int x[4][3], y[4][3]; int i,j;
   printf ("Enter the x- and y-coordinates of the three control points.\n");
 for (i=0; i<4; i++)
  {
   for(j=0; j<3; j++)
    {
      scanf ("%d%d", &x[i][j], &y[i][j]);
     }
 }
bezier (x, y);
}

// Input
/*Enter the x- and y-coordinates of the three control points.
(10,50)     (40,10)     (70,50)
(70,50)        (100,90)     (130,50)
(130,50)     (160,10)     (190,50)
(190,50)     (220,90)     (250,50)
*/

Line Clipping using Cohen Sutherland Algorithm

#include<iostream>
#include<graphics.h>
using namespace std;

void get_code(int x,int y,int code[4],int wxmin,int wxmax,int wymin, int wymax)
{
 if(y>wymax)
    code[0]=1;      //bit1= 1 if endpoint is above the window
 else
    code[0]=0;
 if(y<wymin)
    code[1]=1;    //bit2= 1 if endpoint is below the window
 else
    code[1]=0;
 if(x>wxmax)
    code[2]=1;      //bit3= 1 if endpoint is to the right of the window
 else
    code[2]=0;
 if(x<wxmin)
    code[3]=1;      //bit4= 1 if endpoint is to the left of the window
 else
    code[3]=0;
}

int visible_line(int code1[4],int code2[4])
{
 int i;
 for(i=0;i<4;i++)
  if(code1[i]==1 || code2[i]==1)
    return(0);
  return(1);
}

int invisible_line(int code1[4],int code2[4])
{
 int i;
 for(i=0;i<4;i++)
  if(code1[i]==1 && code2[i]==1)
    return(1);
   return(0);
}

void partial_visible(int x1,int y1,int x2,int y2, int *xp,int *yp,int code[],int wxmin,int wxmax,int wymin,int wymax)
{
 int i=0,x,y;
 while(code[i] !=1 && i<4)
  i++;
 switch(i)
 {
  case 0:
    x= x1+ (wymax- y1)* ((float) (x2-x1)/(y2-y1));
    y=wymax;
    break;
  case 1:
    x= x1+ (wymin - y1)* ((float) (x2-x1)/(y2-y1));
    y=wymin;

    break;
  case 2:
    y= y1+ (wxmax - x1)* ((float) (y2-y1)/(x2-x1));
    x=wxmax;
    break;
  case 3:
    y= y1+ (wxmin - x1)* ((float) (y2-y1)/(x2-x1));
    x=wxmin;
    break;
 }
 if(i!=4)
 {
  *xp=x;
  *yp=y;
 }
}

void algorithm(int x1,int y1,int x2,int y2,int wxmin,int wxmax,int wymin,int wymax)
{
 int code1[4],code2[4],i,j,flag=0,temp;
 do
 {
  get_code(x1,y1,code1,wxmin,wxmax,wymin,wymax);
  get_code(x2,y2,code2,wxmin,wxmax,wymin,wymax);
  if(visible_line(code1,code2)==1)
  {
   rectangle(wxmin,wymin,wxmax,wymax);
   line(x1,y1,x2,y2);
   flag=1;
   getch();
  }
  else
   if(invisible_line(code1,code2)==1)
   {
    rectangle(wxmin,wymin,wxmax,wymax);
    getch();
    flag=1;
   }
  else
  {
   partial_visible(x1,y1,x2,y2,&x1,&y1,code1,wxmin,wxmax,wymin,wymax);
   partial_visible(x1,y1,x2,y2,&x2,&y2,code2,wxmin,wxmax,wymin,wymax);
  }
 }while(flag==0);
}

int main()
{
 int x1,y1,x2,y2,wxmin,wymin,wxmax,wymax;
 char ans;
 cout<<"\n\t Enter the window co-ordinates: \n";
 cout<<"\n\t\twxmin: ";
 cin>>wxmin;
 cout<<"\n\t\twymin: ";
 cin>>wymin;
 cout<<"\n\t\twxmax: ";
 cin>>wxmax;
 cout<<"\n\t\twymax: ";
 cin>>wymax;
 do
 {
  cout<<"\n\n\tEnter the co-ordinates of the line to be clipped: \n";
  cout<<"\n\t(x1,y1): ";
  cin>>x1>>y1;
  cout<<"\n\t(x2,y2): ";
  cin>>x2>>y2;
  int gd = DETECT, gm;
  initgraph(&gd,&gm,NULL);
 rectangle(wxmin,wymin,wxmax,wymax);
  line(x1,y1,x2,y2);
getch();
cleardevice();
setcolor(RED);
 algorithm(x1,y1,x2,y2,wxmin,wxmax,wymin,wymax);
  getch();
  closegraph();
  cout<<"\n\n\tDo you want to clip another line? ";
  cin>>ans;
 }while(ans=='y' || ans=='Y');
}

Friday, 20 February 2015

Bezier curve to draw flower in C

#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <math.h>  
void bezier (int x[4][4], int y[4][4])
{
int gd = DETECT, gm; int i,j; double t;
   initgraph (&gd, &gm, "");
for(i=0;i<4;i++)
{  
for (t = 0.0; t < 1.0; t += 0.0005)
 {
double xt = pow (1-t, 3) * x[i][0] + 3 * t * pow (1-t, 2) * x[i][1] + 3 * pow (t, 2) * (1-t) * x[i][2] + pow (t, 3) * x[i][3];
double yt = pow (1-t, 3) * y[i][0] + 3 * t * pow (1-t, 2) * y[i][1] + 3 * pow (t, 2) * (1-t) * y[i][2] + pow (t, 3) * y[i][3];
putpixel (xt, yt, WHITE);
}
}
setcolor(RED);
getch();
 closegraph();
 return;
 }  
void main()
{
 int x[4][4], y[4][4]; int i,j;
   printf ("Enter the x- and y-coordinates of the four control points.\n"); /* Co-ordinates for four petals total 16. If you want to increase petals increase the size of x & y array. Each petal will add four more points.*/
 for (i=0; i<4; i++)
  {
   for(j=0; j<4; j++)
    {
      scanf ("%d%d", &x[i][j], &y[i][j]);
   
     }
 }
bezier (x, y);
}

//Sample Points Co-ordinates
/*
(200, 200) (160, 160) (240, 160) (200, 200) //for first petal
(200, 200) (240, 160) (240, 240) (200, 200) //for second petal
(200, 200) (240, 240) (160, 240) (200, 200) //for third petal
(200, 200) (160, 240) (160, 160) (200, 200) //for fourth petal

*/

Thursday, 19 February 2015

MIDPOINT Circle

#include<graphics.h>
#include<stdio.h>
#include<time.h>
int main()
{
int gd=DETECT,gm;
int i,r,x,y,xc,yc;
float d;
printf("Enter Radius\n");
scanf("%d",&r);
printf("Enter Center of circle\n");

scanf("%d",&xc);
scanf("%d",&yc);
initgraph(&gd,&gm,NULL);
line(0,240,640,240);
   line(320,0,320,480);
     xc = 320 + xc;
   yc = 240 - yc;
putpixel(xc,yc,RED);
d=1.25-r;
x=0;
y=r;
do
{
if(d<0)
{
x=x+1;
d=d+2*x+1;
}
else
{
x=x+1;
y=y-1;
d=d+2*x-2*y+10;
}
usleep(100000);
putpixel(xc+x,yc+y,1);
putpixel(xc-y,yc-x,2);
putpixel(xc+y,yc-x,3);
putpixel(xc-y,yc+x,4);
putpixel(xc+y,yc+x,5);
putpixel(xc-x,yc-y,6);
putpixel(xc+x,yc-y,7);
putpixel(xc-x,yc+y,8);
}
while(x<y);
getch();
}

DDA Circle

#include<iostream>
#include<math.h>
#include<graphics.h>
using namespace std;
void dda_circle(int xc,int yc,int r);
int main()
{
   int xc,yc,r,gd=DETECT,gm;
  cout<<"\nEnter the Center point (xc,yc) :";
   cin>>xc>>yc;
   cout<<"\nEnter the Radius :";
   cin>>r;
  initgraph(&gd,&gm,"");
   setcolor(10);
   line(0,240,640,240);
   line(320,0,320,480);
   setcolor(5);
   xc = 320 + xc;
   yc = 240 - yc;
   dda_circle(xc,yc,r);
   getch();
closegraph();  
return 0;
}
void dda_circle(int xc,int yc,int r)
{
   float xc1,xc2,yc1,yc2,eps,sx,sy;
  int val,i;
  xc1=r;
  yc1=0;
  sx=xc1;
  sy=yc1;
  i=0;
  do{
      val=pow(2,i);
      i++;
      }while(val<r);
  eps = 1/pow(2,i-1);
  do{
      xc2 = xc1 + yc1*eps;
      yc2 = yc1 - eps*xc2;
      putpixel(xc+xc2+r,yc-yc2,3);
      putpixel(xc+xc2-r,yc-yc2,4);
      putpixel(xc+xc2,yc-yc2+r,5);
      putpixel(xc+xc2,yc-yc2-r,6);
      putpixel(xc+xc2,yc-yc2,15);
      xc1=xc2;
      yc1=yc2;
     }while((yc1-sy)<eps || (sx-xc1)>eps);
}

BRESENHAM's Line Drawing Algorithm

using namespace std;
#include<iostream>
#include<graphics.h>
#include<math.h>

class BRES
{
    int xmid,ymid;
    float x1,y1,x2,y2;
public :
    void quad();
    void simple(float,float,float,float);
    void dot(float,float,float,float);
    void dash(float,float,float,float);
    void dashdot(float,float,float,float);
    void thick(float,float,float,float,int);

};

void BRES :: quad()
{
    int xmax=640,ymax=480;
    int gd=DETECT,gm;
    initgraph(&gd,&gm,NULL);
    xmid=xmax/2;
    ymid=ymax/2;
    line(0,ymid,xmax,ymid);
    line(xmid,0,xmid,ymax);
}

void BRES :: simple(float x1,float y1,float x2,float y2)
{
    int i,j,p,x,y;
    float dx,dy;
    dx = (x2 - x1);
        dy = (y2 - y1);
    p = 2 * (dy) - (dx);
        x = x1;
        y = y1;
    for(i=x;i<=x2;i++)
        {
          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(320+x,240-y,18);
              }
}
      
void BRES :: dot(float x1,float y1,float x2,float y2)
{
    int i,j,p,x,y;
    float dx,dy;
    dx = (x2 - x1);
        dy = (y2 - y1);
    p = 2 * (dy) - (dx);
        x = x1;
        y = y1;
    for(i=x;i<=x2;i++)
        {
          if(p < 0)
          {
            x=x+1;
            y=y;
            p = p + 2 * (dy);
        }
          else
          {
            x=x+1;
            y=y+1;
            p = p + 2 * (dy - dx);
         }
             if(i%2==0)                      
            putpixel(320+x,240-y,18);
    }   

}

void BRES :: dash(float x1,float y1,float x2,float y2)
{
    int i,j,p,x,y;
    float dx,dy;
    dx = (x2 - x1);
        dy = (y2 - y1);
    p = 2 * (dy) - (dx);
        x = x1;
        y = y1;
    for(i=x;i<=x2;i++)
        {
          if(p < 0)
          {
            x=x+1;
            y=y;
            p = p + 2 * (dy);
        }
          else
          {
            x=x+1;
            y=y+1;
            p = p + 2 * (dy - dx);
         }
         if(i%5<3)                      
        putpixel(320+x,240-y,18);
    }   

}

void BRES :: dashdot(float x1,float y1,float x2,float y2)
{
    int i,j,p,x,y;
    float dx,dy;
    dx = (x2 - x1);
        dy = (y2 - y1);
    p = 2 * (dy) - (dx);
        x = x1;
        y = y1;
    for(i=x;i<=x2;i++)
        {
          if(p < 0)
          {
            x=x+1;
            y=y;
            p = p + 2 * (dy);
        }
          else
          {
            x=x+1;
            y=y+1;
            p = p + 2 * (dy - dx);
         }
         if(i%6<3 || i%6==4)                      
            putpixel(320+x,240-y,18);
       }
}

void BRES :: thick(float x1,float y1,float x2,float y2,int t)
{
    int i,j,p,x,y;
    float dx,dy;
    dx = (x2 - x1);
        dy = (y2 - y1);
    p = 2 * (dy) - (dx);
        x = x1;
        y = y1;
    for(i=x;i<=x2;i++)
        {
          if(p < 0)
          {
            x=x+1;
            y=y;
            p = p + 2 * (dy);
        }
          else
          {
            x=x+1;
            y=y+1;
            p = p + 2 * (dy - dx);
         }
         for(j=0;j<t;j++)                      
        putpixel(320+x,240-y+j,18);
       }
}



int main()
{
    int gd=DETECT,gm;
    float x1,y1,x2,y2;
    int ans, ch,t;
    BRES p;
    do
    {
        cout<<"\nEnter X1 : ";
        cin>>x1;
        cout<<"\nEnter Y1 : ";
        cin>>y1;
        cout<<"\nEnter X2 : ";
        cin>>x2;
        cout<<"\nEnter Y2 : ";
        cin>>y2;
       
      cout<<"\nMenu:";
      cout<<"\n1.Simple line\n2.Dashed line\n3.Dotted line\n4.Center line\n5.thick";
      cout<<"\nEnter choice : ";
      cin>>ch;
      switch(ch)
      {

          case 1:
        p.quad();
        if(x1<x2 || y1<y2 )
            p.simple(x1,y1,x2,y2);   
        else if    (x1==x2 && y1==y2)
            putpixel(320+x1,240-y1,15);       
        else
            p.simple(x2,y2,x1,y1);       
        break;
         case 2:
        p.quad();
        if(x1<x2 || y1<y2 )
            p.dash(x1,y1,x2,y2);   
        else if    (x1==x2 && y1==y2)
            putpixel(320+x1,240-y1,15);       
        else
            p.dash(x2,y2,x1,y1);           
        break;
        case 3:
        p.quad();
        if(x1<x2 || y1<y2 )
            p.dot(x1,y1,x2,y2);   
        else if    (x1==x2 && y1==y2)
            putpixel(320+x1,240-y1,15);       
        else
            p.dot(x2,y2,x1,y1);   
        break;
        case 4:
        p.quad();
        if(x1<x2 || y1<y2 )
            p.dashdot(x1,y1,x2,y2);   
        else if    (x1==x2 && y1==y2)
            putpixel(320+x1,240-y1,15);       
        else
            p.dashdot(x2,y2,x1,y1);   
        break;
        case 5:
        printf("\n Enter the thick ness of line");       
        scanf("%d",&t);
        p.quad();
        if(x1<x2 || y1<y2 )
            p.thick(x1,y1,x2,y2,t);   
        else if    (x1==x2 && y1==y2)
            putpixel(320+x1,240-y1,15);       
        else
            p.thick(x2,y2,x1,y1,t);   
        break;
    }       
    getch();   
    closegraph();
        cout<<"Do You Want To Continue : ";
        cin>>ans;
        }while(ans==1);
    getch();
    return 0;
}

DDA Line Drawing Algorithm


PROGRAM:
using namespace std;
#include<iostream>
#include<graphics.h>
#include<math.h>

class DDALINE
{
    int xmid,ymid;

    float x1,y1,x2,y2;
public :
    void quad();
    void simple(float,float,float,float);
    void dot(float,float,float,float);
    void dash(float,float,float,float);
    void dashdot(float,float,float,float);
    void thick(float,float,float,float,int);

};

void DDALINE :: quad()
{
    int xmax=640,ymax=480;
    int gd=DETECT,gm;
    initgraph(&gd,&gm,NULL);
    xmid=xmax/2;
    ymid=ymax/2;
    line(0,ymid,xmax,ymid);
    line(xmid,0,xmid,ymax);
}

void DDALINE :: simple(float x1,float y1,float x2,float y2)
{
    int i,steps;
    float dx,dy,xinc,yinc;
    dx=x2-x1;
    dy=y2-y1 ;

    if(dx<dy)
    {
        steps=abs(dy);
    }
    else
    {
        steps=abs(dx);
    }
    xinc=(x2-x1)/steps;
    yinc=(y2-y1)/steps;
    putpixel(x1,y1,15);
    for(i=1;i<=steps;i++)
                {
                x1=x1+xinc;
                y1=y1+yinc;
                       putpixel(x1,y1,18);
              }
}
     
void DDALINE :: dot(float x1,float y1,float x2,float y2)
{
int i,steps;
    float dx,dy,xinc,yinc;

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

    if(dx<dy)
    {
        steps=abs(dy);
    }
    else
    {
        steps=abs(dx);
    }

    xinc=(x2-x1)/steps;
    yinc=(y2-y1)/steps;
    putpixel(x1,y1,15);
    for(i=1;i<=steps;i++)
                {
                x1=x1+xinc;
                y1=y1+yinc;
    if(i%2==0)                     
    {putpixel(x1,y1,18);}
              }  

}

void DDALINE :: dash(float x1,float y1,float x2,float y2)
{
    int i,steps;
    float dx,dy,xinc,yinc;
    dx=x2-x1;
    dy=y2-y1 ;
    if(dx<dy)
    {
        steps=abs(dy);
    }
    else
    {
        steps=abs(dx);
    }

    xinc=(x2-x1)/steps;
    yinc=(y2-y1)/steps;
    putpixel(x1,y1,15);
    for(i=1;i<=steps;i++)
                {
                x1=x1+xinc;
                y1=y1+yinc;
    if(i%5<3)                     
    {putpixel(x1,y1,18);}
              }  

}

void DDALINE :: dashdot(float x1,float y1,float x2,float y2)
{
    int i,steps;
    float dx,dy,xinc,yinc;
    dx=x2-x1;
    dy=y2-y1 ;
    if(dx<dy)
    {
        steps=abs(dy);
    }
    else
    {
        steps=abs(dx);
    }
    xinc=(x2-x1)/steps;
    yinc=(y2-y1)/steps;
    putpixel(x1,y1,15);
    for(i=1;i<=steps;i++)
                {
                x1=x1+xinc;
                y1=y1+yinc;
                if(i%6<3 || i%6==4)                     
                {putpixel(x1,y1,18);}
              }  

}

void DDALINE :: thick(float x1,float y1,float x2,float y2,int t)
{
    int i,j,steps;
    float dx,dy,xinc,yinc;
    dx=x2-x1;
    dy=y2-y1 ;
    if(dx<dy)
    {
        steps=abs(dy);
    }
    else
    {
        steps=abs(dx);
    }
    xinc=(x2-x1)/steps;
    yinc=(y2-y1)/steps;
    putpixel(x1,y1,15);
    for(i=1;i<=steps;i++)
                {
                x1=x1+xinc;
                y1=y1+yinc;
                for(j=0;j<t;j++)                     
                {putpixel(x1,y1+j,18);}
              }  

}



int main()
{
    int gd=DETECT,gm;
    float x1,y1,x2,y2;
    int ans, ch,t;
    DDALINE p;
    do
    {
        cout<<"\nEnter X1 : ";
        cin>>x1;
        cout<<"\nEnter Y1 : ";
        cin>>y1;
        cout<<"\nEnter X2 : ";
        cin>>x2;
        cout<<"\nEnter Y2 : ";
        cin>>y2;
        x1=x1+320;
        y1=240-y1;
        x2=x2+320;
        y2=240-y2;
      cout<<"\nMenu:";
      cout<<"\n1.Simple line\n2.Dashed line\n3.Dotted line\n4.Center line\n5.thick";
      cout<<"\nEnter choice : ";
      cin>>ch;
      switch(ch)
      {

          case 1:
        p.quad();
        p.simple(x1,y1,x2,y2);  
         break;
         case 2:
        p.quad();
        p.dash(x1,y1,x2,y2);           
        break;
        case 3:
        p.quad();
        p.dot(x1,y1,x2,y2);
         break;
        case 4:
        p.quad();
        p.dashdot(x1,y1,x2,y2);
              break;
        case 5:
        printf("\n Enter the thick ness of line");      
        scanf("%d",&t);
        p.quad();
        p.thick(x1,y1,x2,y2,t);
              break;
    }      
    getch();  
    closegraph();
        cout<<"Do You Want To Continue : ";
        cin>>ans;
    getch();  
    closegraph();
    }while(ans==1);
    getch();
    return 0;
}