Steps To Install Graphics.H in Codeblocks
Steps To Install Graphics.H in Codeblocks
Steps To Install Graphics.H in Codeblocks
16103064
Lab 1
Steps To install graphics.h in CodeBlocks
Step 1 : To setup “graphics.h” in CodeBlocks, first set
up winBGIm graphics library. Download WinBGIm
from http://winbgim.codecutter.org/ or use this link.
Lab 2
Program to implement Analog Clock
#include<iostream>
#include<graphics.h>
#include<math.h>
#include<time.h>
int hr,sec,minu;
int i,inr=70,outr=80;
int hdeg,mindeg,secdeg,x,y;
circle(xc,yc,outr);
setfillstyle(SOLID_FILL,WHITE);
fillellipse(xc,yc,2,2);
for(i=180;i>-360;i-=6)
if(i%5==0)
fillellipse(xc+inr*sin(i*3.14f/180),yc+inr*cos(i*3.14f/180),2,2);
else
fillellipse(xc+inr*sin(i*3.14f/180),yc+inr*cos(i*3.14f/180),1,1);
//draw hands
16103069 4
16103064
hdeg=hr*360/12+30*minu/60;
mindeg=minu*6;
secdeg=sec*6;
x=xc+40*sin(hdeg*3.14f/180);
y=yc-40*cos(hdeg*3.14f/180);
line(xc,yc,x,y);
x=xc+55*sin(mindeg*3.14f/180);
y=yc-55*cos(mindeg*3.14f/180);
line(xc,yc,x,y);
x=xc+65*sin(secdeg*3.14f/180);
y=yc-65*cos(secdeg*3.14f/180);
line(xc,yc,x,y);
int main()
time_t now;
struct tm *timeinfo;
int gd=DETECT,gm,midx,midy;
int tsec,tmin,thr;
initgraph(&gd,&gm,"..\\BGI\\");
16103069 5
16103064
midx=getmaxx()/2;
midy=getmaxy()/2;
time(&now);
timeinfo=localtime(&now);
hr=timeinfo->tm_hour;
minu=timeinfo->tm_min;
sec=timeinfo->tm_sec;
while(!kbhit())
drawClock(midx,midy);
delay(995);
cleardevice();
tsec=(sec+1)%60;
tmin=(minu+(tsec==0?1:0))%60;
thr=(hr+(tmin==0?1:0))%12;
hr=thr;
sec=tsec;
minu=tmin;
}
16103069 6
16103064
#include <graphics.h>
void draw_moving_car(void) {
delay(100);
setcolor(BLACK);
getch();
closegraph();
int main()
draw_moving_car();
return 0;
}
16103069 9
16103064
OUTPUT:
16103069 10
16103064
Lab 3
Program to implement Rain Man.
#include<stdio.h>
#include<graphics.h>
int ldaisp=0;
//Draw Umbrella
pieslice(a+20,GroundY-120,0,180,40);
line(a+20,GroundY-120,a+20,GroundY-70);
//Draw head
circle(a,GroundY-90,10);
line(a,GroundY-80,a,GroundY-30);
//Draw hand
line(a,GroundY-70,a+10,GroundY-60);
line(a,GroundY-65,a+10,GroundY-55);
16103069 11
16103064
line(a+10,GroundY-60,a+20,GroundY-70);
line(a+10,GroundY-55,a+20,GroundY-70);
//Draw legs
line(a,GroundY-30,a+ldaisp,GroundY);
line(a,GroundY-30,a-ldaisp,GroundY);
void Rain(int a)
int i,rx,ry;
for(i=0;i<400;i++)
rx=rand() % ScreenWidth;
ry=rand() % ScreenHeight;
if(ry<GroundY-4)
line(rx,ry,rx+0.5,ry+4);
}
16103069 12
16103064
int main()
int gd=DETECT,gm,a=0;
initgraph(&gd,&gm,"C:\TurboC++\Disk\TurboC3\BGI");
while(!kbhit())
//Draw Ground
line(0,GroundY,ScreenWidth,GroundY);
Rain(a);
ldaisp=(ldaisp+2)%20;
DrawManWithUmbrella(a,ldaisp);
delay(40);
cleardevice();
a=(a+2)%ScreenWidth;
getch();
}
16103069 13
16103064
OUTPUT:
*xp = *yp;
*yp = temp;
{
16103069 14
16103064
int i, j;
swap(&arr[j], &arr[j+1]);
int i;
printf("n");
int main()
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printArray(arr, n);
return 0;
}
16103069 15
16103064
OUTPUT:
#include<graphics.h>
int dx = X1 - X0;
int dy = Y1 - Y0;
float X = X0;
float Y = Y0;
int main()
return 0;
}
16103069 17
16103064
OUTPUT:
16103069 18
16103064
Lab 4
Program to implement Bresenham’s
Algorithm
#include<bits/stdc++.h>
slope_error_new += m_new;
if (slope_error_new >= 0)
y++;
int main()
int x1 = 3, y1 = 2, x2 = 15, y2 = 5;
16103069 19
16103064
bresenham(x1, y1, x2, y2);
return 0;
OUTPUT:
int x=r,y=0;
cout<<"("<<x+Xcentre<<","<<y+Ycentre<<")";
if (r > 0)
16103069 20
16103064
{
cout << "(" << x + Xcentre << ", " << -y + Ycentre << ") ";
cout << "(" << y + Xcentre << ", " << x + Ycentre << ") ";
cout << "(" << -y + Xcentre << ", " << x + Ycentre << ")\n";
int p=1-r;
while(x>y)
y++;
if(p<=0)
p=p+2*y+1;
else{
x--;
p=p+2*y-2*x+1;
if(x<y)
break;
cout << "(" << x + Xcentre << ", " << y + Ycentre << ") ";
cout << "(" << -x + Xcentre << ", " << y + Ycentre << ") ";
cout << "(" << x + Xcentre << ", " << -y + Ycentre << ") ";
cout << "(" << -x + Xcentre << ", " << -y + Ycentre << ")\n";
if (x != y)
cout << "(" << y + Xcentre << ", " << x + Ycentre << ") ";
16103069 21
16103064
cout << "(" << -y + Xcentre << ", " << x + Ycentre << ") ";
cout << "(" << y + Xcentre << ", " << -x + Ycentre << ") ";
cout << "(" << -y + Xcentre << ", " << -x + Ycentre << ")\n";
int main()
{ midpointcircle(0,0,8);
return 0;}
OUTPUT:
16103069 22
16103064
Lab 5
Program to implement Mid Point Ellipse
#include<iostream>
float dx,dy,d1,d2,x,y;
x=0,y=ry;
d1=(ry*ry)-(rx*rx*ry)+(rx*rx*0.25);
dx=2*ry*ry*x;
dy=2*rx*rx*y;
while(dx<dy)
if (d1 < 0)
x++;
dx = dx + (2 * ry * ry);
d1 = d1 + dx + (ry * ry);
16103069 23
16103064
}
else
x++;
y--;
d1=d1+(2*ry*ry)-(2*rx*rx)+(ry*ry);
}}
(rx * rx * ry * ry);
while (y >= 0)
if (d2 > 0)
y--;
dy = dy - (2 * rx * rx);
else
{
16103069 24
16103064
y--;
x++;
dx = dx + (2 * ry * ry);
dy = dy - (2 * rx * rx);
d2 = d2 + dx - dy + (rx * rx);
int main()
midpointellipse(10,15,5,5);
return 0;
Output:
(50.000000, 65.000000)
(50.000000, 65.000000)
(50.000000, 35.000000)
(50.000000, 35.000000)
(51.000000, 65.000000)
(49.000000, 65.000000)
(51.000000, 35.000000)
(49.000000, 35.000000)
(52.000000, 65.000000)
(48.000000, 65.000000)
(52.000000, 35.000000)
(48.000000, 35.000000)
(53.000000, 64.000000)
(47.000000, 64.000000)
16103069 25
16103064
Lab 6
Program to implement 2D Transformation.
1. Translation
#include<bits/stdc++.h>
#include<graphics.h>
setcolor (2);
int main()
return 0;
OUTPUT:
2. Scaling
#include<cstdio>
#include<graphics.h>
int temp[2][1] = { 0 };
p[0][0] = temp[0][0];
16103069 27
16103064
p[1][0] = temp[1][0];
line(x[0],y[0],x[1],y[1]);
line(x[1],y[1],x[2],y[2]);
line(x[2],y[2],x[0],y[0]);
int s[2][2]={sx,0,0,sy};
int p[2][1];
for(int i=0;i<3;i++)
p[0][0]=x[i];
p[1][0]=y[i];
newcoordinate(s,p);
x[i]=p[0][0];
y[i]=p[1][0];
line(x[0],y[0],x[1],y[1]);
line(x[1],y[1],x[2],y[2]);
line(x[2],y[2],x[0],y[0]);
int main()
int x[]={100,200,300};
16103069 28
16103064
int y[]={200,100,200};
int sx=2,sy=2;
int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
scale(x,y,sx,sy);
getch();
return 0;
OUTPUT:
16103069 29
16103064
3. Rotation
#include<iostream>
#include<graphics.h>
#include<math.h>
int i=0;
while(i<n)
a[i][0]=x_point+(x_shift*COS(angle)-y_shift*SIN(angle));
a[i][1]=y_point+(x_shift*SIN(angle)+y_shift*COS(angle));
cout << "(" << a[i][0] << ", " << a[i][1] << ") ";
i++;
int main()
int siz;
16103069 30
16103064
cout<<"enter the no of co-ordinates"<<endl;
cin>>siz;
int p[siz][2];
for(int i=0;i<siz;i++)
for(int j=0;j<2;j++)
cin>>p[i][j];
return 0;
OUTPUT:
4. Reflection
#include <iostream>
#include <complex>
#define x real()
#define y imag()
#define PI 3.1415926535897932384626
void displayPoint(point P)
cout << "(" << P.x << ", " << P.y << ")" << endl;
point Pt = P-A;
point Bt = B-A;
point Pr = Pt/Bt;
return conj(Pr)*Bt + A;
int main()
return 0;
OUTPUT:
16103069 33
16103064
Lab :7
Implementation of Cohen Sudherland line
clipping Algo
#include <iostream>
if (x < x_min)
code |= LEFT;
code |= RIGHT;
16103069 34
16103064
if (y < y_min)
code |= BOTTOM;
code |= TOP;
return code;
while (true)
accept = true;
break;
break;
16103069 35
16103064
}
else
int code_out;
double x, y;
if (code1 != 0)
code_out = code1;
else
code_out = code2;
y = y_max;
y = y_min;
x = x_max;
}
16103069 36
16103064
else if (code_out & LEFT)
x = x_min;
if (code_out == code1)
x1 = x;
y1 = y;
else
x2 = x;
y2 = y;
if (accept)
<< y1 << " to "<< x2 << ", " << y2 << endl;
}
16103069 37
16103064
else
int main()
cohenSutherlandClip(5, 5, 7, 7);
cohenSutherlandClip(1, 5, 4, 1);
return 0;
OUTPUT:
16103069 38
16103064
#include<graphics.h>
#include<math.h>
#include<dos.h>
int main()
int i,gd=DETECT,gm;
int x1,y1,x2,y2,xmin,xmax,ymin,ymax,xx1,xx2,yy1,yy2,dx,dy;
float t1,t2,p[4],q[4],temp;
x1=120;
y1=120;
x2=300;
y2=300;
xmin=100;
ymin=100;
xmax=250;
ymax=250;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
rectangle(xmin,ymin,xmax,ymax);
dx=x2-x1;
16103069 39
16103064
dy=y2-y1;
p[0]=-dx;
p[1]=dx;
p[2]=-dy;
p[3]=dy;
q[0]=x1-xmin;
q[1]=xmax-x1;
q[2]=y1-ymin;
q[3]=ymax-y1;
for(i=0;i<4;i++)
{if(p[i]==0)
if(q[i]>=0)
if(i<2)
if(y1<ymin)
{y1=ymin;}
if(y2>ymax)
{y2=ymax;}
line(x1,y1,x2,y2);}
if(i>1)
{if(x1<xmin)
{
16103069 40
16103064
x1=xmin;}
if(x2>xmax)
{x2=xmax;}
line(x1,y1,x2,y2);
}}}}
t1=0;
t2=1;
for(i=0;i<4;i++)
temp=q[i]/p[i];
if(p[i]<0)
{if(t1<=temp)
t1=temp;}
else
{if(t2>temp)
t2=temp;
}}
if(t1<t2)
xx1 = x1 + t1 * p[1];
xx2 = x1 + t2 * p[1];
yy1 = y1 + t1 * p[3];
yy2 = y1 + t2 * p[3];
line(xx1,yy1,xx2,yy2);
16103069 41
16103064
}
delay(5000);
closegraph();
getch();
OUTPUT:
16103069 42
16103064
Lab:9
Implementation of polygon clipping
#include<iostream>
return num/den;
return num/den;
new_points[new_poly_size][0] = kx;
new_points[new_poly_size][1] = ky;
new_poly_size++;
new_points[new_poly_size][0] = x_intersect(x1,
new_points[new_poly_size][1] = y_intersect(x1,
new_poly_size++;
16103069 44
16103064
new_points[new_poly_size][0] = kx;
new_points[new_poly_size][1] = ky;
new_poly_size++;
new_points[new_poly_size][0] = x_intersect(x1,
new_points[new_poly_size][1] = y_intersect(x1,
new_poly_size++;
else
poly_size = new_poly_size;
poly_points[i][0] = new_points[i][0];
poly_points[i][1] = new_points[i][1];
}
16103069 45
16103064
void suthHodgClip(int poly_points[][2], int poly_size,
clipper_points[i][1], clipper_points[k][0],
clipper_points[k][1]);
int main()
int poly_size = 3;
{300,200}};
int clipper_size = 4;
{200,200}, {200,150} };
{200,100}};*/
clipper_size);
return 0;
OUTPUT: