0% found this document useful (0 votes)
117 views19 pages

CG Codes

The document contains code snippets for various computer graphics algorithms including: 1. Flood fill and boundary fill algorithms for filling regions bounded by a color. 2. DDA and Bresenham's line drawing algorithms for drawing lines on a 2D plane. 3. Circle drawing algorithms using the midpoint method and a recursive flood fill approach. 4. An ellipse drawing algorithm using the midpoint method.

Uploaded by

Anjali shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views19 pages

CG Codes

The document contains code snippets for various computer graphics algorithms including: 1. Flood fill and boundary fill algorithms for filling regions bounded by a color. 2. DDA and Bresenham's line drawing algorithms for drawing lines on a 2D plane. 3. Circle drawing algorithms using the midpoint method and a recursive flood fill approach. 4. An ellipse drawing algorithm using the midpoint method.

Uploaded by

Anjali shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

FLOODFILL

#include<stdio.h>
#include<graphics.h>
void main()
{
int x,y;
int gd=DETECT,gm=0;
initgraph(&gd,&gm,"C:/TURBOC3/BGI");
rectangle(100,100,150,150);
flood(105,105,4,15);
getch();
closegraph();
}
flood(int x, int y, int fill_col,int old_col)
{
if(getpixel(x,y)!=old_col && getpixel(x,y)!= fill_col)
{
putpixel(x,y,fill_col);
flood(x+1,y,fill_col,old_col);
flood(x-1,y,fill_col,old_col);
flood(x,y+1,fill_col,old_col);
flood(x,y-1,fill_col,old_col);
}
return 0;
}

BOUNDARYFILL
#include<stdio.h>
#include<graphics.h>
void main()
{
int x,y;
int gd=DETECT,gm=0;
initgraph(&gd,&gm,"C:/TURBOC3/BGI");
rectangle(100,100,150,150);
fillalgo(105,105,4,15);
getch();
closegraph();
}
int fillalgo(int x, int y, int fill_col,int back_col)
{
if(getpixel(x,y)!=back_col && getpixel(x,y)!= fill_col)
{
putpixel(x,y,fill_col);
fillalgo(x+1,y,fill_col,back_col);
fillalgo(x-1,y,fill_col,back_col);
fillalgo(x,y+1,fill_col,back_col);
fillalgo(x,y-1,fill_col,back_col);
}
return 0;
}

DDA ALGORITHM (LINE)


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

void main()
{
 int gd=DETECT, gm=0;
 int x1,y1,x2,y2,dx,dy,steps,xincre,yincre,x,y,i;
 initgraph(&gd,&gm, "C:\\TURBOC3\\BGI");
 printf("Enter starting Points: ");
 scanf("%d%d", &x1,&y1);
 printf("Enter Ending Points: ");
 scanf("%d%d", &x2,&y2);
 dx=(x2-x1);
 dy=(y2-y1);

 if(abs(dx)>abs(dy))
 {
  steps=abs(dx);
 }

 xincre=dx/steps;
 yincre=dy/steps;
 x=x1;
 y=y1;
 putpixel(x,y,RED);

 for(i=0;i<steps;i++)
 {
  x=x+xincre;
  y=y+yincre;
  putpixel(x,y,RED);
 }
 getch();
 closegraph();
}

BRESHENHAM ALGORITHM (LINE)


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void drawline(int x0, int y0, int x1, int y1)
{
 int dx,dy,p,x,y;
 dx=x1-x0;
 dy=y1-y0;
 x=x0;
 y=y0;
 p=2*dy-dx;
 while(x<x1){
   if(p>=0){
     putpixel(x,y,7);
     y=y+1;
     p=p+2*dy-2*dx;
    }
    else
    {
     putpixel(x,y,7);
     p=p+2*dy;
    }
    x=x+1;
   }
}
int main(){
    int gd=DETECT,gm=0,error,x0,y0,x1,y1;
    initgraph(&gd,&gm, "C:\\TURBOC3\\BGI");
    printf("Enter co-odrinates for starting points: ");
    scanf("%d%d", &x0,&y0);
    printf("Enter co-ordinates for ending points: ");
    scanf("%d%d", &x1,&y1);
    drawline(x0,y0,x1,y1);
    getch();
    return 0;
   }

CIRCLE
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x,y,a,b,radius,dp;
int gdriver=DETECT, gmode;
initgraph(&gdriver, &gmode, "C://TURBOC3//BGI") ;
printf("Enter the coordinates: ");
scanf("%d %d" ,&a,&b);
printf("Enter the radius: ");
scanf("%d" ,&radius);
x=0;
y=radius;
dp=1-radius;
do{
putpixel(a+x, b+y, RED);
putpixel(a+y, b+x, RED);
putpixel(a-y, b+x, RED);
putpixel(a-x, b+y, RED);
putpixel(a-x, b-y, RED);
putpixel(a-y, b-x, RED);
putpixel(a+y, b-x, RED);
putpixel(a+x, b-y, RED);
if(dp<0)
{dp+=(2*x)+1;
}else{
y=y-1;
dp+=(2*x)-(2*y)+1;
}
x=x+1;
}while(y>x);
getch();
}

MIDPOINT CIRCLE
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int x,y,x_mid,y_mid,radius,dp;
int g_mode,g_driver=DETECT;
clrscr();
initgraph(&g_driver,&g_mode,"C:\\TURBOC3\\BGI");
printf("*********** MID POINT Circle drawing algorithm ********\n\n");
printf("\nenter the coordinates= ");
scanf("%d %d",&x_mid,&y_mid);
printf("\n now enter the radius =");
scanf("%d",&radius);
x=0;
y=radius;
dp=1-radius;
do
{
putpixel(x_mid+x,y_mid+y,YELLOW);
putpixel(x_mid+y,y_mid+x,YELLOW);
putpixel(x_mid-y,y_mid+x,YELLOW);
putpixel(x_mid-x,y_mid+y,YELLOW);
putpixel(x_mid-x,y_mid-y,YELLOW);
putpixel(x_mid-y,y_mid-x,YELLOW);
putpixel(x_mid+y,y_mid-x,YELLOW);
putpixel(x_mid+x,y_mid-y,YELLOW);
if(dp<0) {
dp+=(2*x)+1;
}
else{
y=y-1;
dp+=(2*x)-(2*y)+1;
}
x=x+1;
}while(y>x);
getch();
}

MIDPOINT ELLIPSE
#include<stdio.h>
#include<graphics.h>
void main(){
      long x,y,x_center,y_center;
      long a_sqr,b_sqr, fx,fy, d,a,b,tmp1,tmp2;
      int g_driver=DETECT,g_mode;
      clrscr();

    initgraph(&g_driver,&g_mode,"C:\\TURBOC3\\BGI");
    printf("********* MID POINT ELLIPSE ALGORITHM *********");
    printf("\n\n Enter coordinate x and y = ");
    scanf("%ld%ld",&x_center,&y_center);
    printf("\n Now enter constants a and b = ");
    scanf("%ld%ld",&a,&b);
    x=0;
    y=b;
    a_sqr=a*a;
    b_sqr=b*b;
    fx=2*b_sqr*x;
    fy=2*a_sqr*y;
  d=b_sqr-(a_sqr*b)+(a_sqr*0.25);
  do
   {
  putpixel(x_center+x,y_center+y,1);
  putpixel(x_center-x,y_center-y,1);
  putpixel(x_center+x,y_center-y,1);
  putpixel(x_center-x,y_center+y,1);

   if(d<0)
    {
  d=d+fx+b_sqr;
    }
   else
  {
  y=y-1;
  d=d+fx+-fy+b_sqr;
  fy=fy-(2*a_sqr);
  }
  x=x+1;
  fx=fx+(2*b_sqr);
  delay(10);

   }
   while(fx<fy);
   tmp1=(x+0.5)*(x+0.5);
   tmp2=(y-1)*(y-1);
   d=b_sqr*tmp1+a_sqr*tmp2-(a_sqr*b_sqr);
   do
   {
  putpixel(x_center+x,y_center+y,1);
  putpixel(x_center-x,y_center-y,1);
  putpixel(x_center+x,y_center-y,1);
  putpixel(x_center-x,y_center+y,1);

   if(d>=0)
  d=d-fy+a_sqr;
   else

  {
  x=x+1;
  d=d+fx-fy+a_sqr;
  fx=fx+(2*b_sqr);
  }
   y=y-1;
   fy=fy-(2*a_sqr);
   }
   while(y>0);
   getch();
   closegraph();
}

BEIZER CURVE
#include<conio.h>
#include<stdio.h>
#include<dos.h>
#include<graphics.h>
void main(){
int gd=DETECT,gm=0,x[4],y[4],px,py,i;
double t;
initgraph(&gd,&gm,"C://TURBOC3//BGI");
printf("Enter four control points of bezier curve:\n");
for(i=0;i<4;i++)
{
scanf("%d%d",&x[i],&y[i]);
}
for(t=0.0;t<=1.0;t+=0.001)
{
px=(1-t)*(1-t)*(1-t)*x[0]+3*t*(1-t)*(1-t)*x[1]+3*t*t*(1-t)*x[2]+t*t*t*x[3];
py=(1-t)*(1-t)*(1-t)*y[0]+3*t*(1-t)*(1-t)*y[1]+3*t*t*(1-t)*y[2]+t*t*t*y[3];
putpixel(px,py,WHITE);
delay(2);
}
getch();
closegraph();
}

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

void main()
{
 int gd=DETECT, gm=0;
 int x1,y1,x2,y2,dx,dy,steps,xincre,yincre,x,y,i;
 initgraph(&gd,&gm, "C:\\TURBOC3\\BGI");
 printf("Enter starting Points: ");
 scanf("%d%d", &x1,&y1);
 printf("Enter Ending Points: ");
 scanf("%d%d", &x2,&y2);
 dx=(x2-x1);
 dy=(y2-y1);

 if(abs(dx)>abs(dy))
 {
  steps=abs(dx);
 }

 xincre=dx/steps;
 yincre=dy/steps;
 x=x1;
 y=y1;
 putpixel(x,y,RED);
 for(i=0;i<steps;i++)
 {
  x=x+xincre;
  y=y+yincre;
  putpixel(x,y,RED);
 }
 getch();
 closegraph();
}

TRANSLATION,ROTATION,SCALING
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
void main()
{
int gm;
int gd=DETECT;
int x1,x2,x3,y1,y2,y3 , nx1, nx2 , nx3 , ny1 , ny2 , ny3 , c;
int sx,sy,xt,yt,r;
float t;
initgraph(&gd,&gm,"C:/TURBOC3/BGI");
printf("Program for Basic Transformations");
printf("\nEnter the points of triangle");
setcolor(1);
scanf("%d%d%d%d%d%d" , &x1 ,&y1, &x2,&y2 , &x3,&y3);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
printf("1.Translation\n 2.Rotation\n 3.Scaling\n 4.Exit\n");
printf("Enter your choice\n");
scanf("%d",&c);
switch(c)
{
case 1:
printf("Enter the Translation factor");
scanf("%d%d",&xt ,&yt);
nx1=x1+xt;
ny1=y1+yt;
nx2=x2+xt;
ny2=y2+yt;
nx3=x3+xt;
ny3=y3+yt;
line(nx1 , ny1, nx2 , ny2);
line(nx2 ,ny2 , nx3 ,ny3);
line(nx3 ,ny3 , nx1 ,ny1 );
getch();

case 2:
printf("\nEnter the angle of rotation\n");
scanf("%d",&r);
t=3.14*r/180;
nx1=abs(x1*cos(t)-y1*sin(t));
ny1=abs(x1*sin(t)+y1*cos(t));
nx2=abs(x2*cos(t)-y2*sin(t));
ny2=abs(x2*sin(t)+y2*cos(t));
nx3=abs(x3*cos(t)-y3*sin(t));
ny3=abs(x3*sin(t)+y3*cos(t));
line(nx1 ,ny1, nx2 , ny2);
line(nx2 ,ny2, nx3 ,ny3);
line(nx3, ny3, nx1 , ny1);
getch();

case 3:
printf("\nEnter the scaling factor");
scanf("%d%d",&sx,&sy);
nx1=x1*sx;
ny1=y2*sy;
nx2=x2*sx;
ny2=y2*sy;
nx3=x3*sx;
ny3=x3*sy;
line(nx1 , ny1, nx2 , ny2);
line(nx1 , ny2 , nx3, ny3);
line(nx3 ,ny3 , nx1 , ny1);
getch();

case 4:
   break;
default:
printf("Enter the correct choice");
}
closegraph();
}

PARALLEL PROJECTION
#include <stdio.h>
#include <stdlib.h>
#include<graphics.h>
#include<conio.h>
void draw3d(int s,int x[20],int y[20],int d);
void main()
{
    int gd=DETECT,gm;
    int x[20],y[20],i,s,d;
    initgraph(&gd,&gm,"c:\\TURBOC3\\bgi");
    printf("Enter the No of sides : ");
    scanf("%d",&s);
    for(i=0;i< s;i++)
    {
        printf("(x%d,y%d) :",i,i);
        scanf("%d%d",&x[i],&y[i]);
    }
    printf("Depth :");
    scanf("%d",&d);
    draw3d(s,x,y,d);
    getch();
    setcolor(14);
    for(i=0;i< s-1;i++)
    {
        line(x[i]+200,y[i],x[i+1]+200,y[i+1]);
    }
    line(x[i]+200,y[i],x[0]+200,y[0]);
    getch();//top view
    for(i=0;i< s-1;i++)
    {
        line(x[i],300,x[i+1],300);
        line(x[i],300+d*2,x[i+1],300+d*2);
        line(x[i],300,x[i],300+d*2);
        line(x[i+1],300,x[i+1],300+d*2);
    }
    getch();//side view
    for(i=0;i< s-1;i++)
    {
        line(10,y[i],10,y[i+1]);
        line(10+d*2,y[i],10+d*2,y[i+1]);
        line(10,y[i],10+d*2,y[i]);
        line(10,y[i+1],10+d*2,y[i+1]);
    }
    getch();
    closegraph();
}
void draw3d(int s,int x[20],int y[20],int d)
{
    int i,j,k=0;
    for(j=0;j< 2;j++)
    {
        for(i=0;i< s-1;i++)
            line(x[i]+k,y[i]-k,x[i+1]+k,y[i+1]-k);
        line(x[i]+k,y[i]-k,x[0]+k,y[0]-k);
        k=d;
    }
    for(i=0;i< s;i++)
        line(x[i],y[i],x[i]+d,y[i]-d);
}

PERSPECTIVE PROJECTION
#include<stdio.h>
#include<math.h>
#include<graphics.h>
void main()
{
    int x1,y1,x2,y2,gd,gm;
    int ymax,a[4][8];
    float par[4][4],b[4][8];
    int i,j,k,m,n,p;
    int xp, yp, zp, x, y, z;
    a[0][0] = 100; a[1][0] = 100; a[2][0] = -100;
    a[0][1] = 200; a[1][1] = 100; a[2][1] = -100;
    a[0][2] = 200; a[1][2] = 200; a[2][2] = -100;
    a[0][3] = 100; a[1][3] = 200; a[2][3] = -100;
    a[0][4] = 100; a[1][4] = 100; a[2][4] = -200;
    a[0][5] = 200; a[1][5] = 100; a[2][5] = -200;
    a[0][6] = 200; a[1][6] = 200; a[2][6] = -200;
    a[0][7] = 100; a[1][7] = 200; a[2][7] = -200;
    detectgraph(&gd,&gm);
    initgraph(&gd,&gm, "c:\\TURBOC3\\bgi");
    ymax = getmaxy();
    xp = 300; yp = 320; zp = 100;
    for(j=0; j<8; j++)
    {
        x = a[0][j]; y = a[1][j]; z = a[2][j];
        b[0][j] = xp - ( (float)( x - xp )/(z - zp)) * (zp);
        b[1][j] = yp - ( (float)( y - yp )/(z - zp)) * (zp);
    }
    for(j=0;j<3;j++) /*- front plane display -*/
    {
        x1=(int) b[0][j]; y1=(int) b[1][j];
        x2=(int) b[0][j+1]; y2=(int) b[1][j+1];
        line( x1,ymax-y1,x2,ymax-y2);
    }
    x1=(int) b[0][3]; y1=(int) b[1][3];
    x2=(int) b[0][0]; y2=(int) b[1][0];
    line( x1, ymax-y1, x2, ymax-y2);
    setcolor(11);
    for(j=4;j<7;j++) /*- back plane display -*/
    {
        x1=(int) b[0][j]; y1=(int) b[1][j];
        x2=(int) b[0][j+1]; y2=(int) b[1][j+1];
        line( x1, ymax-y1, x2, ymax-y2);
    }
    x1=(int) b[0][7]; y1=(int) b[1][7];
    x2=(int) b[0][4]; y2=(int) b[1][4];
    line( x1, ymax-y1, x2, ymax-y2);
    setcolor(7);
    for(i=0;i<4;i++)
    {
        x1=(int) b[0][i]; y1=(int) b[1][i];
        x2=(int) b[0][4+i]; y2=(int) b[1][4+i];
        line( x1, ymax-y1, x2, ymax-y2);
    }
    getch();
}

BITMAP
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,i,j;
int a[20][20]=
{{0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0},
{0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,0},
{0,1,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,0},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0},
{0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,0},
{0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0}};

initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
for(i=0;i<19;i++)
{
for(j=0;j<19;j++)
{
if(a[i][j]==1){
putpixel(100+j,200+i,WHITE);
}
}
getch();
}
}

STROKE METHOD
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void main()
{
int i,j,k,x,y;
int gd=DETECT,gm;//DETECT is macro defined in graphics.h
    /* ch1 ch2 ch3 ch4 are character arrays that display alphabets */
int ch1[][10]={ {1,1,1,1,1,1,1,1,1,1},
            {1,1,1,1,1,1,1,1,1,1},
            {0,0,0,0,1,1,0,0,0,0},
            {0,0,0,0,1,1,0,0,0,0},
            {0,0,0,0,1,1,0,0,0,0},
            {0,0,0,0,1,1,0,0,0,0},
            {0,0,0,0,1,1,0,0,0,0},
            {0,1,1,0,1,1,0,0,0,0},
            {0,1,1,0,1,1,0,0,0,0},
            {0,0,1,1,1,0,0,0,0,0}};
int ch2[][10]={ {0,0,0,1,1,1,1,0,0,0},
            {0,0,1,1,1,1,1,1,0,0},
            {1,1,0,0,0,0,0,0,1,1},
            {1,1,0,0,0,0,0,0,1,1},
            {1,1,0,0,0,0,0,0,1,1},
            {1,1,0,0,0,0,0,0,1,1},
            {1,1,0,0,0,0,0,0,1,1},
            {1,1,0,0,0,0,0,0,1,1},
            {0,0,1,1,1,1,1,1,0,0},
            {0,0,0,1,1,1,1,0,0,0}};
int ch3[][10]={ {1,1,0,0,0,0,0,0,1,1},
            {1,1,0,0,0,0,0,0,1,1},
            {1,1,0,0,0,0,0,0,1,1},
            {1,1,0,0,0,0,0,0,1,1},
            {1,1,1,1,1,1,1,1,1,1},
            {1,1,1,1,1,1,1,1,1,1},
            {1,1,0,0,0,0,0,0,1,1},
            {1,1,0,0,0,0,0,0,1,1},
            {1,1,0,0,0,0,0,0,1,1},
            {1,1,0,0,0,0,0,0,1,1}};
int ch4[][10]={ {1,1,0,0,0,0,0,0,1,1},
            {1,1,1,1,0,0,0,0,1,1},
            {1,1,0,1,1,0,0,0,1,1},
            {1,1,0,1,1,0,0,0,1,1},
            {1,1,0,0,1,1,0,0,1,1},
            {1,1,0,0,1,1,0,0,1,1},
            {1,1,0,0,0,1,1,0,1,1},
            {1,1,0,0,0,1,1,0,1,1},
            {1,1,0,0,0,0,1,1,1,1},
            {1,1,0,0,0,0,0,0,1,1}};
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");//initialize graphic mode
setbkcolor(LIGHTGRAY);//set color of background to darkgray
for(k=0;k<4;k++)
    {
for(i=0;i<10;i++)
    {
for(j=0;j<10;j++)
        {
if(k==0)
        {
if(ch1[i][j]==1)
putpixel(j+250,i+230,RED);
        }
if(k==1)
        {
if(ch2[i][j]==1)
putpixel(j+300,i+230,RED);
        }
if(k==2)
        {
if(ch3[i][j]==1)
putpixel(j+350,i+230,RED);
        }
if(k==3)
        {
if(ch4[i][j]==1)
putpixel(j+400,i+230,RED);
        }
        }
delay(200);
    }
    }
getch();
closegraph();
}

CLIPPING
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
typedef struct coordinate
{
    int x,y;
    char code[4];
}PT;
void drawwindow();
void drawline(PT p1,PT p2);
PT setcode(PT p);
int visibility(PT p1,PT p2);
PT resetendpt(PT p1,PT p2);
void main()
{
    int gd=DETECT,v,gm;
    PT p1,p2,p3,p4,ptemp;
    clrscr();
    printf("\nEnter x1 and y1\n");
    scanf("%d %d",&p1.x,&p1.y);
    printf("\nEnter x2 and y2\n");
    scanf("%d %d",&p2.x,&p2.y);
    initgraph(&gd, &gm, "C://TURBOC3//BGI");
    drawwindow();
    delay(500);
    drawline(p1,p2);
    delay(500);
    cleardevice();
    delay(500);
    p1=setcode(p1);
    p2=setcode(p2);
    v=visibility(p1,p2);
    delay(500);
    switch(v)
    {
    case 0: drawwindow();
            delay(500);
            drawline(p1,p2);
            break;
    case 1: drawwindow();
            delay(500);
            break;
    case 2: p3=resetendpt(p1,p2);
            p4=resetendpt(p2,p1);
            drawwindow();
            delay(500);
            drawline(p3,p4);
            break;
    }
   
    delay(5000);
    closegraph();
}

void drawwindow()
{
    line(150,100,450,100);
    line(450,100,450,350);
    line(450,350,150,350);
    line(150,350,150,100);
}

void drawline(PT p1,PT p2)


{
    line(p1.x,p1.y,p2.x,p2.y);
}

PT setcode(PT p)    //for setting the 4 bit code


{
    PT ptemp;
   
    if(p.y<100)
        ptemp.code[0]='1';  //Top
    else
        ptemp.code[0]='0';
   
    if(p.y>350)
        ptemp.code[1]='1';  //Bottom
    else
        ptemp.code[1]='0';
       
    if(p.x>450)
        ptemp.code[2]='1';  //Right
    else
        ptemp.code[2]='0';
       
    if(p.x<150)
        ptemp.code[3]='1';  //Left
    else
        ptemp.code[3]='0';
   
    ptemp.x=p.x;
    ptemp.y=p.y;
    return(ptemp);
}

int visibility(PT p1,PT p2)


{
    int i,flag=0;
    for(i=0;i<4;i++)
    {
        if((p1.code[i]!='0') || (p2.code[i]!='0'))
            flag=1;
    }
    if(flag==0)
        return(0);
   
    for(i=0;i<4;i++)
    {
        if((p1.code[i]==p2.code[i]) && (p1.code[i]=='1'))
            flag='0';
    }
   
    if(flag==0)
        return(1);
   
    return(2);
}

PT resetendpt(PT p1,PT p2)


{
    PT temp;
    int x,y,i;
    float m,k;
   
    if(p1.code[3]=='1')
        x=150;
   
    if(p1.code[2]=='1')
        x=450;
   
    if((p1.code[3]=='1') || (p1.code[2]=='1'))
    {
        m=(float)(p2.y-p1.y)/(p2.x-p1.x);
        k=(p1.y+(m*(x-p1.x)));
        temp.y=k;
        temp.x=x;
       
        for(i=0;i<4;i++)
            temp.code[i]=p1.code[i];
       
        if(temp.y<=350 && temp.y>=100)
            return (temp);
    }
   
    if(p1.code[0]=='1')
        y=100;
   
    if(p1.code[1]=='1')
        y=350;
       
    if((p1.code[0]=='1') || (p1.code[1]=='1'))
    {
        m=(float)(p2.y-p1.y)/(p2.x-p1.x);
        k=(float)p1.x+(float)(y-p1.y)/m;
        temp.x=k;
        temp.y=y;
       
        for(i=0;i<4;i++)
            temp.code[i]=p1.code[i];
       
        return(temp);
    }
    else
        return(p1);
}

DRAWLINE
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void drawline (int x0, int y0, int x1, int y1)
{
 int dx, dy, p, x,y;
 dx=x1-x0;
 dy=y1-y0;
 x=x0;
 y=y0;
 p=2*dy-dx;
 while(x<x1){
  if(p>=0){
    putpixel(x,y,7);
    y=y+1;
    p=p+2*dy-2*dx;
  }
  else
  {
   putpixel(x,y,7);
   p=p+2*dy;
  }
  x=x+1;
 }
}
int main()
{
 int gd=DETECT, gm=0,error,x0,y0,x1,y1;
 initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
 printf("Enter co-ordinates of first point");
 scanf("%d %d", &x0, &y0);
 printf("Enter co-ordinates for second points");
 scanf("%d %d", &x1,&y1);
 drawline(x0, y0, x1, y1);
 getch();
 return 0;
 }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy