Computer Graphics in C,C++

Tuesday, 6 January 2009

Dotted line by using Bresenham’s algorithm of any slope

#include "graphics.h"
#include "stdlib.h"
#include "stdio.h"
#include "conio.h"
void main()
{
int gd=DETECT,M;
float dx,dy,x,y,p,m;
float x1,y1,x2,y2;
int thick,i,j,c=0;
clrscr();
initgraph(&gd,&M,"");
printf("\n\nEnter the values of x1,y1:");
scanf("%f%f",&x1,&y1);
printf("\n\nEnter the values of x2,y2:");
scanf("%f%f",&x2,&y2);
{
double dx,dy,x,y,p,m;
dx=abs(x2-x1);
dy=abs(y2-y1);
m=dy/dx;
x=x1; //Plot Starting point
y=y1;
putpixel(x,y,RED);
if(dx==0) //Vertical Line
{
while(y!=y2)
{
if(y1>y2) y--;
else y++;
if((int)y%2==0)
continue;
putpixel(x,y,3);
}
}

else if(dy==0) //Horizontal Line
{
while(x!=x2)
{
if(x1>x2) x--;
else x++;
if((int)x%2==0)
continue;
putpixel(x,y,3);
}
}

else if(m==1) //Slope 45 deg
{
while(x!=x2)
{
if(x1>x2) x--;
else x++;
if(y1>y2) y--;
else y++;
if((int)x%2==0)
continue;
putpixel(x,y,3);
}
}

else if(m>1) //Slope greater than 1
{
p=dy-2*dx;
while(y!=y2)
{
if(y1>y2) y--;
else y++;
if(p>0)
p=p-2*dx;
else
{
if(x1>x2) x--;
else x++;
p=p+2*(dy-dx);
}
if((int)y%2==0)
continue;

putpixel(x,y,3);
}
}
else //Slope less than 1
{
p=2*dy-dx;
while(x!=x2)
{
if(x1>x2) x--;
else x++;
if(p<0)
p=p+2*dy;
else
{
if(y1>y2) y--;
else y++;
p=p+2*(dy-dx);
}
if((int)x%2==0)
continue;
putpixel(x,y,3);
}
}
getch();
clearviewport();
}

Thick line(in terms of pixels) by using Bresenham’s algorithm of any slope.

#include "graphics.h"
#include "stdlib.h"
#include "stdio.h"
#include "conio.h"
void main()
{
int gd=DETECT,M;
float dx,dy,x,y,p,m;
float x1,y1,x2,y2;
int thick,i,j,c=0;
clrscr();
initgraph(&gd,&M,"");
printf("\n\nEnter the values of x1,y1:");
scanf("%f%f",&x1,&y1);
printf("\n\nEnter the values of x2,y2:");
scanf("%f%f",&x2,&y2);
dy=abs(y2-y1);
dx=abs(x2-x1);
m=(dy)/(dx);
c=y1-m*x1;

printf("THICKNESS OF LINE DESIRED(in pixels)... ");
scanf("%d",&thick);
clrscr();
if(m>1)
{
for(i=0,j=0;i<=thick;i++,j++)
{
if(c%2==0)
{
y1-=i;
y2-=i;
}
else
{
y1+=i;
y2+=i;
}
c++;
for(y=y1;y!=y2;y1>y2?y--:y++)
{
x=(y-c)/m;
putpixel(x,y,j);
}
}
}
else
{
for(i=0,j=0;i<=thick;i++,j++)
{
if(c%2==0)
{
x1-=i;
x2-=i;
}
else
{
x1+=i;
x2+=i;
}
c++;
for(x=x1;x!=x2;x1 {
y=m*x+c;
putpixel(x,y,j);
}
}
}
getch();
clearviewport();
}

Solid line by using Bresenham’s algorithm of any slope

#include "graphics.h"
#include "stdlib.h"
#include "stdio.h"
#include "conio.h"
void main()
{
int gd=DETECT,M;
float dx,dy,x,y,p,m;
float x1,y1,x2,y2;
clrscr();
initgraph(&gd,&M,"");
printf("\n\nEnter the values of x1,y1:");
scanf("%f%f",&x1,&y1);
printf("\n\nEnter the values of x2,y2:");
scanf("%f%f",&x2,&y2);
dx=abs(x2-x1);
dy=abs(y2-y1);
m=dy/dx;
x=x1; //Plot Starting point
y=y1;
putpixel(x,y,6);
if(dx==0) //Vertical Line
{
while(y!=y2)
{
if(y1>y2) y--;
else y++;
putpixel(x,y,3);
}
}

else if(dy==0) //Horizontal Line
{
while(x!=x2)
{
if(x1>x2) x--;
else x++;
putpixel(x,y,3);
}
}
else if(m==1) //Slope 45 deg
{
while(x!=x2)
{
if(x1>x2) x--;
else x++;
if(y1>y2) y--;
else y++;
putpixel(x,y,3);
}
}

else if(m>1) //Slope greater than 1
{
p=dy-2*dx;
while(y!=y2)
{
if(y1>y2) y--;
else y++;
if(p>0)
p=p-2*dx;
else
{
if(x1>x2) x--;
else x++;
p=p+2*(dy-dx);
}
putpixel(x,y,3);
}
}
else //Slope less than 1
{
p=2*dy-dx;
while(x!=x2)
{
if(x1>x2) x--;
else x++;
if(p<0)
p=p+2*dy;
else
{
if(y1>y2) y--;
else y++;
p=p+2*(dy-dx);
}
putpixel(x,y,3);
}
}
getch();
}

Solid line by using Bresenham’s algorithm of any slope

#include "graphics.h"
#include "stdlib.h"
#include "stdio.h"
#include "conio.h"
void main()
{
int gd=DETECT,M;
float dx,dy,x,y,p,m;
float x1,y1,x2,y2;
clrscr();
initgraph(&gd,&M,"");
printf("\n\nEnter the values of x1,y1:");
scanf("%f%f",&x1,&y1);
printf("\n\nEnter the values of x2,y2:");
scanf("%f%f",&x2,&y2);
dx=abs(x2-x1);
dy=abs(y2-y1);
m=dy/dx;
x=x1; //Plot Starting point
y=y1;
putpixel(x,y,6);
if(dx==0) //Vertical Line
{
while(y!=y2)
{
if(y1>y2) y--;
else y++;
putpixel(x,y,3);
}
}

else if(dy==0) //Horizontal Line
{
while(x!=x2)
{
if(x1>x2) x--;
else x++;
putpixel(x,y,3);
}
}
else if(m==1) //Slope 45 deg
{
while(x!=x2)
{
if(x1>x2) x--;
else x++;
if(y1>y2) y--;
else y++;
putpixel(x,y,3);
}
}

else if(m>1) //Slope greater than 1
{
p=dy-2*dx;
while(y!=y2)
{
if(y1>y2) y--;
else y++;
if(p>0)
p=p-2*dx;
else
{
if(x1>x2) x--;
else x++;
p=p+2*(dy-dx);
}
putpixel(x,y,3);
}
}
else //Slope less than 1
{
p=2*dy-dx;
while(x!=x2)
{
if(x1>x2) x--;
else x++;
if(p<0)
p=p+2*dy;
else
{
if(y1>y2) y--;
else y++;
p=p+2*(dy-dx);
}
putpixel(x,y,3);
}
}
getch();
}

Clip a line by using Cohen- Sutherland Algorithm

#include"stdio.h"
#include"graphics.h"
enum { TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 };
void lineclip(float x1,float y1,float x2,float y2,float xwmin,float ywmin,float xwmax,float ywmax );
int calcode (float x,float y,float xwmin,float ywmin,float xwmax,float ywmax);
main()
{

float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
printf("\n\n\tEnter the co-ordinates of Line :");
printf("\n\n\tX1 Y1 : ");
scanf("%f %f",&x1,&y1);
printf("\n\n\tX2 Y2 : ");
scanf("%f %f",&x2,&y2);
printf("\n\tEnter the co_ordinates of window :\n ");
printf("\n\txwmin , ywmin : ");
scanf("%f %f",&xwmin,&ywmin);
printf("\n\txwmax , ywmax : ");
scanf("%f %f",&xwmax,&ywmax);
clearviewport();
line(x1,y1,x2,y2);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
cleardevice();
lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );
getch();

}

void lineclip(float x1,float y1,float x2,float y2,float xwmin,float ywmin,float xwmax,float ywmax )
{
int code0,code1,codeout;
int accept = 0, done=0;

code0 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
code1 = calcode(x2,y2,xwmin,ywmin,xwmax,ywmax);

do{
if(!(code0 | code1))
{ accept =1 ; done =1; }
else
if(code0 & code1) { done = 1;}
else
{
float x,y;
if(code0)
codeout=code0;
else codeout=code1;
//codeout = code0 ? code0 : code1;
if(codeout & TOP)
{
x = x1 + (x2-x1)*(ywmax-y1)/(y2-y1);
y = ywmax;
}
else
if( codeout & BOTTOM)
{
x = x1 + (x2-x1)*(ywmin-y1)/(y2-y1);
y = ywmin;
}
else
if ( codeout & RIGHT)
{
y = y1+(y2-y1)*(xwmax-x1)/(x2-x1);
x = xwmax;
}
else
{
y = y1 + (y2-y1)*(xwmin-x1)/(x2-x1);
x = xwmin;
}
if( codeout == code0)
{
x1 = x; y1 = y;
code0=calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
}
else
{
x2 = x; y2 = y;
code1 = calcode(x2,y2,xwmin,ywmin,xwmax,ywmax);
}
}
} while( done == 0);

if(accept) line(x1,y1,x2,y2);

rectangle(xwmin,ywmin,xwmax,ywmax);

getch();

}


int calcode (float x,float y,float xwmin,float ywmin,float xwmax,float ywmax)
{
int code =0;

if(y> ywmax)
code =TOP;
else if( y code = BOTTOM;
else if(x > xwmax)
code = RIGHT;
else if ( x< xwmin)
code = LEFT;

return(code);
}

Function to draw the axis on the screen

#include"stdio.h"
#include"graphics.h"
#include"conio.h"
#include"math.h"
#include"stdlib.h>"
void main()
{

int I=DETECT,M,g;/*/

void xyaxis()
{
int h,k,y1=0,y2,x1=20,x2,i;
float r,x,y,t;
char s[20];


line(280,0,280,480);
line(0,240,560,240);
i=12;
while(y1<480)
{

outtextxy(266,y1-2,itoa(i,s,10));
i--;
y2=y1;
line(275,y1,285,y2);
y1=y1+20;

}
i=-13;
while(x1<560)
{
outtextxy(x1-5,225,itoa(i,s,10));
i++;
x2=x1;
line(x1,235,x2,245);
x1=x1+20;
}
getch();

}

Program to draw a triangle by using any line Algorithm

#include"stdio.h"
#include"conio.h"
#include"graphics.h"
#include"math.h"
void dline(float,float,float,float);
void main()
{
int i =DETECT,M,j;
float x1,y1,x2,y2,x3,y3;
clrscr();
initgraph(&i,&M,"");
printf("Enter the values of x1,y1,x2,y2,x3,y3::");
scanf("%f%f%f%f%f%f",&x1,&y1,&x2,&y2,&x3,&y3);
dline(x1,y1,x3,y3);

dline(x1,y1,x2,y2);
dline(x2,y2,x3,y3);
// dline(x2,y1,x2,y2);
getch();
}
void dline(float x1,float y1,float x2,float y2)
{
float dx,dy,xend,m,b,x,y,steps,xinc,yinc,k;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xinc = dx/steps;
yinc = dy/steps;
x=x1;y=y1;
putpixel(x,y,3);
for(k=1;k<=steps;k++)
{
x=x+xinc;
y=y+yinc;
putpixel(x,y,5);
}
}

Program to draw a triangle by using any line Algorithm

#include"stdio.h"
#include"conio.h"
#include"graphics.h"
#include"math.h"
void dline(float,float,float,float);
void main()
{
int i =DETECT,M,j;
float x1,y1,x2,y2,x3,y3;
clrscr();
initgraph(&i,&M,"");
printf("Enter the values of x1,y1,x2,y2,x3,y3::");
scanf("%f%f%f%f%f%f",&x1,&y1,&x2,&y2,&x3,&y3);
dline(x1,y1,x3,y3);

dline(x1,y1,x2,y2);
dline(x2,y2,x3,y3);
// dline(x2,y1,x2,y2);
getch();
}
void dline(float x1,float y1,float x2,float y2)
{
float dx,dy,xend,m,b,x,y,steps,xinc,yinc,k;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xinc = dx/steps;
yinc = dy/steps;
x=x1;y=y1;
putpixel(x,y,3);
for(k=1;k<=steps;k++)
{
x=x+xinc;
y=y+yinc;
putpixel(x,y,5);
}
}

Program to draw an arc

#include"stdio.h"
#include"graphics.h"
#include"conio.h"
#include"math.h"
#define PI 3.14

void main()
{
int h,k;
float r,x,y,t,t1,t2,j;
int i=DETECT,M,g;
clrscr();
initgraph(&i,&M," ");
printf("Enter the values of h,k,r,t1,t2:");
scanf("%d%d%f%f%f",&h,&k,&r,&t1,&t2);

if(t1 {
for(j=0;t1<=t2;j++)
{ delay(100);
x=r*cos((PI/180)*(-j));
y=r*sin((PI/180)*(-j));
putpixel(h+x,y+k,1);
t1++;
}
}
else
{
for(j=0;t2<=t1;j++)
{ delay(100);
x=r*cos((PI/180)*(j));
y=r*sin((PI/180)*(j));
putpixel(h+x,y+k,1);
t2++;
}
}


getch();
}

Program to draw a sector

#include"stdio.h"
#include"graphics.h"
#include"conio.h"
#include"math.h"
#define PI 3.14

void main()
{
int h,k;
float r,x,y,t,t1,t2;
int i=DETECT,M,g;
clrscr();
initgraph(&i,&M," ");
printf("Enter the values of h,k,r,t1,t2:");
scanf("%d%d%f%f%f",&h,&k,&r,&t1,&t2);
for(t=t1;t<=t2;t++)
{ delay(100);
x=r*cos((PI/180)*t);
y=r*sin((PI/180)*t);
if(t1>=0&&t2<=45)
putpixel(h+x,y+k,1);
else if(t1>45&&t2<=90)
putpixel(h+x,-y+k,2);
else if(t1>90&&t2<=135)
putpixel(-y+h,x+k,3);
else if(t1>135&&t2<=180)
putpixel(-y+h,-x+k,4);
else if(t1>180&&t2<=225)
putpixel(-x+h,-y+k,5);
else if(t1>225&&t2<=270)
putpixel(-x+h,y+k,6);
else if(t1>270&&t2<=315)
putpixel(y+h,-x+k,7);
else
putpixel(y+h,x+k,8);
}
line(h,k,r*cos((PI/180)*t1)+h,r*sin((PI/180)*t1)+k);
line(h,k,r*cos((PI/180)*t2)+h,r*sin((PI/180)*t2)+k);
getch();
}

Rectangle by polynomial method

#include"stdio.h"
#include"conio.h"
#include"graphics.h"
#define PI 3.1416
#include
void main()
{
int gd=DETECT,M;
int h,k,x,y;
float a,b;
clrscr();
initgraph(&gd,&M,"");
printf("Enter h,k,a,b;");
scanf("%d%d%f%f",&h,&k,&a,&b);
for(x=0;x {

y=b*sqrt((x*x/a*a)-1);
putpixel(x+h,y+k,4);
putpixel(-x+h,y+k,4);
putpixel(x+h,-y+k,4);
putpixel(-x+h,-y+k,4);
}
getch();
}

Rectangle by trigonometric method.

#include"stdio.h"
#include"conio.h"
#include"graphics.h"
#define PI 3.1416
#include
void main()
{
int gd=DETECT,M;
int h,k,x,y,t;
float a,b;
clrscr();
initgraph(&gd,&M,"");
printf("Enter h,k,a,b;");
scanf("%d%d%f%f",&h,&k,&a,&b);
for(t=0;t<90;t++)
{
x=a*cos((PI/180)*t);
y=b*sin((PI/180)*t);
putpixel(x+h,y+k,4);
putpixel(-x+h,y+k,4);
putpixel(x+h,-y+k,4);
putpixel(-x+h,-y+k,4);
}
getch();
}
-------------
Sandeep Kumar