Вы находитесь на странице: 1из 46

16103069 1

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.

Step 2 : Extract the downloaded file. There will be three


files:
 graphics.h
 winbgim.h
 libbgi.a
tep 3 : Copy and
S

paste graphics.h and winbgim.h files into


the include folder of compiler directory. (If you have
Code::Blocks installed in C drive of your computer, go
through: Disk C >> Program Files >> CodeBlocks >>
MinGW >> include. Paste these two files there.)
Step 4 : Copy and paste libbgi.a to the lib folder of
compiler directory.

Step 5 : Open Code::Blocks. Go to Settings >>


Compiler >> Linker settings.

Step 6 : In that window, click the Add button under the


“Link libraries” part, and browse.
16103069 2
16103064

Step 7 : In right part (ie. other linker options) paste


commands
-lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32
Step 8 : Click Ok

Step 9 : Try compiling a graphics.h program in C or C++,


still there will be an error. To solve it, open graphics.h
file (pasted in include folder in step 3) with Notepad++.
Go to line number 302, and replace that line with this
line : int left=0, int top=0, int right=INT_MAX, int
bottom=INT_MAX,

Step 10 : Save the file. Done !


16103069 3
16103064

Lab 2
Program to implement Analog Clock
#include<iostream>

#include<graphics.h>

#include<math.h>

#include<time.h>

using namespace std;

int hr,sec,minu;

void drawClock(int xc,int yc)

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

Program to implement Moving Car


#include <iostream>

#include <graphics.h>

using namespace std;

void draw_moving_car(void) {

int i, j = 0, gd = DETECT, gm;

initgraph(&gd, &gm, "");

for (i = 0; i <= 420; i = i + 10) {


16103069 7
16103064
setcolor(RED);

line(0 + i, 300, 210 + i, 300);

line(50 + i, 300, 75 + i, 270);

line(75 + i, 270, 150 + i, 270);

line(150 + i, 270, 165 + i, 300);

line(0 + i, 300, 0 + i, 330);

line(210 + i, 300, 210 + i, 330);

circle(65 + i, 330, 15);

circle(65 + i, 330, 2);

circle(145 + i, 330, 15);

circle(145 + i, 330, 2);

line(0 + i, 330, 50 + i, 330);

line(80 + i, 330, 130 + i, 330);

line(210 + i, 330, 160 + i, 330);

delay(100);

setcolor(BLACK);

line(0 + i, 300, 210 + i, 300);

line(50 + i, 300, 75 + i, 270);

line(75 + i, 270, 150 + i, 270);

line(150 + i, 270, 165 + i, 300);

line(0 + i, 300, 0 + i, 330);

line(210 + i, 300, 210 + i, 330);

circle(65 + i, 330, 15);

circle(65 + i, 330, 2);


16103069 8
16103064
circle(145 + i, 330, 15);

circle(145 + i, 330, 2);

line(0 + i, 330, 50 + i, 330);

line(80 + i, 330, 130 + i, 330);

line(210 + i, 330, 160 + i, 330);

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>

#define ScreenWidth getmaxx()

#define ScreenHeight getmaxy()

#define GroundY ScreenHeight*0.75

int ldaisp=0;

void DrawManWithUmbrella(int a,int ldaisp)

//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)

if(ry<GroundY-120 || (ry>GroundY-120 && (rx<a-20 ||


rx>a+60)))

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:

Program to implement Bubble Sort


#include <stdio.h>

void swap(int *xp, int *yp)

int temp = *xp;

*xp = *yp;

*yp = temp;

void bubbleSort(int arr[], int n)

{
16103069 14
16103064
int i, j;

for (i = 0; i < n-1; i++)

for (j = 0; j < n-i-1; j++)

if (arr[j] > arr[j+1])

swap(&arr[j], &arr[j+1]);

void printArray(int arr[], int size)

int i;

for (i=0; i < size; i++)

printf("%d ", arr[i]);

printf("n");

int main()

int arr[] = {64, 34, 25, 12, 22, 11, 90};

int n = sizeof(arr)/sizeof(arr[0]);

bubbleSort(arr, n);

printf("Sorted array: \n");

printArray(arr, n);

return 0;

}
16103069 15
16103064

OUTPUT:

Program to implement DDA Algorithm


#include<stdio.h>

#include<graphics.h>

int abs (int n)

return ( (n>0) ? n : ( n * (-1)));

void DDA(int X0, int Y0, int X1, int Y1)

int dx = X1 - X0;

int dy = Y1 - Y0;

int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);

float Xinc = dx / (float) steps;


16103069 16
16103064
float Yinc = dy / (float) steps;

float X = X0;

float Y = Y0;

for (int i = 0; i <= steps; i++)

putpixel (X,Y,RED); // put pixel at (X,Y)

X += Xinc; // increment in x at each step

Y += Yinc; // increment in y at each step

delay(100); // for visualization of line-

// generation step by step

int main()

int gd = DETECT, gm;

initgraph (&gd, &gm, "");

int X0 = 2, Y0 = 2, X1 = 14, Y1 = 16;

DDA(2, 2, 14, 16);

return 0;

}
16103069 17
16103064

OUTPUT:
16103069 18
16103064

Lab 4
Program to implement Bresenham’s
Algorithm
#include<bits/stdc++.h>

using namespace std;

void bresenham(int x1, int y1, int x2, int y2)

int m_new = 2 * (y2 - y1);

int slope_error_new = m_new - (x2 - x1);

for (int x = x1, y = y1; x <= x2; x++)

cout << "(" << x << "," << y << ")\n";

slope_error_new += m_new;

if (slope_error_new >= 0)

y++;

slope_error_new -= 2 * (x2 - x1);

int main()

int x1 = 3, y1 = 2, x2 = 15, y2 = 5;
16103069 19
16103064
bresenham(x1, y1, x2, y2);

return 0;

OUTPUT:

Program to implement Mid Point Circle


Algorithm.
#include<iostream>

using namespace std;

int midpointcircle(int Xcentre,int Ycentre,int r)

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>

using namespace std;

int midpointellipse(int rx,int ry,int xc,int yc)

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)

cout << x + xc << " , " << y + yc << endl;

cout << -x + xc << " , " << y + yc << endl;

cout << x + xc << " , " << -y + yc << endl;

cout << -x + xc << " , " << -y + yc << endl;

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);

}}

d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5))) +

((rx * rx) * ((y - 1) * (y - 1))) -

(rx * rx * ry * ry);

while (y >= 0)

cout << x + xc << " , " << y + yc << endl;

cout << -x + xc << " , " << y + yc << endl;

cout << x + xc << " , " << -y + yc << endl;

cout << -x + xc << " , " << -y + yc << endl;

if (d2 > 0)

y--;

dy = dy - (2 * rx * rx);

d2 = d2 + (rx * rx) - dy;

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>

using namespace std;

void translateRectangle ( int P[][2], int T[])

int gd = DETECT, gm, errorcode;

initgraph (&gd, &gm, "c:\\tc\\bgi");

setcolor (2);

rectangle (P[0][0], P[0][1], P[1][0], P[1][1]);

P[0][0] = P[0][0] + T[0];

P[0][1] = P[0][1] + T[1];

P[1][0] = P[1][0] + T[0];

P[1][1] = P[1][1] + T[1];

rectangle (P[0][0], P[0][1], P[1][0], P[1][1]);

int main()

int P[2][2] = {5, 8, 12, 18};

int T[] = {6, 7};

translateRectangle (P, T);


16103069 26
16103064
getch();

return 0;

OUTPUT:

2. Scaling
#include<cstdio>

#include<graphics.h>

void newcoordinate(int s[][2], int p[][1])

int temp[2][1] = { 0 };

for (int i = 0; i < 2; i++)

for (int j = 0; j < 1; j++)

for (int k = 0; k < 2; k++)

temp[i][j] += (s[i][k] * p[k][j]);

p[0][0] = temp[0][0];
16103069 27
16103064
p[1][0] = temp[1][0];

void scale(int x[],int y[],int sx,int sy)

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>

using namespace std;

#define SIN(x) sin(x * 3.141592653589/180)

#define COS(x) cos(x * 3.141592653589/180)

int rotate(int a[][2],int n,int x_point,int y_point,int angle)

int i=0;

while(i<n)

int x_shift= a[i][0] - x_point;

int y_shift = a[i][1] - y_point;

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];

cout<<"enter the co-ordinate points"<<endl;

for(int i=0;i<siz;i++)

for(int j=0;j<2;j++)

cin>>p[i][j];

rotate(p, siz, 0, 0, 45);

return 0;

OUTPUT:

4. Reflection
#include <iostream>

#include <complex>

using namespace std;


16103069 31
16103064

typedef complex<double> point;

#define x real()

#define y imag()

#define PI 3.1415926535897932384626

void displayPoint(point P)

cout << "(" << P.x << ", " << P.y << ")" << endl;

point reflect(point P, point A, point B)

point Pt = P-A;

point Bt = B-A;

point Pr = Pt/Bt;

return conj(Pr)*Bt + A;

int main()

point P(4.0, 7.0);

point A(1.0, 1.0);

point B(3.0, 3.0);

point P_reflected = reflect(P, A, B);


16103069 32
16103064
cout << "The point P on reflecting about AB becomes:";

cout << "P_reflected"; displayPoint(P_reflected);

return 0;

OUTPUT:
16103069 33
16103064

Lab :7
Implementation of Cohen Sudherland line
clipping Algo
#include <iostream>

using namespace std;

const int INSIDE = 0;

const int LEFT = 1;

const int RIGHT = 2;

const int BOTTOM = 4;

const int TOP = 8; .

const int x_max = 10;

const int y_max = 8;

const int x_min = 4;

const int y_min = 4;

int computeCode(double x, double y)

int code = INSIDE;

if (x < x_min)

code |= LEFT;

else if (x > x_max)

code |= RIGHT;
16103069 34
16103064
if (y < y_min)

code |= BOTTOM;

else if (y > y_max)

code |= TOP;

return code;

void cohenSutherlandClip(double x1, double y1,

double x2, double y2)

int code1 = computeCode(x1, y1);

int code2 = computeCode(x2, y2);

bool accept = false;

while (true)

if ((code1 == 0) && (code2 == 0))

accept = true;

break;

else if (code1 & code2)

break;
16103069 35
16103064
}

else

int code_out;

double x, y;

if (code1 != 0)

code_out = code1;

else

code_out = code2;

if (code_out & TOP)

x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1);

y = y_max;

else if (code_out & BOTTOM)

x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1);

y = y_min;

else if (code_out & RIGHT)

y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1);

x = x_max;

}
16103069 36
16103064
else if (code_out & LEFT)

y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1);

x = x_min;

if (code_out == code1)

x1 = x;

y1 = y;

code1 = computeCode(x1, y1);

else

x2 = x;

y2 = y;

code2 = computeCode(x2, y2);

if (accept)

cout <<"Line accepted from " << x1 << ", "

<< y1 << " to "<< x2 << ", " << y2 << endl;

}
16103069 37
16103064
else

cout << "Line rejected" << endl;

int main()

cohenSutherlandClip(5, 5, 7, 7);

cohenSutherlandClip(7, 9, 11, 4);

cohenSutherlandClip(1, 5, 4, 1);

return 0;

OUTPUT:
16103069 38
16103064

Implementation of liang barsky line clipping


algorithm
#include<iostream>

#include<graphics.h>

#include<math.h>

#include<dos.h>

using namespace std;

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)

{cout<<"line is parallel to one of the clipping boundary";

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>

using namespace std;

const int MAX_POINTS = 20;

int x_intersect(int x1, int y1, int x2, int y2,

int x3, int y3, int x4, int y4)

int num = (x1*y2 - y1*x2) * (x3-x4) -

(x1-x2) * (x3*y4 - y3*x4);

int den = (x1-x2) * (y3-y4) - (y1-y2) * (x3-x4);

return num/den;

int y_intersect(int x1, int y1, int x2, int y2,

int x3, int y3, int x4, int y4)

int num = (x1*y2 - y1*x2) * (y3-y4) -

(y1-y2) * (x3*y4 - y3*x4);

int den = (x1-x2) * (y3-y4) - (y1-y2) * (x3-x4);

return num/den;

void clip(int poly_points[][2], int &poly_size,


16103069 43
16103064
int x1, int y1, int x2, int y2)

int new_points[MAX_POINTS][2], new_poly_size = 0;

for (int i = 0; i < poly_size; i++)

int k = (i+1) % poly_size;

int ix = poly_points[i][0], iy = poly_points[i][1];

int kx = poly_points[k][0], ky = poly_points[k][1];

int i_pos = (x2-x1) * (iy-y1) - (y2-y1) * (ix-x1);

int k_pos = (x2-x1) * (ky-y1) - (y2-y1) * (kx-x1);

if (i_pos < 0 && k_pos < 0)

new_points[new_poly_size][0] = kx;

new_points[new_poly_size][1] = ky;

new_poly_size++;

else if (i_pos >= 0 && k_pos < 0)

new_points[new_poly_size][0] = x_intersect(x1,

y1, x2, y2, ix, iy, kx, ky);

new_points[new_poly_size][1] = y_intersect(x1,

y1, x2, y2, ix, iy, kx, ky);

new_poly_size++;
16103069 44
16103064
new_points[new_poly_size][0] = kx;

new_points[new_poly_size][1] = ky;

new_poly_size++;

else if (i_pos < 0 && k_pos >= 0)

new_points[new_poly_size][0] = x_intersect(x1,

y1, x2, y2, ix, iy, kx, ky);

new_points[new_poly_size][1] = y_intersect(x1,

y1, x2, y2, ix, iy, kx, ky);

new_poly_size++;

else

//No points are added

poly_size = new_poly_size;

for (int i = 0; i < poly_size; i++)

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,

int clipper_points[][2], int clipper_size)

for (int i=0; i<clipper_size; i++)

int k = (i+1) % clipper_size;

clip(poly_points, poly_size, clipper_points[i][0],

clipper_points[i][1], clipper_points[k][0],

clipper_points[k][1]);

for (int i=0; i < poly_size; i++)

cout << '(' << poly_points[i][0] <<

", " << poly_points[i][1] << ") ";

int main()

int poly_size = 3;

int poly_points[20][2] = {{100,150}, {200,250},

{300,200}};

int clipper_size = 4;

int clipper_points[][2] = {{150,150}, {150,200},

{200,200}, {200,150} };

// 2nd Example with triangle


clipper
16103069 46
16103064
/*int clipper_size = 3;

int clipper_points[][2] = {{100,300}, {300,300},

{200,100}};*/

//Calling the clipping function

suthHodgClip(poly_points, poly_size, clipper_points,

clipper_size);

return 0;

OUTPUT:

Вам также может понравиться