SlideShare uma empresa Scribd logo
1 de 70
SHRI ATMANAND JAIN INSTITUTE OF MANAGEMENT AND
TECHNOLOGY
PRACTICAL FILE OF
PROGRAMMING IN C-GRAPHICS
SUBMITTED BY
Shubham Kanojia
MCA 4th SEMESTER
Roll No. 1511
HEAD OF DEPTT. SUBMITTED TO EXAMINER
Mrs. Rinkey Chaudhary Mrs. Rinkey Chaudhary
(HOD of, MCA) (HOD of, MCA)
INDEX
Sr.No PARTICULARS SIGNATURE
1. Program to draw a line using polynomial method
2. Program to draw a line using DDA algorithm
3. Program to draw a line using Bresenham’s Algorithm
4. Program to draw circle using Polynomial method
5. Program to draw circle using Bresenham’s Algorithm
6. Program to draw a circle using trigonometric method
7. Program to draw a circle using mid-point method
8. Program to draw a ellipse using polynomial method
9. Program to draw a ellipse using Bresenham’s Algorithm
10. Program to draw a ellipse using trigonometric method
11. Program to draw a ellipse using mid-point method
12. Program to implement 2-D Translation
13. Program to implement 2-D Rotation
14. Program to implement 2-D Scaling
15. Program to draw sine curve
16. Program to draw X-Y axis
17. Program to print HELLO using line function
18. Program to print random lines on screen
19. Program to find whether a point is inside or outside the polygon
20. Program to implement mapping from window to viewport
21. Program to plot an ARC using trigonometric method
22. Program to implement 2-D reflection
23. Program to implement 2-D shearing
24. Program to implement line clipping using Cohan Sutherland
algorithm
25. Program to implement line clipping using Liang Barsky method
26. Program of Boundary fill algorithm
27. Program of Flood fill algorithm
1. Programto draw a line using polynomial method :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int main()
{
float m;
int i,gd=DETECT,gm,x1,y1,x2,y2,x,y,dy,dx,b;
printf("enter the x coordinates:");
scanf("%d%d",&x1,&x2);
printf("enter the y coordinates");
scanf("%d%d",&y1,&y2);
initgraph(&gd,&gm,"c:tcbgi");
dx=x2-x1;
dy=y2-y1;
m=dy/dx;
b=y1-(m*x1);
x=x1;
y=y1;
if(m<1)
while(x<=x2)
{
putpixel(x, y+0.5,WHITE);
x++;
y=(m*x)+b;
}
else
{
while(y<=y2)
{
putpixel(x+0.5,y,WHITE);
y++;
x=(y-b)/m;
}
}
getch();
closegraph();
}
Output :
2. Programto draw a line using DDA algorithm :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int main()
{
float x,y,x1,y1,x2,y2,dx,dy,pixel;
int i,gd=DETECT,gm;
printf("enter the value of x1:");
scanf("%f",&x1);
printf("enter the value of y1:");
scanf("%f",&y1);
printf("enter the value of x2:");
scanf("%f",&x2);
printf("enter the value of y2:");
scanf("%f",&y2);
initgraph(&gd,&gm,"c:tcbgi");
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
pixel=dx;
else
pixel=dy;
dx=dx/pixel;
dy=dy/pixel;
x=x1;
y=y1;
i=1;
while(i<=pixel)
{
putpixel(x,y,WHITE);
x=x+dx;
y=y+dx;
i=i+1;
delay(100);
}
getch();
closegraph();
}
Output:
3. Program to draw a line using Bresenham’s algorithm:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int main()
{
float x,y,x1,y1,x2,y2,dx,dy;
int i,gd=DETECT,gm,p,end;
printf("enter the value of x1:");
scanf("%f",&x1);
printf("enter the value of y1:");
scanf("%f",&y1);
printf("enter the value of x2:");
scanf("%f",&x2);
printf("enter the value of y2:");
scanf("%f",&y2);
initgraph(&gd,&gm,"c:tcbgi");
dx=abs(x1-x2);
dy=abs(y1-y2);
p=2*dy-dx;
if(x1>x2)
{
x=x2;
y=y2;
end=x1;
}
else
{
x=x1;
y=y1;
end=x2;
}
putpixel(x,y,WHITE);
while(x<end)
{
x=x+1;
if(p<0)
{
p=p+2*dy;
}
else
{
y=y+1;
p=p+2*(dy-dx);
}
putpixel(x,y,WHITE);
}
getch();
closegraph();
}
Output :
4. Programto draw a circle using Polynomial method :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int main()
{
int gd=DETECT,gm,xc,yc,xend,r,x,y;
initgraph(&gd,&gm,"c:tcbgi");
printf("***draw circle using polunomial method ***n");
printf("enter x centre and y centret");
scanf("%d%d",&xc,&yc);
printf("enter the radius t");
scanf("%d",&r);
x=0;
xend=r/sqrt(2);
while(x<xend)
{
y=sqrt(r*r-x*x);
putpixel(xc+x,yc+y,WHITE);
putpixel(xc+y,yc+x,WHITE);
putpixel(xc-x,yc-y,WHITE);
putpixel(xc-y,yc-x,WHITE);
putpixel(xc+x,yc-y,WHITE);
putpixel(xc+y,yc-x,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc-y,yc+x,WHITE);
x++;
}
getch();
closegraph();
}
Output :
5. Programto draw a circle using Bresenham’s algorithm :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int main()
{
int gd=DETECT,gm,xc,yc,r,x,y,pk;
initgraph(&gd,&gm,"c:tcbgi");
printf("*** Bresenham's circle drawing algorithm***");
printf("enter the vlaue of xc t");
scanf("%d",&xc);
printf("enter the vlaue of yc t");
scanf("%d",&yc);
printf("enter the radius t");
scanf("%d",&r);
x=0;
y=r;
putpixel(xc+x,yc-y,WHITE);
pk=3-(2*r);
for(x=0;x<=y;x++)
{
if(pk<0)
{
y=y;
pk=(pk+(4*x)+6);
}
else
{
y=y-1;
pk=pk+((4*(x-y)+10));
}
putpixel(xc+x,yc+y,WHITE);
putpixel(xc+y,yc+x,WHITE);
putpixel(xc-x,yc-y,WHITE);
putpixel(xc-y,yc-x,WHITE);
putpixel(xc+x,yc-y,WHITE);
putpixel(xc+y,yc-x,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc-y,yc+x,WHITE);
delay(100);
}
getch();
closegraph();
}
Output :
6. Programto draw a circle using trigonometric method :
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void put8pixel(int,int,int,int);
void main()
{
int gd=DETECT,gm;
int x,y,x1,y1,r,h,k,theta;
float n=3.14159/180;
printf("*Trignometric Method to draw a circle*n");
printf("nenter the x and y coordinates:-");
scanf("%d%d",&h,&k);
printf("nenter the radius:-n");
scanf("%d",&r);
initgraph(&gd,&gm,"c:tcbgi");
for(theta=0;theta<=45;theta++)
{
x1=r*cos(theta*n);
y1=r*sin(theta*n);
x= x1+0.5;
y=y1+0.5;
put8pixel(x,y,h,k);
}
outtextxy(115,70,"circle using Trigonometric method");
getch();
closegraph();
}
void put8pixel(int x,int y,int h,int k)
{
putpixel(x+h,y+k,WHITE);
putpixel(x+h,-y+k,WHITE);
putpixel(-x+h,y+k,WHITE);
putpixel(-x+h,-y+k,WHITE);
putpixel(y+h,x+k,WHITE);
putpixel(y+h,-x+k,WHITE);
putpixel(-y+h,x+k,WHITE);
putpixel(-y+h,-x+k,WHITE);
}
Output :
7. Programto draw a circle using mid-point method :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT, gm;
int xc,yc,x,y,r,f;
initgraph(&gd,&gm,"c:tcbgi");
printf("Enter x-centre, y-centre and radius");
scanf("%d%d%d",&xc,&yc,&r);
x=0;
y=r;
f=1-r;
while(x<=y)
{
if(f<0)
{
f=f+(3+2*x);
x++;
}
else
{
f=f+(2*x-2*y+5);
x++;
y--;
}
putpixel(x+xc, y+yc, WHITE);
putpixel(y+xc, x+yc,WHITE);
putpixel(-x+xc, y+yc, WHITE);
putpixel(-y+xc, x+yc, WHITE);
putpixel(-x+xc, -y+yc, WHITE);
putpixel(-y+xc, -x+yc, WHITE);
putpixel(x+xc,-y+yc,WHITE);
putpixel(y+xc,-x+yc,WHITE);
}
getch();
closegraph();
}
Output :
8. Program to draw a ellipse using polynomial method :
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void plot4pixels(int,int,int,int);
void main()
{
int gd=DETECT,gm;
int x,y,r,i,h,k,a,b;
printf("n*Polynomial method to draw an ellipse*n");
printf("nenter the x and y coordinates:-n");
scanf("%d%d",&h,&k);
printf("nenter the first radius:-n");
scanf("%d",&a);
printf("nenter the second radius:-n");
scanf("%d",&b);
x=0;
y=b;
initgraph(&gd,&gm,"c:tcbgi");
while(x<a)
{
plot4pixels(x,y,h,k);
x++;
y=b*sqrt(((a*a)-(x*x*1.0))/(a*a));
}
plot4pixels(x,y,h,k);
setcolor(WHITE);
outtextxy(100,60,"ellipse using polynomial method");
getch();
}
void plot4pixels(int x,int y,int h,int k)
{
putpixel(x+h,y+k,WHITE);
putpixel(x+h,-y+k,WHITE);
putpixel(-x+h,y+k,WHITE);
putpixel(-x+h,-y+k,WHITE);
}
Output :
9. Programto draw a ellipse using bresenham’s algorithm :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void bresenhamellipse(int,int,int,int);
void ellipseplotpoints(int,int,int,int,int);
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:tcbgi");
bresenhamellipse(300,300,30,40);
getch();
closegraph();
}
void bresenhamellipse(int xcenter,int ycenter,int rx,int ry)
{
// xcenter and ycenter are the centre points.
//rx and ry ,in these two any one can be the following,semi major axis and semi minor
axis.
long rx2=rx*rx,ry2=ry,
x=0,
y=ry,
twory2=2*ry2,
tworx2=2*rx2,
p=0;
ellipseplotpoints(xcenter,ycenter,x,y,WHITE);
p=((int)(ry2*ry)+(.25*rx2));
while(twory2*x<tworx2*y)
{
x++;
if(p<0)
p+=(int)twory2*x+ry2;
else
{
y--;
p+=(int)twory2*ry2-ry2-tworx2*y;
}
ellipseplotpoints(xcenter,ycenter,x,y,WHITE);
}
//for region 2
p=(int)(ry2*(x+.5)*(x+.5)*(x+.5)+rx2*(y-1)*(y-1)-rx2*ry2);
while(y>0)
{
y--;
if(p>0)
p+=rx2-tworx2*y;
else
{
x++;
p+=rx2+twory2*x-tworx2*y;
}
ellipseplotpoints(xcenter,ycenter,x,y,WHITE);
}
}
void ellipseplotpoints(int xcenter,int ycenter,int x,int y,int color)
{
putpixel(xcenter+x,ycenter+y,WHITE);
putpixel(xcenter-x,ycenter+y,WHITE);
putpixel(xcenter+x,ycenter-y,WHITE);
putpixel(xcenter-x,ycenter-y,WHITE);
}
Output :
10. Programto draw a ellipse using trigonometric method :
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void put4pixel(int,int,int,int);
void main()
{
int x,y,x1,y1,a,b,h,k,theta;
int gd=DETECT,gm;
float p=3.14159/180;
printf("*Trignometric method to draw an ellippse*n");
printf("nenter the x and y coordinates:-n");
scanf("%d%d",&h,&k);
printf("nenter the first radius:-n");
scanf("%d",&a);
printf("nenter the second radius:-n");
scanf("%d",&b);
initgraph(&gd,&gm,"c:tcbgi");
for(theta=0;theta<=90;theta++)
{
x1=a*cos(theta*p);
y1=b*sin(theta*p);
x=x1+0.5;
y=y1+0.5;
put4pixel(x,y,h,k);
}
outtextxy(100,60,"ellipse using Trigonometric method");
getch();
closegraph();
}
void put4pixel(int x,int y,int h,int k)
{
putpixel(x+h,y+k,WHITE);
putpixel(x+h,-y+k,WHITE);
putpixel(-x+h,y+k,WHITE);
putpixel(-x+h,-y+k,WHITE);
}
Output :
11. Programto draw a ellipse using mid-point method :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void disp();
float x,y;
int xc,yc;
void main()
{
int gd=DETECT,gm;
int a,b;
float p1,p2;
clrscr();
initgraph(&gd,&gm,"c:tcbgi");
printf("enter the xc and yc,a and b radius");
scanf("%d%d",&xc,&yc);
scanf("%d%d",&a,&b);
x=0;y=b;
disp();
p1=(b*b)-(a*a*b)+(a*a)/4;
while((2.0*b*b*x)<=(2.0*a*a*y))
{
x++;
if(p1<=0)
p1=p1+(2.0*b*b*x)+(b*b);
else
{
y--;
p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y);
}
disp();
x=-x;
disp();
x=-x;
}
x=a;
y=0;
disp();
p2=(a*a)+2.0*(b*b*a)+(b*b)/4;
while((2.0*b*b*x)>(2.0*a*a*y))
{
y++;
if(p2>0)
p2=p2+(a*a)-(2.0*a*a*y);
else
{
x--;
p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a);
}
disp();
y=-y;
disp();
y=-y;
}
getch();
closegraph();
}
void disp()
{
putpixel(xc+x,yc+y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc+x,yc-y,WHITE);
putpixel(xc+x,yc-y,WHITE);
}
Output :
12. Programto implement 2-D 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:tcbgi");
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();
}
Output :
13. Programto implement 2-D 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:tcbgi");
line(x1,y1,x2,y2);
printf("nnn[ 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();
}
Output :
14. Programto implement a 2-D scaling :
#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:tcbgi");
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();
}
Output :
15. Programto draw sine curve :
#include <conio.h>
#include <math.h>
#include <graphics.h>
#include <dos.h>
int main()
{
int gd = DETECT, gm;
int angle = 0;
double x, y;
initgraph(&gd, &gm, "C:TCBGI");
line(0, getmaxy() / 2, getmaxx(), getmaxy() / 2);
/* generate a sine wave */
for(x = 0; x < getmaxx(); x+=3) {
/* calculate y value given x */
y = 50*sin(angle*3.141/180);
y = getmaxy()/2 - y;
/* color a pixel at the given position */
putpixel(x, y, 15);
delay(100);
/* increment angle */
angle+=5;
}
getch();
/* deallocate memory allocated for graphics screen */
closegraph();
return 0;
}
Output :
16. Program to draw X-Y axis :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int gd=DETECT,gm;
float xm,ym;
initgraph(&gd,&gm,"c:tcbgi");
//calculating the mid point of screen
xm=getmaxx()/2;
ym=getmaxy()/2;
line(xm,0,xm,2*ym); //creating y axis
line(0,ym,2*xm,ym); //creating x axis
outtextxy(2*xm-60,ym+5,"X-axis");
outtextxy(xm+4,10,"Y-axis");
getch();
closegraph();
}
Output :
17. Programto print HELLO using line function :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x,y,x1,y1;
initgraph(&gd,&gm,"c:tcbgi");
//h
line(100,100,100,200);
line(100,150,150,150);
line(150,100,150,200);
//e
line(200,100,250,100);
line(200,100,200,200);
line(200,200,250,200);
line(200,150,250,150);
//l
line(300,100,300,200);
line(300,200,350,200);
//l
line(400,100,400,200);
line(400,200,450,200);
//o
line(500,100,500,200);
line(500,100,550,100);
line(500,200,550,200);
line(550,100,550,200);
getch();
closegraph();
}
Output :
18. Programto print random lines on screen:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int gd=DETECT,gm;
int i,j,xxmax,yymax,xmax,ymax,x,y,xx,yy;
initgraph(&gd,&gm,"c:tcbgi");
xmax=getmaxx();
ymax=getmaxy();
xxmax=getmaxx();
yymax=getmaxy();
while(!kbhit())
{
for(i=0;i<=100;i++)
{
x=random(xmax);
y=random(ymax);
xx=random(xxmax);
yy=random(yymax);
for(j=1;j<=5;j++)
{
setcolor(WHITE);
line(x,y,xx,yy);
}
}
}
getch();
closegraph();
}
resorecrtmode():
}
Output :
19. Programto find whether a point is inside or outside the polygon :
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm,errorcode;
float x[10],y[10],x1,y1,c[10],xm,ym;
int flag,n,i,j;
initgraph(&gd,&gm,"c:tcbgi");
printf("enter the number of vertices of polygon");
scanf("%d",&n);
printf("n enter the verticesn");
printf("x[i] and y[i]n");
for(i=0;i<n;i++)
{
scanf("%f%f",&x[i],&y[i]);
}
printf("n enter the point to be checkedn");
scanf("%f%f",&x1,&y1);
xm=getmaxx()/2;
ym=getmaxx()/2;
line(xm,0,xm,2*ym);
line(0,ym,2*xm,ym);
for(i=0;i<n-1;i++)
{
circle(x[i]+xm,(-y[i]+ym),2);
line(x[i]+xm,(-y[i]+ym),x[i+1]+xm,(-y[i+1]+ym));
}
circle(x[n-1]+xm,(-y[n-1]+ym),2);
line(x[n-1]+xm,(-y[n-1]+ym),x[0]+xm,(-y[0]+ym));
circle(x1+xm,-y1+ym,2);
for(i=0;i<n;i++)
c[i]=((x[i+1]-x[i])*(y1-y[i]))-((y[i+1]-y[i])*(x1-x[i]));
c[n-1]=((x[0]-x[n-1])*(y1-y[n-1]))-((y[0]-y[n-1])*(x1-x[n-1]));
flag=0;
for(i=0;i<n;i++)
{
if(c[i]>0)
flag=1;
if(c[i]<0)
{
flag=0;
break;
}
}
if(flag==1)
printf("n point is inside the polygon");
if(flag==0)
printf("n point outside the polygon");
getch();
}
Output :
20. Program to implement mapping from window to viewport :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void main()
{
float xwmin,xwmax,ywmax,ywmin;
float xvmin,xvmax,yvmax,yvmin;
float x[10],y[10],yv,xv,sx,sy;
int gd=DETECT,gm,i;
clrscr();
printf("n enter window port coordinates:n(xwmin,ywmin,xwmax,ywmax): ");
scanf("%f%f%f%f",&xwmin,&ywmin,&xwmax,&ywmax);
printf("n enter view port coordinates:n(xvmin,yvmin,xvmax,yvmax): ");
scanf("%f%f%f%f",&xvmin,&yvmin,&xvmax,&yvmax);
printf("n enter vertices for triangle: ");
for(i=0;i < 3;i++)
{
printf("n enter(x%d,y%d):",i,i);
scanf("%f%f",&x[i],&y[i]);
}
sx=((xvmax-xvmin)/(xwmax-xwmin));
sy=((yvmax-yvmin)/(ywmax-xwmin));
initgraph(&gd,&gm,"c:tcbgi");
outtextxy(80,30,"window port");
rectangle(xwmin,ywmin,xwmax,ywmax);
for(i=0;i <2;i++)
{
line(x[i],y[i],x[i+1],y[i+1]);
}
line(x[2],y[2],x[0],y[0]);
getch();
cleardevice();
for(i=0;i <3;i++)
{
x[i]=xvmin+((x[i]-xwmin)*sx);
y[i]=yvmin+((y[i]-ywmin)*sy);
}
outtextxy(150,10,"view port");
rectangle(xvmin,yvmin,xvmax,yvmax);
for(i=0;i <2;i++)
{
line(x[i],y[i],x[i+1],y[i+1]);
}
line(x[2],y[2],x[0],y[0]);
getch();
}
Output :
21. Programto plot an ARC using trigonometric method :
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<graphics.h>
#define pi 3.14
void main()
{
int gd=DETECT,gm,i,ch,errorcode;
float a,b,h,k,x,y,theta,theta1;
initgraph(&gd,&gm,"c:tcbgi");
printf("n enter the coordinates of arc center h and k");
scanf("%f%f",&h,&k);
printf("n step size");
scanf("%d",&i);
printf("n enter the length of major and minor axis");
scanf("%f%f",&a,&b);
printf("n enter the starting and ending angle");
scanf("%f%f",&theta,&theta1);
errorcode=graphresult();
//if(errorcode!=grOk)
//{
//printf("graphics errorn");
//getch();
//exit(1);
//}
while(theta<theta1)
{
x=a*cos((pi*theta)/180)+h;
y=b*sin((pi*theta)/180)+k;
putpixel(x,y,WHITE);
theta=theta+i;
}
getch();
}
Output :
22. Programto implement 2-D Reflection:
#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm;
int i,xmid,ymid,x1,y1,x2,y2,x3,y3,x,y,dy,dx,p,gap=50,temp,option;
int x1dash,x2dash,x3dash,y1dash,y2dash,y3dash;
double theta;
float m;
char str[5];
clrscr();
initgraph(&gd,&gm,"..bgi");
printf("Enter first co-ords of the trianglen");
scanf("%d %d",&x1,&y1);
printf("Enter second co-ords of the trianglen");
scanf("%d %d",&x2,&y2);
printf("Enter third co-ords of the trianglen");
scanf("%d %d",&x3,&y3);
xmid= getmaxx()/2;
ymid= getmaxy()/2;
line(5,ymid,getmaxx()-5,ymid);
line(xmid+3,5,xmid+3,getmaxy()-5);
for( i= xmid+gap;i<getmaxx()-5;i=i+gap)
{
outtextxy(i,ymid-3,"|");
itoa(i-xmid,str,10);
outtextxy(i,ymid+3,str);
}
for( i= ymid-gap;i>5;i=i-gap)
{
outtextxy(xmid,i,"-");
itoa(ymid-i,str,10);
outtextxy(xmid+5,i,str);
}
for( i= xmid-gap;i>5;i=i-gap)
{
outtextxy(i,ymid-3,"|");
itoa(-(xmid-i),str,10);
outtextxy(i-6,ymid+3,str);
}
for( i= ymid+gap;i<getmaxy()-5;i=i+gap)
{
outtextxy(xmid,i,"-");
itoa(-(i-ymid),str,10);
outtextxy(xmid+8,i,str);
}
line(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
line(x2+xmid,ymid-y2,x3+xmid,ymid-y3);
line(x3+xmid,ymid-y3,x1+xmid,ymid-y1);
setcolor(255);
printf("Reflection about n");
printf("X axis : 1n");
printf("Y axis : 2n");
printf("X=Y axis : 3n");
printf(" Enter the option (1-3):");
scanf("%d",&option);
switch( option)
{
case 1: y1=-y1; y2=-y2;y3=-y3; break;
case 2: x1=-x1;x2=-x2;x3=-x3;break;
case 3: y1=-y1; y2=-y2;y3=-y3;
theta= ((double) 90 *3.14f )/(double)180;
x1dash=x1*cos(theta)-y1*sin(theta);
x2dash=x2*cos(theta)-y2*sin(theta);
x3dash=x3*cos(theta)-y3*sin(theta);
y1dash=x1*sin(theta)+y1*cos(theta);
y2dash=x2*sin(theta)+y2*cos(theta);
y3dash=x3*sin(theta)+y3*cos(theta);
x1=x1dash;x2=x2dash;x3=x3dash;
y1=y1dash;y2=y2dash;y3=y3dash;
}
line(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
line(x2+xmid,ymid-y2,x3+xmid,ymid-y3);
line(x3+xmid,ymid-y3,x1+xmid,ymid-y1);
getch();
closegraph();
}
Output :
23. Programto implement 2-D Shearing :
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm,option,xref,yref;
int i,maxx,maxy,x1,y1,x2,y2,x3,y3,x4,y4,gap=50;
float shx=0.0,shy=0.0;
char str[5];
clrscr();
initgraph(&gd,&gm,"..bgi");
printf("enter the endpoints of the top of the rectangle (x1,y1) & (x2,y2):");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
printf("enter the endpoints of bottom of the rectangle (x3,y3) & (x4,y4)");
scanf("%d%d%d%d",&x3,&y3,&x4,&y4);
printf(" Enter the axis to shearn");
printf(" 1 - X axis Shearn");
printf(" 2 - Y axis shearn");
scanf("%d",&option );
if(option==1)
{
printf("enter the value for x-axis shear( can be fraction too):");
scanf("%f",&shx);
}
else
{
printf("enter the value for y-axis shear( can be fraction too):");
scanf("%f",&shy);
}
clearviewport();
maxx= getmaxx();
maxy=getmaxy();
line(3,maxy-1,maxx-5,maxy-1);
line(5,5,5,maxy-3);
for( i= 0;i<maxx-5;i=i+gap) // code to display co-ordinates
{
outtextxy(i+3,maxy-7,"|");
itoa(i,str,10);
outtextxy(i,maxy-10,str);
}
for( i= maxy;i>0;i=i-gap)
{
outtextxy(3,i,"-");
itoa(maxy-i,str,10);
outtextxy(9,i,str);
}
setcolor(WHITE); // drawing rectangle using endpoints
line(x1,maxy-y1,x2,maxy-y2);
line(x3,maxy-y3,x4,maxy-y4);
line(x1,maxy-y1,x3,maxy-y3);
line(x2,maxy-y2,x4,maxy-y4);
outtextxy(10,10,"hit any key to see the shearing effect" );
getch();
setcolor(WHITE); // to hide the rectangle drawn
line(x1,maxy-y1,x2,maxy-y2);
line(x3,maxy-y3,x4,maxy-y4);
line(x1,maxy-y1,x3,maxy-y3);
line(x2,maxy-y2,x4,maxy-y4);
setcolor(WHITE); // to redraw the rectangle
if(option==1)
{
// shearing about x axis so only points x1 and x2 need to be recomputed
line(x1+shx*y1,maxy-y1,x2+shx*y2,maxy-y2);
line(x3,maxy-y3,x4,maxy-y4);
line(x1+shx*y1,maxy-y1,x3,maxy-y3);
line(x2+shx*y2,maxy-y2,x4,maxy-y4);
}
else
{
// shearing about y axis so only points y2 and y4 need to be recomputed
line(x1,maxy-y1,x2,maxy-(y2+shy*x2));
line(x3,maxy-y3,x4,maxy-(y4+shy*x4));
line(x1,maxy-y1,x3,maxy-y3);
line(x2,maxy-(y2+shy*x2),x4,maxy-(y4+shy*x4));
}
getch();
closegraph();
}
Output :
24. Programto implement line clipping using CohanSutherland algorithm
:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT, gm;
float i,xmax,ymax,xmin,ymin,x1,y1,x2,y2,m;
float start[4],end[4],code[4];
clrscr();
initgraph(&gd,&gm,"c:tcbgi");
printf("ntEnter the bottom-left coordinate of viewport: ");
scanf("%f %f",&xmin,&ymin);
printf("ntEnter the top-right coordinate of viewport: ");
scanf("%f %f",&xmax,&ymax);
printf("nEnter the coordinates for starting point of line: ");
scanf("%f %f",&x1,&y1);
printf("nEnter the coordinates for ending point of line: ");
scanf("%f %f",&x2,&y2);
for(i=0;i <4;i++)
{
start[i]=0;
end[i]=0;
}
m=(y2-y1)/(x2-x1);
if(x1 <xmin) start[0]=1;
if(x1 >xmax) start[1]=1;
if(y1 >ymax) start[2]=1;
if(y1 <ymin) start[3]=1;
if(x2 <xmin) end[0]=1;
if(x2 >xmax) end[1]=1;
if(y2 >ymax) end[2]=1;
if(y2 <ymin) end[3]=1;
for(i=0;i <4;i++)
code[i]=start[i]&&end[i];
if((code[0]==0)&&(code[1]==0)&&(code[2]==0)&&(code[3]==0))
{
if((start[0]==0)&&(start[1]==0)&&(start[2]==0)&&(start[3]==0)&&(end[0]==0)&&(en
d[1]==0)&&(end[2]==0)&&(end[3]==0))
{
cleardevice();
printf("nttThe line is totally visiblenttand not a clipping candidate");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
}
else
{
cleardevice();
printf("nttLine is partially visible");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
if((start[2]==0)&&(start[3]==1))
{
x1=x1+(ymin-y1)/m;
y1=ymin;
}
if((end[2]==0)&&(end[3]==1))
{
x2=x2+(ymin-y2)/m;
y2=ymin;
}
if((start[2]==1)&&(start[3]==0))
{
x1=x1+(ymax-y1)/m;
y1=ymax;
}
if((end[2]==1)&&(end[3]==0))
{
x2=x2+(ymax-y2)/m;
y2=ymax;
}
if((start[1]==0)&&(start[0]==1))
{
y1=y1+m*(xmin-x1);
x1=xmin;
}
if((end[1]==0)&&(end[0]==1))
{
y2=y2+m*(xmin-x2);
x2=xmin;
}
if((start[1]==1)&&(start[0]==0))
{
y1=y1+m*(xmax-x1);
x1=xmax;
}
if((end[1]==1)&&(end[0]==0))
{
y2=y2+m*(xmax-x2);
x2=xmax;
}
clrscr();
cleardevice();
printf("nttAfter clippling:");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
}
}
else
{
clrscr();
cleardevice();
printf("nLine is invisible");
rectangle(xmin,ymin,xmax,ymax);
}
getch();
closegraph();
}
Output :
25. Programto implement line clipping using Liang Barskymethod :
#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int gd, gm ;
int x1 , y1 , x2 , y2 ;
int wxmin,wymin,wxmax, wymax ;
float u1 = 0.0,u2 = 1.0 ;
int p1 , q1 , p2 , q2 , p3 , q3 , p4 ,q4 ;
float r1 , r2 , r3 , r4 ;
int x11 , y11 , x22 , y22 ;
clrscr();
printf("Enter the windows left xmin , top boundry yminn");
scanf("%d%d",&wxmin,&wymin);
printf("Enter the windows right xmax ,bottom boundry ymaxn");
scanf("%d%d",&wxmax,&wymax);
printf("Enter line x1 , y1 co-ordinaten");
scanf("%d%d",&x1,&y1);
printf("Enter line x2 , y2 co-ordinaten");
scanf("%d%d",&x2,&y2);
printf("liang barsky express these 4 inequalities using lpk<=qpkn");
p1 = -(x2 - x1 ); q1 = x1 - wxmin ;
p2 = ( x2 - x1 ) ; q2 = wxmax - x1 ;
p3 = - ( y2 - y1 ) ; q3 = y1 - wymin ;
p4 = ( y2 - y1 ) ; q4 = wymax - y1 ;
printf("p1=0 line is parallel to left clippingn");
printf("p2=0 line is parallel to right clippingn");
printf("p3=0 line is parallel to bottom clippingn");
printf("p4=0 line is parallel to top clippingn");
if( ( ( p1 == 0.0 ) && ( q1 < 0.0 ) ) ||
( ( p2 == 0.0 ) && ( q2 < 0.0 ) ) ||
( ( p3 == 0.0 ) && ( q3 < 0.0 ) ) ||
( ( p4 == 0.0 ) && ( q4 < 0.0 ) ) )
{
printf("Line is rejectedn");
getch();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:tcbgi");
setcolor(WHITE);
rectangle(wxmin,wymax,wxmax,wymin);
setcolor(WHITE);
line(x1,y1,x2,y2);
getch();
setcolor(WHITE);
line(x1,y1,x2,y2);
getch();
}
else
{
if( p1 != 0.0 )
{
r1 =(float) q1 /p1 ;
if( p1 < 0 )
u1 = max(r1 , u1 );
else
u2 = min(r1 , u2 );
}
if( p2 != 0.0 )
{
r2 = (float ) q2 /p2 ;
if( p2 < 0 )
u1 = max(r2 , u1 );
else
u2 = min(r2 , u2 );
}
if( p3 != 0.0 )
{
r3 = (float )q3 /p3 ;
if( p3 < 0 )
u1 = max(r3 , u1 );
else
u2 = min(r3 , u2 );
}
if( p4 != 0.0 )
{
r4 = (float )q4 /p4 ;
if( p4 < 0 )
u1 = max(r4 , u1 );
else
u2 = min(r4 , u2 );
}
if( u1 > u2 )
printf("line rejectedn");
else
{
x11 = x1 + u1 * ( x2 - x1 ) ;
y11 = y1 + u1 * ( y2 - y1 ) ;
x22 = x1 + u2 * ( x2 - x1 );
y22 = y1 + u2 * ( y2 - y1 );
printf("Original line cordinatesn");
printf("x1 = %d , y1 = %d, x2 = %d, y2 = %dn",x1,y1,x2,y2);
printf("Windows coordinate are n");
printf("wxmin = %d, wymin = %d,wxmax = %d , wymax = %d
",wxmin,wymin,wxmax,wymax);
printf("New coordinates are n");
printf("x1 = %d, y1 = %d,x2 = %d , y2 = %dn",x11,y11,x22,y22);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:TCBGI");
setcolor(WHITE);
rectangle(wxmin,wymax,wxmax,wymin);
setcolor(WHITE);
line(x1,y1,x2,y2);
getch();
setcolor(BLACK);
line(x1,y1,x2,y2);
setcolor(WHITE);
line(x11,y11,x22,y22);
getch();
}
}
}
Output :
26. Programof Boundary fill algorithm :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void fill_right(int x,int y);
void fill_left(int x,int y);
void main()
{
int gd=DETECT,gm,x,y,n,i;
clrscr();
initgraph(&gd,&gm,"c:tcbgi");
printf("*** Boundary Fill algorithm ***");
/*- draw object -*/
line (50,50,200,50);
line (200,50,200,300);
line (200,300,50,300);
line (50,300,50,50);
/*- set seed point -*/
x=100; y=100;
fill_right(x,y);
fill_left(x-1,y);
getch();
}
void fill_right(int x,int y)
{
if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))
{
putpixel(x,y,RED);
fill_right(++x,y); x=x-1;
fill_right(x,y-1);
fill_right(x,y+1);
}
delay(1);
}
void fill_left(int x,int y)
{
if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))
{
putpixel(x,y,RED);
fill_left(--x,y); x=x+1;
fill_left(x,y-1);
fill_left(x,y+1);
}
delay(1);
}
Output :
27. Programof Floodfill algorithm :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void flood(int,int,int,int);
void main()
{
int gd,gm=DETECT;
clrscr();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:tcbgi");
rectangle(50,50,100,100);
flood(55,55,12,0);
getch();
}
void flood(int x,int y, int fill_col, int old_col)
{
if(getpixel(x,y)==old_col)
{
delay(10);
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);
}
}
Output :

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Liang barsky Line Clipping Algorithm
Liang barsky Line Clipping AlgorithmLiang barsky Line Clipping Algorithm
Liang barsky Line Clipping Algorithm
 
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
 
C Graphics Functions
C Graphics FunctionsC Graphics Functions
C Graphics Functions
 
Curve clipping
Curve clippingCurve clipping
Curve clipping
 
C++ file
C++ fileC++ file
C++ file
 
Computer Vision: Feature matching with RANSAC Algorithm
Computer Vision: Feature matching with RANSAC AlgorithmComputer Vision: Feature matching with RANSAC Algorithm
Computer Vision: Feature matching with RANSAC Algorithm
 
5 R Tutorial Data Visualization
5 R Tutorial Data Visualization5 R Tutorial Data Visualization
5 R Tutorial Data Visualization
 
Tugas CSS
Tugas CSSTugas CSS
Tugas CSS
 
Introduction to Bootstrap
Introduction to BootstrapIntroduction to Bootstrap
Introduction to Bootstrap
 
Computer graphics
Computer graphics   Computer graphics
Computer graphics
 
CSS
CSS CSS
CSS
 
CSC446: Pattern Recognition (LN5)
CSC446: Pattern Recognition (LN5)CSC446: Pattern Recognition (LN5)
CSC446: Pattern Recognition (LN5)
 
IMAGE QUALITY ASSESSMENT- A SURVEY OF RECENT APPROACHES
IMAGE QUALITY ASSESSMENT- A SURVEY OF RECENT APPROACHES IMAGE QUALITY ASSESSMENT- A SURVEY OF RECENT APPROACHES
IMAGE QUALITY ASSESSMENT- A SURVEY OF RECENT APPROACHES
 
Css animation
Css animationCss animation
Css animation
 
Output primitives computer graphics c version
Output primitives   computer graphics c versionOutput primitives   computer graphics c version
Output primitives computer graphics c version
 
2D viewing & clipping
2D viewing & clipping2D viewing & clipping
2D viewing & clipping
 
Computer graphics lab report with code in cpp
Computer graphics lab report with code in cppComputer graphics lab report with code in cpp
Computer graphics lab report with code in cpp
 
Trapezoidal Method IN Numerical Analysis
Trapezoidal Method IN  Numerical AnalysisTrapezoidal Method IN  Numerical Analysis
Trapezoidal Method IN Numerical Analysis
 
Mid point circle algorithm
Mid point circle algorithmMid point circle algorithm
Mid point circle algorithm
 
2d transformation
2d transformation2d transformation
2d transformation
 

Semelhante a C graphics programs file

1. Translation program#includestdio.h#includeconio.h#incl.pdf
1. Translation program#includestdio.h#includeconio.h#incl.pdf1. Translation program#includestdio.h#includeconio.h#incl.pdf
1. Translation program#includestdio.h#includeconio.h#incl.pdf
sutharbharat59
 

Semelhante a C graphics programs file (20)

Computer graphics
Computer graphicsComputer graphics
Computer graphics
 
Computer graphics
Computer graphics   Computer graphics
Computer graphics
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
 
COMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUALCOMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUAL
 
C-programs
C-programsC-programs
C-programs
 
C programs Set 2
C programs Set 2C programs Set 2
C programs Set 2
 
c programs.pptx
c programs.pptxc programs.pptx
c programs.pptx
 
C lab excellent
C lab excellentC lab excellent
C lab excellent
 
C and Data Structures Lab Solutions
C and Data Structures Lab SolutionsC and Data Structures Lab Solutions
C and Data Structures Lab Solutions
 
C and Data Structures
C and Data Structures C and Data Structures
C and Data Structures
 
computer graphics practicals
computer graphics practicalscomputer graphics practicals
computer graphics practicals
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU HyderabadSrinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
 
Srinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
Srinivas Reddy Amedapu C and Data Structures JNTUH HyderabadSrinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
Srinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
 
C Programming lab
C Programming labC Programming lab
C Programming lab
 
2.docx
2.docx2.docx
2.docx
 
1. Translation program#includestdio.h#includeconio.h#incl.pdf
1. Translation program#includestdio.h#includeconio.h#incl.pdf1. Translation program#includestdio.h#includeconio.h#incl.pdf
1. Translation program#includestdio.h#includeconio.h#incl.pdf
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
Progr3
Progr3Progr3
Progr3
 
Graphics point clipping c program
Graphics point clipping c programGraphics point clipping c program
Graphics point clipping c program
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 

C graphics programs file

  • 1. SHRI ATMANAND JAIN INSTITUTE OF MANAGEMENT AND TECHNOLOGY PRACTICAL FILE OF PROGRAMMING IN C-GRAPHICS SUBMITTED BY Shubham Kanojia MCA 4th SEMESTER Roll No. 1511 HEAD OF DEPTT. SUBMITTED TO EXAMINER Mrs. Rinkey Chaudhary Mrs. Rinkey Chaudhary (HOD of, MCA) (HOD of, MCA)
  • 2. INDEX Sr.No PARTICULARS SIGNATURE 1. Program to draw a line using polynomial method 2. Program to draw a line using DDA algorithm 3. Program to draw a line using Bresenham’s Algorithm 4. Program to draw circle using Polynomial method 5. Program to draw circle using Bresenham’s Algorithm 6. Program to draw a circle using trigonometric method 7. Program to draw a circle using mid-point method 8. Program to draw a ellipse using polynomial method 9. Program to draw a ellipse using Bresenham’s Algorithm 10. Program to draw a ellipse using trigonometric method 11. Program to draw a ellipse using mid-point method 12. Program to implement 2-D Translation 13. Program to implement 2-D Rotation 14. Program to implement 2-D Scaling 15. Program to draw sine curve 16. Program to draw X-Y axis 17. Program to print HELLO using line function 18. Program to print random lines on screen 19. Program to find whether a point is inside or outside the polygon 20. Program to implement mapping from window to viewport 21. Program to plot an ARC using trigonometric method 22. Program to implement 2-D reflection 23. Program to implement 2-D shearing 24. Program to implement line clipping using Cohan Sutherland algorithm 25. Program to implement line clipping using Liang Barsky method 26. Program of Boundary fill algorithm 27. Program of Flood fill algorithm
  • 3. 1. Programto draw a line using polynomial method : #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> int main() { float m; int i,gd=DETECT,gm,x1,y1,x2,y2,x,y,dy,dx,b; printf("enter the x coordinates:"); scanf("%d%d",&x1,&x2); printf("enter the y coordinates"); scanf("%d%d",&y1,&y2); initgraph(&gd,&gm,"c:tcbgi"); dx=x2-x1; dy=y2-y1; m=dy/dx; b=y1-(m*x1); x=x1; y=y1; if(m<1) while(x<=x2) { putpixel(x, y+0.5,WHITE); x++; y=(m*x)+b; } else { while(y<=y2) { putpixel(x+0.5,y,WHITE); y++; x=(y-b)/m; } } getch();
  • 5. 2. Programto draw a line using DDA algorithm : #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> int main() { float x,y,x1,y1,x2,y2,dx,dy,pixel; int i,gd=DETECT,gm; printf("enter the value of x1:"); scanf("%f",&x1); printf("enter the value of y1:"); scanf("%f",&y1); printf("enter the value of x2:"); scanf("%f",&x2); printf("enter the value of y2:"); scanf("%f",&y2); initgraph(&gd,&gm,"c:tcbgi"); dx=abs(x2-x1); dy=abs(y2-y1); if(dx>=dy) pixel=dx; else pixel=dy; dx=dx/pixel; dy=dy/pixel; x=x1; y=y1; i=1; while(i<=pixel) { putpixel(x,y,WHITE); x=x+dx; y=y+dx; i=i+1; delay(100); } getch(); closegraph();
  • 7.
  • 8. 3. Program to draw a line using Bresenham’s algorithm: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> int main() { float x,y,x1,y1,x2,y2,dx,dy; int i,gd=DETECT,gm,p,end; printf("enter the value of x1:"); scanf("%f",&x1); printf("enter the value of y1:"); scanf("%f",&y1); printf("enter the value of x2:"); scanf("%f",&x2); printf("enter the value of y2:"); scanf("%f",&y2); initgraph(&gd,&gm,"c:tcbgi"); dx=abs(x1-x2); dy=abs(y1-y2); p=2*dy-dx; if(x1>x2) { x=x2; y=y2; end=x1; } else { x=x1; y=y1; end=x2; } putpixel(x,y,WHITE); while(x<end) { x=x+1; if(p<0)
  • 10.
  • 11. 4. Programto draw a circle using Polynomial method : #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> int main() { int gd=DETECT,gm,xc,yc,xend,r,x,y; initgraph(&gd,&gm,"c:tcbgi"); printf("***draw circle using polunomial method ***n"); printf("enter x centre and y centret"); scanf("%d%d",&xc,&yc); printf("enter the radius t"); scanf("%d",&r); x=0; xend=r/sqrt(2); while(x<xend) { y=sqrt(r*r-x*x); putpixel(xc+x,yc+y,WHITE); putpixel(xc+y,yc+x,WHITE); putpixel(xc-x,yc-y,WHITE); putpixel(xc-y,yc-x,WHITE); putpixel(xc+x,yc-y,WHITE); putpixel(xc+y,yc-x,WHITE); putpixel(xc-x,yc+y,WHITE); putpixel(xc-y,yc+x,WHITE); x++; } getch(); closegraph(); }
  • 13. 5. Programto draw a circle using Bresenham’s algorithm : #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> int main() { int gd=DETECT,gm,xc,yc,r,x,y,pk; initgraph(&gd,&gm,"c:tcbgi"); printf("*** Bresenham's circle drawing algorithm***"); printf("enter the vlaue of xc t"); scanf("%d",&xc); printf("enter the vlaue of yc t"); scanf("%d",&yc); printf("enter the radius t"); scanf("%d",&r); x=0; y=r; putpixel(xc+x,yc-y,WHITE); pk=3-(2*r); for(x=0;x<=y;x++) { if(pk<0) { y=y; pk=(pk+(4*x)+6); } else { y=y-1; pk=pk+((4*(x-y)+10)); } putpixel(xc+x,yc+y,WHITE); putpixel(xc+y,yc+x,WHITE); putpixel(xc-x,yc-y,WHITE); putpixel(xc-y,yc-x,WHITE); putpixel(xc+x,yc-y,WHITE); putpixel(xc+y,yc-x,WHITE); putpixel(xc-x,yc+y,WHITE);
  • 15. 6. Programto draw a circle using trigonometric method : #include<stdio.h> #include<graphics.h> #include<conio.h> #include<math.h> void put8pixel(int,int,int,int); void main() { int gd=DETECT,gm; int x,y,x1,y1,r,h,k,theta; float n=3.14159/180; printf("*Trignometric Method to draw a circle*n"); printf("nenter the x and y coordinates:-"); scanf("%d%d",&h,&k); printf("nenter the radius:-n"); scanf("%d",&r); initgraph(&gd,&gm,"c:tcbgi"); for(theta=0;theta<=45;theta++) { x1=r*cos(theta*n); y1=r*sin(theta*n); x= x1+0.5; y=y1+0.5; put8pixel(x,y,h,k); } outtextxy(115,70,"circle using Trigonometric method"); getch(); closegraph(); } void put8pixel(int x,int y,int h,int k) { putpixel(x+h,y+k,WHITE); putpixel(x+h,-y+k,WHITE); putpixel(-x+h,y+k,WHITE); putpixel(-x+h,-y+k,WHITE); putpixel(y+h,x+k,WHITE); putpixel(y+h,-x+k,WHITE); putpixel(-y+h,x+k,WHITE); putpixel(-y+h,-x+k,WHITE);
  • 17. 7. Programto draw a circle using mid-point method : #include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT, gm; int xc,yc,x,y,r,f; initgraph(&gd,&gm,"c:tcbgi"); printf("Enter x-centre, y-centre and radius"); scanf("%d%d%d",&xc,&yc,&r); x=0; y=r; f=1-r; while(x<=y) { if(f<0) { f=f+(3+2*x); x++; } else { f=f+(2*x-2*y+5); x++; y--; } putpixel(x+xc, y+yc, WHITE); putpixel(y+xc, x+yc,WHITE); putpixel(-x+xc, y+yc, WHITE); putpixel(-y+xc, x+yc, WHITE); putpixel(-x+xc, -y+yc, WHITE); putpixel(-y+xc, -x+yc, WHITE); putpixel(x+xc,-y+yc,WHITE); putpixel(y+xc,-x+yc,WHITE); } getch(); closegraph(); }
  • 19. 8. Program to draw a ellipse using polynomial method : #include<stdio.h> #include<graphics.h> #include<conio.h> #include<math.h> void plot4pixels(int,int,int,int); void main() { int gd=DETECT,gm; int x,y,r,i,h,k,a,b; printf("n*Polynomial method to draw an ellipse*n"); printf("nenter the x and y coordinates:-n"); scanf("%d%d",&h,&k); printf("nenter the first radius:-n"); scanf("%d",&a); printf("nenter the second radius:-n"); scanf("%d",&b); x=0; y=b; initgraph(&gd,&gm,"c:tcbgi"); while(x<a) { plot4pixels(x,y,h,k); x++; y=b*sqrt(((a*a)-(x*x*1.0))/(a*a)); } plot4pixels(x,y,h,k); setcolor(WHITE); outtextxy(100,60,"ellipse using polynomial method"); getch(); } void plot4pixels(int x,int y,int h,int k) { putpixel(x+h,y+k,WHITE); putpixel(x+h,-y+k,WHITE); putpixel(-x+h,y+k,WHITE); putpixel(-x+h,-y+k,WHITE); }
  • 21. 9. Programto draw a ellipse using bresenham’s algorithm : #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void bresenhamellipse(int,int,int,int); void ellipseplotpoints(int,int,int,int,int); int main() { int gd=DETECT,gm; initgraph(&gd,&gm,"c:tcbgi"); bresenhamellipse(300,300,30,40); getch(); closegraph(); } void bresenhamellipse(int xcenter,int ycenter,int rx,int ry) { // xcenter and ycenter are the centre points. //rx and ry ,in these two any one can be the following,semi major axis and semi minor axis. long rx2=rx*rx,ry2=ry, x=0, y=ry, twory2=2*ry2, tworx2=2*rx2, p=0; ellipseplotpoints(xcenter,ycenter,x,y,WHITE); p=((int)(ry2*ry)+(.25*rx2)); while(twory2*x<tworx2*y) { x++; if(p<0) p+=(int)twory2*x+ry2; else { y--; p+=(int)twory2*ry2-ry2-tworx2*y; }
  • 22. ellipseplotpoints(xcenter,ycenter,x,y,WHITE); } //for region 2 p=(int)(ry2*(x+.5)*(x+.5)*(x+.5)+rx2*(y-1)*(y-1)-rx2*ry2); while(y>0) { y--; if(p>0) p+=rx2-tworx2*y; else { x++; p+=rx2+twory2*x-tworx2*y; } ellipseplotpoints(xcenter,ycenter,x,y,WHITE); } } void ellipseplotpoints(int xcenter,int ycenter,int x,int y,int color) { putpixel(xcenter+x,ycenter+y,WHITE); putpixel(xcenter-x,ycenter+y,WHITE); putpixel(xcenter+x,ycenter-y,WHITE); putpixel(xcenter-x,ycenter-y,WHITE); }
  • 24. 10. Programto draw a ellipse using trigonometric method : #include<stdio.h> #include<graphics.h> #include<conio.h> #include<math.h> void put4pixel(int,int,int,int); void main() { int x,y,x1,y1,a,b,h,k,theta; int gd=DETECT,gm; float p=3.14159/180; printf("*Trignometric method to draw an ellippse*n"); printf("nenter the x and y coordinates:-n"); scanf("%d%d",&h,&k); printf("nenter the first radius:-n"); scanf("%d",&a); printf("nenter the second radius:-n"); scanf("%d",&b); initgraph(&gd,&gm,"c:tcbgi"); for(theta=0;theta<=90;theta++) { x1=a*cos(theta*p); y1=b*sin(theta*p); x=x1+0.5; y=y1+0.5; put4pixel(x,y,h,k); } outtextxy(100,60,"ellipse using Trigonometric method"); getch(); closegraph(); } void put4pixel(int x,int y,int h,int k) { putpixel(x+h,y+k,WHITE); putpixel(x+h,-y+k,WHITE); putpixel(-x+h,y+k,WHITE); putpixel(-x+h,-y+k,WHITE); }
  • 26. 11. Programto draw a ellipse using mid-point method : #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void disp(); float x,y; int xc,yc; void main() { int gd=DETECT,gm; int a,b; float p1,p2; clrscr(); initgraph(&gd,&gm,"c:tcbgi"); printf("enter the xc and yc,a and b radius"); scanf("%d%d",&xc,&yc); scanf("%d%d",&a,&b); x=0;y=b; disp(); p1=(b*b)-(a*a*b)+(a*a)/4; while((2.0*b*b*x)<=(2.0*a*a*y)) { x++; if(p1<=0) p1=p1+(2.0*b*b*x)+(b*b); else { y--; p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y); } disp(); x=-x; disp(); x=-x; } x=a; y=0; disp();
  • 29. 12. Programto implement 2-D 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:tcbgi"); 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(); }
  • 31. 13. Programto implement 2-D 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:tcbgi"); line(x1,y1,x2,y2); printf("nnn[ 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(); }
  • 33. 14. Programto implement a 2-D scaling : #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:tcbgi"); 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(); }
  • 35. 15. Programto draw sine curve : #include <conio.h> #include <math.h> #include <graphics.h> #include <dos.h> int main() { int gd = DETECT, gm; int angle = 0; double x, y; initgraph(&gd, &gm, "C:TCBGI"); line(0, getmaxy() / 2, getmaxx(), getmaxy() / 2); /* generate a sine wave */ for(x = 0; x < getmaxx(); x+=3) { /* calculate y value given x */ y = 50*sin(angle*3.141/180); y = getmaxy()/2 - y; /* color a pixel at the given position */ putpixel(x, y, 15); delay(100); /* increment angle */ angle+=5; } getch(); /* deallocate memory allocated for graphics screen */ closegraph(); return 0; }
  • 37. 16. Program to draw X-Y axis : #include<stdio.h> #include<conio.h> #include<graphics.h> int main() { int gd=DETECT,gm; float xm,ym; initgraph(&gd,&gm,"c:tcbgi"); //calculating the mid point of screen xm=getmaxx()/2; ym=getmaxy()/2; line(xm,0,xm,2*ym); //creating y axis line(0,ym,2*xm,ym); //creating x axis outtextxy(2*xm-60,ym+5,"X-axis"); outtextxy(xm+4,10,"Y-axis"); getch(); closegraph(); }
  • 39. 17. Programto print HELLO using line function : #include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT,gm; int x,y,x1,y1; initgraph(&gd,&gm,"c:tcbgi"); //h line(100,100,100,200); line(100,150,150,150); line(150,100,150,200); //e line(200,100,250,100); line(200,100,200,200); line(200,200,250,200); line(200,150,250,150); //l line(300,100,300,200); line(300,200,350,200); //l line(400,100,400,200); line(400,200,450,200); //o line(500,100,500,200); line(500,100,550,100); line(500,200,550,200); line(550,100,550,200); getch(); closegraph(); }
  • 41. 18. Programto print random lines on screen: #include<stdio.h> #include<graphics.h> #include<conio.h> #include<stdlib.h> void main() { int gd=DETECT,gm; int i,j,xxmax,yymax,xmax,ymax,x,y,xx,yy; initgraph(&gd,&gm,"c:tcbgi"); xmax=getmaxx(); ymax=getmaxy(); xxmax=getmaxx(); yymax=getmaxy(); while(!kbhit()) { for(i=0;i<=100;i++) { x=random(xmax); y=random(ymax); xx=random(xxmax); yy=random(yymax); for(j=1;j<=5;j++) { setcolor(WHITE); line(x,y,xx,yy); } } } getch(); closegraph(); } resorecrtmode(): }
  • 43. 19. Programto find whether a point is inside or outside the polygon : #include<stdio.h> #include<stdlib.h> #include<conio.h> #include<graphics.h> #include<math.h> void main() { int gd=DETECT,gm,errorcode; float x[10],y[10],x1,y1,c[10],xm,ym; int flag,n,i,j; initgraph(&gd,&gm,"c:tcbgi"); printf("enter the number of vertices of polygon"); scanf("%d",&n); printf("n enter the verticesn"); printf("x[i] and y[i]n"); for(i=0;i<n;i++) { scanf("%f%f",&x[i],&y[i]); } printf("n enter the point to be checkedn"); scanf("%f%f",&x1,&y1); xm=getmaxx()/2; ym=getmaxx()/2; line(xm,0,xm,2*ym); line(0,ym,2*xm,ym); for(i=0;i<n-1;i++) { circle(x[i]+xm,(-y[i]+ym),2); line(x[i]+xm,(-y[i]+ym),x[i+1]+xm,(-y[i+1]+ym)); } circle(x[n-1]+xm,(-y[n-1]+ym),2); line(x[n-1]+xm,(-y[n-1]+ym),x[0]+xm,(-y[0]+ym)); circle(x1+xm,-y1+ym,2); for(i=0;i<n;i++) c[i]=((x[i+1]-x[i])*(y1-y[i]))-((y[i+1]-y[i])*(x1-x[i])); c[n-1]=((x[0]-x[n-1])*(y1-y[n-1]))-((y[0]-y[n-1])*(x1-x[n-1])); flag=0; for(i=0;i<n;i++)
  • 44. { if(c[i]>0) flag=1; if(c[i]<0) { flag=0; break; } } if(flag==1) printf("n point is inside the polygon"); if(flag==0) printf("n point outside the polygon"); getch(); } Output :
  • 45. 20. Program to implement mapping from window to viewport : #include<stdio.h> #include<conio.h> #include<graphics.h> #include<stdlib.h> void main() { float xwmin,xwmax,ywmax,ywmin; float xvmin,xvmax,yvmax,yvmin; float x[10],y[10],yv,xv,sx,sy; int gd=DETECT,gm,i; clrscr(); printf("n enter window port coordinates:n(xwmin,ywmin,xwmax,ywmax): "); scanf("%f%f%f%f",&xwmin,&ywmin,&xwmax,&ywmax); printf("n enter view port coordinates:n(xvmin,yvmin,xvmax,yvmax): "); scanf("%f%f%f%f",&xvmin,&yvmin,&xvmax,&yvmax); printf("n enter vertices for triangle: "); for(i=0;i < 3;i++) { printf("n enter(x%d,y%d):",i,i); scanf("%f%f",&x[i],&y[i]); } sx=((xvmax-xvmin)/(xwmax-xwmin)); sy=((yvmax-yvmin)/(ywmax-xwmin)); initgraph(&gd,&gm,"c:tcbgi"); outtextxy(80,30,"window port"); rectangle(xwmin,ywmin,xwmax,ywmax); for(i=0;i <2;i++) { line(x[i],y[i],x[i+1],y[i+1]); } line(x[2],y[2],x[0],y[0]); getch(); cleardevice(); for(i=0;i <3;i++) { x[i]=xvmin+((x[i]-xwmin)*sx); y[i]=yvmin+((y[i]-ywmin)*sy); }
  • 47.
  • 48. 21. Programto plot an ARC using trigonometric method : #include<stdio.h> #include<conio.h> #include<math.h> #include<stdlib.h> #include<graphics.h> #define pi 3.14 void main() { int gd=DETECT,gm,i,ch,errorcode; float a,b,h,k,x,y,theta,theta1; initgraph(&gd,&gm,"c:tcbgi"); printf("n enter the coordinates of arc center h and k"); scanf("%f%f",&h,&k); printf("n step size"); scanf("%d",&i); printf("n enter the length of major and minor axis"); scanf("%f%f",&a,&b); printf("n enter the starting and ending angle"); scanf("%f%f",&theta,&theta1); errorcode=graphresult(); //if(errorcode!=grOk) //{ //printf("graphics errorn"); //getch(); //exit(1); //} while(theta<theta1) { x=a*cos((pi*theta)/180)+h; y=b*sin((pi*theta)/180)+k; putpixel(x,y,WHITE); theta=theta+i; } getch(); }
  • 50. 22. Programto implement 2-D Reflection: #include<graphics.h> #include<math.h> #include<conio.h> #include<stdio.h> void main() { int gd=DETECT,gm; int i,xmid,ymid,x1,y1,x2,y2,x3,y3,x,y,dy,dx,p,gap=50,temp,option; int x1dash,x2dash,x3dash,y1dash,y2dash,y3dash; double theta; float m; char str[5]; clrscr(); initgraph(&gd,&gm,"..bgi"); printf("Enter first co-ords of the trianglen"); scanf("%d %d",&x1,&y1); printf("Enter second co-ords of the trianglen"); scanf("%d %d",&x2,&y2); printf("Enter third co-ords of the trianglen"); scanf("%d %d",&x3,&y3); xmid= getmaxx()/2; ymid= getmaxy()/2; line(5,ymid,getmaxx()-5,ymid); line(xmid+3,5,xmid+3,getmaxy()-5); for( i= xmid+gap;i<getmaxx()-5;i=i+gap) { outtextxy(i,ymid-3,"|"); itoa(i-xmid,str,10); outtextxy(i,ymid+3,str); } for( i= ymid-gap;i>5;i=i-gap) { outtextxy(xmid,i,"-"); itoa(ymid-i,str,10); outtextxy(xmid+5,i,str); } for( i= xmid-gap;i>5;i=i-gap) {
  • 51. outtextxy(i,ymid-3,"|"); itoa(-(xmid-i),str,10); outtextxy(i-6,ymid+3,str); } for( i= ymid+gap;i<getmaxy()-5;i=i+gap) { outtextxy(xmid,i,"-"); itoa(-(i-ymid),str,10); outtextxy(xmid+8,i,str); } line(x1+xmid,ymid-y1,x2+xmid,ymid-y2); line(x2+xmid,ymid-y2,x3+xmid,ymid-y3); line(x3+xmid,ymid-y3,x1+xmid,ymid-y1); setcolor(255); printf("Reflection about n"); printf("X axis : 1n"); printf("Y axis : 2n"); printf("X=Y axis : 3n"); printf(" Enter the option (1-3):"); scanf("%d",&option); switch( option) { case 1: y1=-y1; y2=-y2;y3=-y3; break; case 2: x1=-x1;x2=-x2;x3=-x3;break; case 3: y1=-y1; y2=-y2;y3=-y3; theta= ((double) 90 *3.14f )/(double)180; x1dash=x1*cos(theta)-y1*sin(theta); x2dash=x2*cos(theta)-y2*sin(theta); x3dash=x3*cos(theta)-y3*sin(theta); y1dash=x1*sin(theta)+y1*cos(theta); y2dash=x2*sin(theta)+y2*cos(theta); y3dash=x3*sin(theta)+y3*cos(theta); x1=x1dash;x2=x2dash;x3=x3dash; y1=y1dash;y2=y2dash;y3=y3dash; } line(x1+xmid,ymid-y1,x2+xmid,ymid-y2); line(x2+xmid,ymid-y2,x3+xmid,ymid-y3); line(x3+xmid,ymid-y3,x1+xmid,ymid-y1); getch(); closegraph();
  • 53. 23. Programto implement 2-D Shearing : #include<graphics.h> #include<conio.h> void main() { int gd=DETECT,gm,option,xref,yref; int i,maxx,maxy,x1,y1,x2,y2,x3,y3,x4,y4,gap=50; float shx=0.0,shy=0.0; char str[5]; clrscr(); initgraph(&gd,&gm,"..bgi"); printf("enter the endpoints of the top of the rectangle (x1,y1) & (x2,y2):"); scanf("%d%d%d%d",&x1,&y1,&x2,&y2); printf("enter the endpoints of bottom of the rectangle (x3,y3) & (x4,y4)"); scanf("%d%d%d%d",&x3,&y3,&x4,&y4); printf(" Enter the axis to shearn"); printf(" 1 - X axis Shearn"); printf(" 2 - Y axis shearn"); scanf("%d",&option ); if(option==1) { printf("enter the value for x-axis shear( can be fraction too):"); scanf("%f",&shx); } else { printf("enter the value for y-axis shear( can be fraction too):"); scanf("%f",&shy); } clearviewport(); maxx= getmaxx(); maxy=getmaxy(); line(3,maxy-1,maxx-5,maxy-1); line(5,5,5,maxy-3); for( i= 0;i<maxx-5;i=i+gap) // code to display co-ordinates { outtextxy(i+3,maxy-7,"|"); itoa(i,str,10); outtextxy(i,maxy-10,str);
  • 54. } for( i= maxy;i>0;i=i-gap) { outtextxy(3,i,"-"); itoa(maxy-i,str,10); outtextxy(9,i,str); } setcolor(WHITE); // drawing rectangle using endpoints line(x1,maxy-y1,x2,maxy-y2); line(x3,maxy-y3,x4,maxy-y4); line(x1,maxy-y1,x3,maxy-y3); line(x2,maxy-y2,x4,maxy-y4); outtextxy(10,10,"hit any key to see the shearing effect" ); getch(); setcolor(WHITE); // to hide the rectangle drawn line(x1,maxy-y1,x2,maxy-y2); line(x3,maxy-y3,x4,maxy-y4); line(x1,maxy-y1,x3,maxy-y3); line(x2,maxy-y2,x4,maxy-y4); setcolor(WHITE); // to redraw the rectangle if(option==1) { // shearing about x axis so only points x1 and x2 need to be recomputed line(x1+shx*y1,maxy-y1,x2+shx*y2,maxy-y2); line(x3,maxy-y3,x4,maxy-y4); line(x1+shx*y1,maxy-y1,x3,maxy-y3); line(x2+shx*y2,maxy-y2,x4,maxy-y4); } else { // shearing about y axis so only points y2 and y4 need to be recomputed line(x1,maxy-y1,x2,maxy-(y2+shy*x2)); line(x3,maxy-y3,x4,maxy-(y4+shy*x4)); line(x1,maxy-y1,x3,maxy-y3); line(x2,maxy-(y2+shy*x2),x4,maxy-(y4+shy*x4)); } getch(); closegraph(); }
  • 56.
  • 57. 24. Programto implement line clipping using CohanSutherland algorithm : #include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT, gm; float i,xmax,ymax,xmin,ymin,x1,y1,x2,y2,m; float start[4],end[4],code[4]; clrscr(); initgraph(&gd,&gm,"c:tcbgi"); printf("ntEnter the bottom-left coordinate of viewport: "); scanf("%f %f",&xmin,&ymin); printf("ntEnter the top-right coordinate of viewport: "); scanf("%f %f",&xmax,&ymax); printf("nEnter the coordinates for starting point of line: "); scanf("%f %f",&x1,&y1); printf("nEnter the coordinates for ending point of line: "); scanf("%f %f",&x2,&y2); for(i=0;i <4;i++) { start[i]=0; end[i]=0; } m=(y2-y1)/(x2-x1); if(x1 <xmin) start[0]=1; if(x1 >xmax) start[1]=1; if(y1 >ymax) start[2]=1; if(y1 <ymin) start[3]=1; if(x2 <xmin) end[0]=1; if(x2 >xmax) end[1]=1; if(y2 >ymax) end[2]=1; if(y2 <ymin) end[3]=1; for(i=0;i <4;i++) code[i]=start[i]&&end[i]; if((code[0]==0)&&(code[1]==0)&&(code[2]==0)&&(code[3]==0)) {
  • 58. if((start[0]==0)&&(start[1]==0)&&(start[2]==0)&&(start[3]==0)&&(end[0]==0)&&(en d[1]==0)&&(end[2]==0)&&(end[3]==0)) { cleardevice(); printf("nttThe line is totally visiblenttand not a clipping candidate"); rectangle(xmin,ymin,xmax,ymax); line(x1,y1,x2,y2); getch(); } else { cleardevice(); printf("nttLine is partially visible"); rectangle(xmin,ymin,xmax,ymax); line(x1,y1,x2,y2); getch(); if((start[2]==0)&&(start[3]==1)) { x1=x1+(ymin-y1)/m; y1=ymin; } if((end[2]==0)&&(end[3]==1)) { x2=x2+(ymin-y2)/m; y2=ymin; } if((start[2]==1)&&(start[3]==0)) { x1=x1+(ymax-y1)/m; y1=ymax; } if((end[2]==1)&&(end[3]==0)) { x2=x2+(ymax-y2)/m; y2=ymax; } if((start[1]==0)&&(start[0]==1)) { y1=y1+m*(xmin-x1); x1=xmin;
  • 61.
  • 62. 25. Programto implement line clipping using Liang Barskymethod : #include<graphics.h> #include<dos.h> #include<conio.h> #include<stdlib.h> void main() { int gd, gm ; int x1 , y1 , x2 , y2 ; int wxmin,wymin,wxmax, wymax ; float u1 = 0.0,u2 = 1.0 ; int p1 , q1 , p2 , q2 , p3 , q3 , p4 ,q4 ; float r1 , r2 , r3 , r4 ; int x11 , y11 , x22 , y22 ; clrscr(); printf("Enter the windows left xmin , top boundry yminn"); scanf("%d%d",&wxmin,&wymin); printf("Enter the windows right xmax ,bottom boundry ymaxn"); scanf("%d%d",&wxmax,&wymax); printf("Enter line x1 , y1 co-ordinaten"); scanf("%d%d",&x1,&y1); printf("Enter line x2 , y2 co-ordinaten"); scanf("%d%d",&x2,&y2); printf("liang barsky express these 4 inequalities using lpk<=qpkn"); p1 = -(x2 - x1 ); q1 = x1 - wxmin ; p2 = ( x2 - x1 ) ; q2 = wxmax - x1 ; p3 = - ( y2 - y1 ) ; q3 = y1 - wymin ; p4 = ( y2 - y1 ) ; q4 = wymax - y1 ; printf("p1=0 line is parallel to left clippingn"); printf("p2=0 line is parallel to right clippingn"); printf("p3=0 line is parallel to bottom clippingn"); printf("p4=0 line is parallel to top clippingn"); if( ( ( p1 == 0.0 ) && ( q1 < 0.0 ) ) || ( ( p2 == 0.0 ) && ( q2 < 0.0 ) ) || ( ( p3 == 0.0 ) && ( q3 < 0.0 ) ) || ( ( p4 == 0.0 ) && ( q4 < 0.0 ) ) ) { printf("Line is rejectedn"); getch();
  • 63. detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:tcbgi"); setcolor(WHITE); rectangle(wxmin,wymax,wxmax,wymin); setcolor(WHITE); line(x1,y1,x2,y2); getch(); setcolor(WHITE); line(x1,y1,x2,y2); getch(); } else { if( p1 != 0.0 ) { r1 =(float) q1 /p1 ; if( p1 < 0 ) u1 = max(r1 , u1 ); else u2 = min(r1 , u2 ); } if( p2 != 0.0 ) { r2 = (float ) q2 /p2 ; if( p2 < 0 ) u1 = max(r2 , u1 ); else u2 = min(r2 , u2 ); } if( p3 != 0.0 ) { r3 = (float )q3 /p3 ; if( p3 < 0 ) u1 = max(r3 , u1 ); else u2 = min(r3 , u2 ); } if( p4 != 0.0 ) {
  • 64. r4 = (float )q4 /p4 ; if( p4 < 0 ) u1 = max(r4 , u1 ); else u2 = min(r4 , u2 ); } if( u1 > u2 ) printf("line rejectedn"); else { x11 = x1 + u1 * ( x2 - x1 ) ; y11 = y1 + u1 * ( y2 - y1 ) ; x22 = x1 + u2 * ( x2 - x1 ); y22 = y1 + u2 * ( y2 - y1 ); printf("Original line cordinatesn"); printf("x1 = %d , y1 = %d, x2 = %d, y2 = %dn",x1,y1,x2,y2); printf("Windows coordinate are n"); printf("wxmin = %d, wymin = %d,wxmax = %d , wymax = %d ",wxmin,wymin,wxmax,wymax); printf("New coordinates are n"); printf("x1 = %d, y1 = %d,x2 = %d , y2 = %dn",x11,y11,x22,y22); detectgraph(&gd,&gm); initgraph(&gd,&gm,"C:TCBGI"); setcolor(WHITE); rectangle(wxmin,wymax,wxmax,wymin); setcolor(WHITE); line(x1,y1,x2,y2); getch(); setcolor(BLACK); line(x1,y1,x2,y2); setcolor(WHITE); line(x11,y11,x22,y22); getch(); } } }
  • 66.
  • 67. 26. Programof Boundary fill algorithm : #include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> void fill_right(int x,int y); void fill_left(int x,int y); void main() { int gd=DETECT,gm,x,y,n,i; clrscr(); initgraph(&gd,&gm,"c:tcbgi"); printf("*** Boundary Fill algorithm ***"); /*- draw object -*/ line (50,50,200,50); line (200,50,200,300); line (200,300,50,300); line (50,300,50,50); /*- set seed point -*/ x=100; y=100; fill_right(x,y); fill_left(x-1,y); getch(); } void fill_right(int x,int y) { if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED)) { putpixel(x,y,RED); fill_right(++x,y); x=x-1; fill_right(x,y-1); fill_right(x,y+1); } delay(1); } void fill_left(int x,int y) { if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))
  • 69. 27. Programof Floodfill algorithm : #include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> void flood(int,int,int,int); void main() { int gd,gm=DETECT; clrscr(); detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:tcbgi"); rectangle(50,50,100,100); flood(55,55,12,0); getch(); } void flood(int x,int y, int fill_col, int old_col) { if(getpixel(x,y)==old_col) { delay(10); 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); } }