"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();
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)
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();
}
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;
}
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();
}
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++)
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();
}
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",­);
}
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();
}