Experiment No. 1: Graphics Mode Initialization

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 51

Experiment No.

1
Aim: To study about graphics.h, initgraph, graphics driver and graphics mode.

GRAPHICS.H
The header file contains many library functions which can be used to draw different shapes and texts change color and carry out more such operations. It can be used to draw circles, lines, rectangle, bars or any other geometrical figures. The user can change their color accordingly. Some of the functions include in this header file are: initgraph closegraph cleardevice arc line ellipse fillellipse rectangle pieslice setcolor setbkcolor grapherromsg

Graphics Mode Initialization


initgraph
initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver) then putting the system into graphics mode. initgraph also resets all graphics setting (color, palette, current position, viewport, etc.) to their defaults, then resets graphresult to 0. Return Value: initgraph always sets the internal error code.

On success, initgraph sets the code to 0. On error, initgraoh sets *graphdriver to -2, -2, -4, or -5, and graphresult returns the same value.

Graphics drivers

*Graphdriver
Integer that specify the graph driver to be used. The user can give graphdriver a value using a constant of the graphics_drivers enumeration type. Constant DETECT CGA MCGA EGA EGA64 EGAMONO iBM8514 HERCMONO ATT400 VGA PC3270 Value 0 1 2 3 4 5 6 7 8 9 10

*Graphmode
Integer that specifies the initial graphics mode (unless *graphdiver = DETECT). If *graphdriver = DETECT, initgraph sets *graphmode to the highest resolution available for the detect driver. The user can give *graphmode a value using a constant of the graphics_modes enumeration type.

*Pathtodriver
Specifies the directory path where initgraph looks for graphics drivers (*.BGI) first. o If they are not there, initgraph looks in the current directory.

o If pathtodriver is null, the driver files must be in the current directory.

Graphics Errors
Graphresult
Returns an error code for the last unsuccessful graphics operation and then resets the error level to grOk. Declaration: int far graphresult(void);

Grapherrormsg
Grapherrmosg will accept the errorcode returned by graphresult and returns the error string. Declaration: char *far grapherrormsg (int errorcode);

Closegraph
Closegraph function does the graphics mode, deallocates all the memory allocated by graphics system and restores the screen to the mode it was in before the user called initgraph. Declaration:

void closegraph();

Experiment No. 2
Aim: Write a program to draw different shapes using graphics.h PROGRAM: #include <graphics.h> #include <iosteam.h> #include <stdio.h> #include <conio.h> #include <process.h> void main() { clrscr(); int gdriver = DETECT, gmode,ch,x1,y1,x2,y2,r; initgraph(&gmode,&gdriver,"C:\\TC\\BGI"); do { cleardevice(); cout<<"\nGraphics Menu"; cout<<"\n1. Line"; cout<<"\n2. Circle"; cout<<"\n3. Arc"; cout<<"\n4. Ellipse"; cout<<"\n5. Rectangle"; cout<<"\n6. Pieslice"; cout<<"\n7. Exit";

cout<<"\nEnter your choice"; cin>>ch; clrscr(); cleardevice(); switch(ch) { case1: cout<<"\nEnter the first coordinates: \n"; cin>>x1>>y1; cout<<"\nEnter the second coordinates: \n"; cin>>x2>>y2; clrscr(); cleardevice(); line(x1,y1,x2,y2); getch(); break; case2: cout<<"\nEnter centre and radius: \n"; cout<<"\nEnter centre coordinate x: \n"; cin>>x1; cout<<"\nEnter centre cordinate y: \n"; cin>>x1; cout<<"\nEnter radius r: \n"; cin>>r;

clrscr(); cleardevice(); circle(x1,y1,r); getch(); break(); case3: cout<<"\nEnter the coordinates: \n"; cin>>x1>>y1; cout<<"\nEnter start angle & end angle: \n"; cin>>x2>>y2; cout<<"\nEnter radius: ; cin>>r; clrscr(); cleardevice(); arc(x1,y1,x2,y2,r); getch(); break; case4: cout<<\nEnter x and y: \n; cin>>x1>>y1; cout<<\nEnter xradius and yradius: \n; cin>>x2>>y2; clrscr();

cleardevice(); fillellipse(x1,y1,x2,y2); getch(); break; case5: cout<<\nEnter coordinates of diagonal of a rectangle: \n; cin>>x1>>y1>>x2>>y2; clrscr(); cleardevice(); rectangle(x1,y1,x2,y2); getch(); break; case6: cout<<\nEnter midx and midy: \n; cin>>x1>>y1; cout<<\nEnter stangle and endangle: \n; cin>>x2>>y2; cout<<\nEnter radius: \n; cin>>r; clrscr(); cleardevice(); pieslice(x1,y1,x2,y2,r); getch();

break; case7: exit(0); } } while(ch>0&&ch<<8); getch(); closegraph(); }

//OUTPUT
Graphics Menu 1.Line 2.Circle 3.Arc 4.Ellipse 5.Rectangle 6.Pieslice 7.Exit Enter your choice 1 Enter your first coordinates: 11

13 Enter the second coordinates: 15 8

Graphics Menu 1.Line 2.Circle 3.Arc 4.Ellipse 5.Rectangle 6.Pieslice 7.Exit Enter your choice 2 Enter the center and the radius:

Enter center cordinate x: 50 Enter center coordinate y: 63 Enter radius r: 41

Experiment No. 3
Aim: Write a program to generate a line using DDA line generation algorithm PROGRAM: #include <graphics.h> #include <stdio.h> #include <conio.h> #include <math.h>

void main() { int gd = DETECT, gm = DETECT, s, dx, dy, m, x1, y1, x2, y2; float xi, yi, x, y; clrscr(); cout<<Enter the starting point x1 & y1: \n); cin>>x1>>y1; cout<<Enter the end point x2 & y2: \n); cin>>x2>>y2; initgraph(&gd, &gm, ); cleardevice(); dx = x2 - x1; dy = y2 - y1; if (abs(dx) > abs(dy)) s = abs(dx); else s = abs(dy);

xi = dx / (float) s; yi = dy / (float) s; x = x1; y = y1; putpixel(x1, y1, 4);

for (m = 0; m < s; m++) { x += xi; y += yi; putpixel(x, y, 4); } getch(); }

//OUTPUT
Enter the starting point x1 & y1: 20 30 Enter the end point x2 & y2: 75 52

Experiment No. 4
Aim: Write a program to generate a line using Bresenhams line generation algorithm. PROGRAM: #include<iostream.h> #include<conio.h> #include<graphics.h> #include<math.h> void swap(int &x, int &y) { int k = x; x = y; y = k; } void main() { int gd = DETECT, gm = DETECT, x1, x2, y1, y2, dx, dy, p, k; float m = 0; clrscr(); cout<<\nEnter the starting point x1 & y1: \n; cin>>x1>>y1; cout<<\nEnter the end point x2 & y2: \n; cin>>x2>>y2; dx = abs(x2 - x1);

dy = abs(y2 - y1); m = (float) (y2 - y1) / (x2 - x1); initgraph(&gd, &gm, ); cleardevice(); if (fabs(m) > 1) { swap(x1, y1); swap(x2, y2); swap(dx, dy); } if ((x1 > x2)) { x1 = x2; y1 = y2; } p = 2 * dy - dx; for (k = 0; k < abs(dx); k++) { if (p < 0) { p = p + 2 * dy; } else { if (m < 0) y1--; else y1++; p = p + (2 * dy) - (2 * dx); } if (fabs(m) <= 1)

putpixel(x1++, y1, 15); else putpixel(y1, x1++, 15); } getch(); }

//OUTPUT
Enter 150 150 Enter the end 250 300 point x2 & y2: the starting point x1 & y1:

Experiment No. 5
Aim: Write a program to generate a circle using Bresenhams circle generation algorithm. PROGRAM: #include <iostream.h> #include <dos.h> #include <graphics.h> void plotPoints(int cx, int cy, int x, int y) { putpixel(cx+x, cy+y, RED); putpixel(cx-x, cy+y, RED); putpixel(cx+x, cy-y, RED); putpixel(cx-x, cy-y, RED); putpixel(cx+y, cy+x, RED); putpixel(cx-y, cy+x, RED); putpixel(cx+y, cy-x, RED); putpixel(cx-y, cy-x, RED); }

void main() { int cx, cy, x = 0, y, r, p; int gd = DETECT, gm; clrscr();

cout<<Enter the coordinates of centre of the circle: ;

cin>>cx>>cy; cout<<Enter radius of : ; cin>>r;

y = r; p = 3 - 2 * r;

initgraph(&gd, &gm, ); cleardevice(); while (x < y) { plotPoints(cx, cy, x, y); x++; if (p < 0) p = p + 4 * x + 6; else { y--; p = p + 4 * (x - y) + 10; } plotPoints(cx, cy, x, y); delay(200); } getch(); }

//OUTPUT
Enter the 400 400 Enter radius of: coordinates of circle: 30

Experiment No. 6
Aim: Write a program to implement midpoint circle algorithm.

PROGRAM: #include<iostream.h> #include<conio.h> #include<graphics.h>

void draw_circle(int,int,int); void symmetry(int,int,int,int);

main() { int xc,yc,R; int driver,mode;

clrscr(); cout<<Enter the center of the circle:\n; cout<<\tXc = ; cin>>xc; cout<<\tYc = ; cin>>yc; cout<<Enter the radius of the circle: ; cin>>R;

clrscr(); driver = DETECT; initgraph(&driver,&mode,\\tc\\bgi); //the path may be different in your case. draw_circle(xc,yc,R);

getch(); closegraph(); }

void draw_circle(int xc,int yc,int rad) { int x = 0; int y = rad; int p = 1-rad; symmetry(x,y,xc,yc);

for(x= 0;y>x;x++) { if(p<0) p += 2*x + 3; else { p += 2*(x-y) + 5; y--;

} symmetry(x,y,xc,yc); } }

void symmetry(int x,int y,int xc,int yc) { putpixel(xc+x,yc-y,EGA_WHITE);//For pixel (x,y) putpixel(xc+y,yc-x,EGA_WHITE);//For pixel (y,x) putpixel(xc+y,yc+x,EGA_WHITE);//For pixel (y,-x) putpixel(xc+x,yc+y,EGA_WHITE);//For pixel (x,-y) putpixel(xc-x,yc+y,EGA_WHITE);//For pixel (-x,-y) putpixel(xc-y,yc+x,EGA_WHITE);//For pixel (-y,-x) putpixel(xc-y,yc-x,EGA_WHITE);//For pixel (-y,x) putpixel(xc-x,yc-y,EGA_WHITE);//For pixel (-x,y) }

//OUTPUT
Enter the center of the circle: Xc = 100 Yc = 150 Enter the radius of the circle: 105

Experiment No. 7
Aim: Write a program for Line clipping using Cohen and Sutherland Algorithm. PROGRAM: #include <iostream.h> #include <graphics.h> #include <conio.h> #include <math.h> #define TRUE 1 #define FALSE 0 typedef unsigned int outcode; outcode CompOutCode(float x,float y); enum { TOP = 0x1, BOTTOM = 0x2, RIGHT = 0x4, LEFT = 0x8 float xmin,xmax,ymin,ymax; void clip(float x0,float y0,float x1,float y1) { outcode outcode0,outcode1,outcodeOut; int accept = FALSE,done = FALSE; outcode0 = CompOutCode(x0,y0); outcode1 = CompOutCode(x1,y1); do { if(!(outcode0|outcode1)) { accept = TRUE; done = TRUE; } };

else if(outcode0 & outcode1) done = TRUE; else { float x,y; outcodeOut = outcode0?outcode0:outcode1; if(outcodeOut & TOP) { x = x0+(x1-x0)*(ymax-y0)/(y1-y0); y = ymax; } else if(outcodeOut & BOTTOM) { x = x0+(x1-x0)*(ymin-y0)/(y1-y0); y = ymin; } else if(outcodeOut & RIGHT) { y = y0+(y1-y0)*(xmax-x0)/(x1-x0); x = xmax; } else { y = y0+(y1-y0)*(xmin-x0)/(x1-x0); x = xmin;

} if(outcodeOut==outcode0) { x0 = x; y0 = y; outcode0 = CompOutCode(x0,y0); } else { x1 = x; y1 = y; outcode1 = CompOutCode(x1,y1); } } }while(done==FALSE); if(accept) line(x0,y0,x1,y1); outtextxy(200,20,LINE AFTER CLIPPING); rectangle(xmin,ymin,xmax,ymax); } outcode CompOutCode(float x,float y) { outcode code = 0; if(y>ymax) code|=TOP; else if(y<ymin)

code|=BOTTOM; if(x>xmax) code|=RIGHT; else if(x<xmin) code|=LEFT; return code; } void main( ) { float x1,y1,x2,y2,x3,y3,x4,y4; int gdriver = DETECT, gmode ; cout<<\nEnter the endpoints of first line: \n; cin>>x1>>y1>>x2>>y2; cout<<\nEnter the endpoints of second line: \n; cin>>x3>>y3>>x4>>y4; cout<<Enter the rectangular coordinates of clipping window: ; cin>>xmin>>ymin>>xmax>>ymax; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, c:\\tc\\bgi); outtextxy(200,20,LINE BEFORE CLIPPING); line(x1,y1,x2,y2); line(x3,y3,x4,y4); rectangle(xmin,ymin,xmax,ymax); getch( ); cleardevice( ); clip(x1,y1,x2,y2);

clip(x3,y3,x4,y4); getch( ); restorecrtmode( ); }

//OUTPUT
Enter the endpoints of first line 100 80 470 340 Enter the endpoints of second line 108 145 113 295

Enter the rectangular coordinates of clipping window 180 150 300 280

Experiment No. 8
Aim: Implement Translation, Scaling, rotation and Shearing in 2-D Transformations. PROGRAM: /***** Rotation *****/ #include<graphics.h> #include<iostream.h> #include<conio.h> #include<math.h>

void rotate( int figure[], int edges, double angle, int cx, int cy ) { double x, y; angle = -1 * (angle*3.14/180); double cos_a = cos(angle); double sin_a = sin(angle);

for(int i=0; i < edges; i++) { x = figure[2*i] - cx; y = figure[2*i+1] - cy; figure[2*i] = ceil( (x * cos_a) - (y * sin_a) + cx ); figure[2*i+1] = ceil( (x * sin_a)+(y * cos_a) + cy ); }

void main() { int figure[20], edges; // A Figure with Max 10 edges. double angle; int cx=0, cy=0; int gd = DETECT, gm; initgraph( &gd, &gm, ); int max_y = getmaxy(); clrscr(); cleardevice();

cout<<Number of edges: ; cin>>edges;

for(int i=0; i < edges; i++) { cout<<Enter edge (x%d,y%d) : , i , i ); scanf( %d %d, &figure[2*i], &figure[2*i+1] ); } figure[2*i] = figure[0]; figure[2*i+1] = figure[1]; edges += 1;

cout<<Enter angle of rotation in degrees: ; cin>>angle;

cout<<Enter the center of rotation: \n; cout<<cx: ; cin>>cx; cout<<cy: ; cin>>cy; cy = max_y - cy;

cleardevice(); setbkcolor(WHITE); setcolor(YELLOW); setlinestyle(SOLID_LINE, 0, 3); drawpoly( edges, figure ); getch(); for(int i=0; i < edges; i++) figure[2*i+1] = max_y - figure[2*i+1]; rotate(figure,edges,angle,cx,cy); for(int i=0; i < edges; i++) figure[2*i+1] = max_y - figure[2*i+1];

setcolor(BLUE); drawpoly( edges, figure );

getch(); }

//OUTPUT
Number of edges: 3 Enter edge (x0,y0): 280 80 Enter edge (x1,y1): 80 280 Enter edge (x2,y2): 480 280 Enter angle of rotation in degree: 180 Enter the center of rotation: cx: 80 280

/**** Translation*****/ #include<graphics.h> #include<iostream.h> #include<conio.h>

void translation( int figure[], int edges, int dx, int dy ) { for(int i=0; i < edges; i++) { figure[2*i] += dx; figure[2*i+1] += dy; } }

void main() { int figure[20], edges, dx, dy; // A Figure with Max 10 edges. int gd = DETECT, gm; clrscr();

cout<<Number of edges: ; cin>>edges;

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

{ cout<<Enter edge (x<<i<< y<<i): ; cin>>figure[2*i]>>figure[2*i+1]; } figure[2*i] = figure[0]; figure[2*i+1] = figure[1]; edges += 1;

cout<<Enter dx: ; cin>>dx; cout<<Enter dy: ; cin>>dy;

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

drawpoly( edges, figure ); getch();

translation(figure,edges,dx,dy);

setcolor(RED); drawpoly( edges, figure ); getch(); }

//OUTPUT
Number of edges: 3 Enter edge (x0, y0): 105 105 Enter edge (x1, y1): 15 105 Enter edge (x2, y2): 205 205 Enter dx: 88 Enter dy: 88

/**** Scaling *****/ #include<graphics.h> #include<stdio.h> #include<conio.h> void scale( int figure[], int edges, int dx, int dy, int cx, int cy ) { for(int i=0; i < edges; i++) { figure[2*i] = (figure[2*i] - cx) * dx + cx; figure[2*i+1] = (figure[2*i+1] - cy) * dy + cy; } } void main() { int figure[20], edges; // A Figure with Max 10 edges. int dx, dy, cx=0, cy=0; int gd = DETECT, gm; clrscr(); cout<<Number of edges: ; cin>>edges; for(int i=0; i < edges; i++) { cout<<Enter edge (x<<i<<,y<<i<<): ; cin>>figure[2*i]>>figure[2*i+1];

} figure[2*i] = figure[0]; figure[2*i+1] = figure[1]; edges += 1; cout<<Enter dx: ; cin>>dx; cout<<Enter dy: ; cin>>dy;

cout<<Enter the center of scaling: \n; cout<<cx: ; cin>>cx; cout<<cy: ; cin>>cy; initgraph( &gd, &gm, ); cleardevice(); setbkcolor(WHITE); setcolor(GREEN); setlinestyle(SOLID_LINE, 0, 3); drawpoly( edges, figure ); getch(); scale(figure,edges,dx,dy,cx,cy); setcolor(RED); drawpoly( edges, figure ); getch();

//OUTPUT
Number of edges: 3 Enter edge (x0,y0): 155 5 Enter edge (x1,y1): 55 55 Enter edge (x2,y2): 255 55 Enter dx: 2 Enter dy: 2 Enter center scaling: cx: 155 cy: 5 the of

/***** Shearing *****/ #include <graphics.h> #include <iostream.h> #include <conio.h> void main( ) { /* request auto detection */ int gdriver = DETECT, gmode; int x1,y1,x2,y2,a,b; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, c:\\tc\\bgi); cout<<Enter the value of line coordinates: ; cin>>x1>>y1>>x2>>y2; cout<<Enter the value of x-shearing factor: ; cin>>a; cout<<Enter the value of y-shearing factor: ; cin>>b; cleardevice( ); outtextxy(200,20,LINE BEFORE SHEARING); line(x1,y1,x2,y2); x1 = x1+a*y1; y1 = b*x1+y1; x2 = x2+a*y2; y2 = b*x2+y2; getch( ); cleardevice( ); outtextxy(200,20,LINE AFTER SHEARING);

line(x1,y1,x2,y2); getch( ); closegraph( ); restorecrtmode( ); }

//OUTPUT:
Enter the value of line coordinates: 18 18 87 87 Enter the value of x-shearing factor: 3 Enter the value of y-shearing: 3

Experiment No. 9
Aim: Write a program to Implement Cubic Bezier Curves. PROGRAM: #include<iostream.h> #include<conio.h> #include<graphics.h>

int x,y,z;

void main()

{ float u; int gd,gm,ymax,i,n,c[4][3];

for(i=0;i<4;i++) { c[i][0]=0; c[i][1]=0; }

printf(\n\n Enter four points : \n\n); for(i=0; i<4; i++) { printf(\t X%d Y%d : ,i,i); scanf(%d %d,&c[i][0],&c[i][1]); }

c[4][0]=c[0][0]; c[4][1]=c[0][1];

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

ymax = 480;

setcolor(13); for(i=0;i<3;i++) { line(c[i][0],ymax-c[i][1],c[i+1][0],ymax-c[i+1][1]);

setcolor(3); n=3;

for(i=0;i<=40;i++) { u=(float)i/40.0; bezier(u,n,c); if(i==0) { moveto(x,ymax-y);} else { lineto(x,ymax-y); } getch(); } getch(); }/*-main()-*/ /*--------------------------?curve.c?--------------------------*/ bezier(u,n,p) float u;int n; int p[4][3]; { int j; float v,b; float blend(int,int,float); x=0;y=0;z=0;

for(j=0;j<=n;j++) { b=blend(j,n,u); x=x+(p[j][0]*b); y=y+(p[j][1]*b); z=z+(p[j][2]*b); } } /*-------------------------------------------------*/ float blend(int j,int n,float u) { int k; float v,blend; v=C(n,j); for(k=0;k<j;k++) { v*=u; } for(k=1;k<=(n-j);k++) { v *= (1-u); } blend=v; return(blend); } /*-------------------------------------------------*/ C(int n,int j) { int k,a,c;

a=1; for(k=j+1;k<=n;k++) { a*=k; } for(k=1;k<=(n-j);k++) { a=a/k; } c=a; return(c); }

//OUTPUT

Experiment No. 10
Aim: Write a program to Implement B-Splines Curves. PROGRAM:

You might also like