45
Computer Graphics Practical By Mukhtiar Ahmed Kori Syed Salman Mehdi - 06IT29 QUEST Nawabshah 1 Practical – 01 Object: Perform a LINE statement activity. Tool: Turbo C++ compiler of Borland Language: C. Line Statement: The line statement we use in C programming for graphics and it is a part of GRAPHICS.H header file. Through line statement we can draw lines for creating shapes in our programs. In this practical we learn how to create lines and make shapes through line statement. It is necessary to create Computer Graphics through C programming. Syntax of Line statement: line(x1, y1, x2, y2); The syntax shows four values x1, y1, x2, y2. The first two values are x1, y1 are for initial point or pixel address where you start the line and the second part is x2, y2 which is for last of ending point of the line. How to make shapes? You can create lines with this type of simple programs. #include<graphics.h> #include<stdio.h> #include<conio.h> void main (void) { clrscr (); int gd,gm; gd=DETECT; initgraph (&gd, &gm,"c:\\tc\\bgi"); // initializing graphics line (10, 10, 600, 10); //simple line }/end of program. Output: Figure 1.1: How to draw Line in C Graphics mode.

Computer Graphics Practicals

Embed Size (px)

DESCRIPTION

These 12 practicals + a Game Project I have made in 7th Term when I was doing BS(IT) from QUEST Nawabshah. Also videos are available on the net , www.06it29.tk

Citation preview

Page 1: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 1

Practical – 01

Object: Perform a LINE statement activity. Tool: Turbo C++ compiler of Borland Language: C. Line Statement: The line statement we use in C programming for graphics and it is a part of GRAPHICS.H header file. Through line statement we can draw lines for creating shapes in our programs. In this practical we learn how to create lines and make shapes through line statement. It is necessary to create Computer Graphics through C programming. Syntax of Line statement: line(x1, y1, x2, y2); The syntax shows four values x1, y1, x2, y2. The first two values are x1, y1 are for initial point or pixel address where you start the line and the second part is x2, y2 which is for last of ending point of the line. How to make shapes? You can create lines with this type of simple programs. #include<graphics.h> #include<stdio.h> #include<conio.h> void main (void) { clrscr (); int gd,gm; gd=DETECT; initgraph (&gd, &gm,"c:\\tc\\bgi"); // initializing graphics line (10, 10, 600, 10); //simple line }/end of program. Output:

Figure 1.1: How to draw Line in C Graphics mode.

Page 2: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 2

Create a triangle line (20, 80, 60, 30); //triangle line (60, 30, 100, 80); line (20, 80, 100, 80); //complete triangle Create a Triangle in a Square. line (20, 90, 100, 90); //square with triangle line (20, 90, 20,150); //square line (20,150,100,150); line (100, 150, 100, 90); //square complete line (60, 90, 20,150); //triangle line (60, 90,100,150); //complete triangle Output:

Figure 1.2 How to draw a Triangle & Triangle in Square. Creating a Two triangle in a Cross on each other is: line (20,160,200,160); // square line (20, 160, 20,300); line (20,300,200,300); line (200,160,200,300); //square complete line (110, 170, 45,290); //triangle line (110,170,175,290); line (45,290,175,290); line (108,290,185,200); line (185, 200, 35,200); line (35,200,108,290); // triangle complete.

Page 3: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 3

Output:

Figure 1.3: How to draw Cross Line Triangle which intersects each other.

Creating a Star with Line statement: line (110, 320, 45,440); //only star line (110,320,175,440); line (45,440,185,350); line (185, 350, 35,350); line (35,350,175,440); //star complete Output:

Figure 1.4: How to draw a Star.

UHow to make a hut with line Statement

#include<graphics.h> #include<conio.h> #include<stdio.h> void main (void)

Page 4: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 4

{ int gd,gm; gd=DETECT; initgraph(&gd,&gm,"C:\\tc\\bgi"); line(140,80,480,80); //top horizontal line(140,80,72,180); //left point diagonal line(480,80,412,180); // right point diagonal -1 line(480,80,548,180); // right point diagonal -2 line(72,180,548,180); //mid Hut horizontal line(72,180,72,360); //mid Hut horizontal to vertical down line(412,180,412,360);//second vertical line(548,180,548,360); //third vertical line(72,360,548,360); // Bottom Line horizontal line(450,250,510,250); //door of the HUT line(450,250,450,360); line(510,250,510,360); line(462,120,495,120);//window line(462,120,462,150); line(495,120,495,150); line(462,150,495,150); //complete HUT getch(); }//end of Program. Output:

Figure 1.5: How to make a Hut through Line statement on C Graphics mode

Page 5: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 1

Practical – 02

Object: Draw circular shapes with ARC, CIRCLE & PIESLICE statements in C. Tool: Turbo C++ compiler of Borland Language: C. ARC Statement: The arc statement we use in C programming for draw curve on the screen and it is a part of GRAPHICS.H header file. With arc we can draw curves in the 0-360 degree dimensions. Mostly it use in drawings or graphical work in C. Syntax of ARC statement: arc (midx, midy, stangle, endangle, radius); The syntax of arc shows the values we are required for draw a shape of curve , which are depend on six type of value . all values are same like arc in pieslice But CIRCLE have on three type values midx,midy & radius , circle not use stangle(Start angle) & endangle(END ANGLE) parameters. A little description (midx, midy, stangle, endangle & radius). midx: It is the value of x (row pixel) point on the screen. midy: It is the value of y (column pixel) point on screen. stangle: It means start angle, from where the arc curve starts. endangle: It shows the end point in angle where the curve stop or limit of curve. A simple program to draw an ARC. #include<stdio.h> #include<conio.h> #include<graphics.h> void main(void) { clrscr(); int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); /*how draw an arc*/

Page 6: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 2

arc(320,240,0,135,50); getch(); }// end of program Output:

Figure 2.1 : How ARC draw on screen

PIESLICE shape drawing in graphical mode. It is also a function <graphics.h> header file which look a cut part of a circle. Like a piece of PIZZA . Syntax of PIESLICE. It has same attributes like arc . Pieslice(midx,midy,stangle,endangle,radius); A simple program example for drawing a pieslice. #include<stdio.h> #include<conio.h> #include<graphics.h> void main(void) { clrscr(); int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); /*how draw an pieslice*/ pieslice(320,240,0,135,50); getch(); }

Page 7: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 3

Output:

Figure 2.2 : How PIESLICE draw on screen

The circle drawing Normally in freehand we use a campus and a pencil with it to draw shape of a circle. In C give value center of the circle and then its radius. Syntax of the circle Circle(midx,midy,radius); A program to draw a circle. #include<stdio.h> #include<conio.h> #include<graphics.h> void main(void) { clrscr(); int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); /*how draw an circle*/ circle(320,240,50); getch(); } Output:

Page 8: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 4

Figure 2.3 : How CIRCLE draw on screen

A practical approach in which we use all statement like LINE,ARC,PIESLICE & Circle to draw some shapes.

#include<conio.h> #include<graphics.h> #include<stdio.h> void main(void) { clrscr(); int gd=DETECT,gm; initgraph(&gd,&gm,"C:\\tc\\bgi"); arc(250,180,90,270,105);//moon arc(220,180,90,270,100); /////////////////////////////////////////////////////// line(250,140,210,210); //Star line(250,140,290,210); line(210,210,290,160); //left line diagonal line(290,160,210,160); //Horizontal line(210,160,290,210); //right line diagonal /////////////////////////////////////////////////////// line(380,140,350,40); line(380,140,410,40); arc(380,65,40,140,40); arc(380,70,40,138,38); arc(380,75,40,136,34); pieslice(380,140,310,230,30); circle(380,160,20); circle(380,160,30); ////////////////////////////////////////////////////////////// line(320,0,320,480); line(0,300,640,300); //////////////////////////////////////////////////////// setcolor(RED+GREEN); circle(60,380,50); outtextxy(40,380,"CIRCLE"); ////////////////////////////////////////////////////////// setcolor(BLUE+WHITE+YELLOW); arc(160,380,0,135,30); outtextxy(160,380,"ARC"); //////////////////////////////////////////////////////////

Page 9: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 5

setcolor(YELLOW+RED+BLACK); pieslice(200,390,215,350,40); outtextxy(200,390,"PIESLICE"); ////////////////////////////////////////////////////////////// setcolor(GREEN); outtextxy(340,320,"Syed Salman Mehdi"); setcolor(GREEN+BLUE); outtextxy(340,340,"06IT29 QUEST Nawabshah"); setcolor(RED+WHITE+BLUE); outtextxy(340,360,"Practical #2,Computer Graphics"); getch(); }

Output:

Figure 2.4 : How make shape combination on screen

Page 10: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 1

Practical – 03

Object: Draw line shapes with help of “SETLINESTYLE” and Drawing a poly lines with “DRAWPOLY” statements in C. Tool: Turbo C++ compiler of Borland Language: C. The SETLINESTYLE statements With help setlinestyle statement we can draw four different kinds of lines, e.g. SOLID, DOTTED, DASHED & CENTER style lines. Syntax of SETLINESTYLE In the syntax we gave three values to make a line like: Setlinestyle(int linestyle , UNSIGNED UPATTERN , int thickness); With the help of simple program in C , I have made these lines .

Program # 1 #include<conio.h> #include<stdio.h> #include<graphics.h> #define _num 0 void main(void) { clrscr(); int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); setcolor(GREEN + WHITE); outtextxy(20,10,"Solid line"); setlinestyle(0,_num,1); // Simple / Solid line line(20,30,490,30); setcolor(GREEN + RED); outtextxy(20,50,"Dotted line"); setlinestyle(1,_num,1); // Dotted line line(20,70,490,70); setcolor(RED + BLUE); outtextxy(20,90,"Center line"); setlinestyle(2,_num,1); //Center line line(20,110,490,110); setcolor(BLUE + CYAN); outtextxy(20,130,"Dashed line");

Page 11: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 2

setlinestyle(3,_num,1); //Dashed line line(20,150,490,150); setcolor(15); outtextxy(140,170,"\\\\\\\\\\============06IT29============///////"); getch(); }

Figure shows how lines can be shown on the screen

The "DRAWPOLY" STATEMENT.

It is use to draw multiple lines within a single statement. In this statement we need to declare arrays which have all the line points where line direction you want to change. Then you it as parameter in DRAWPOLY. Syntax of DRAWPOLY(multilinking).

drawpoly(int numpoints, int far *polypoints);

Program # 2

Draw Parallelogram , Pantagonal , Hexagonal & Rectangle with help of

DRAWPOLY

#include<conio.h> #include<stdio.h> #include<graphics.h> #define _num 0 void main(void) { clrscr(); int gd=DETECT,gm;

Page 12: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 3

initgraph(&gd,&gm,"c:\\tc\\bgi"); setcolor(29 + 33); outtextxy(153,130,"RECTANGLE by DRAWPOLY Function"); setcolor(43 + 44 + 3); setlinestyle(0,_num,1); // Rectangle rectangle(180,140,360,260); setcolor(33 + 43); outtextxy(235,195,"Hexagonal"); //Hexagonal int points[]={200,200,240,160,300,160,340,200,300,240,240,240,200,200}; setcolor(29 + 33 + 43); drawpoly(7,points); setcolor(43 + 44 + 3); outtextxy(10,145,"Parallelogram"); //Parallelogram int points1[]={10,200,80,160,80,260,10,300,10,200}; setcolor(43 + 33); drawpoly(5,points1); setcolor(3 + 44); outtextxy(340,80,"Pantagonal"); int points2[]={320,80,380,40,440,80,410,120,350,120,320,80}; setcolor(33 + 3); drawpoly(6,points2); setcolor(15 + 29 + 33 + 43 + 44); outtextxy(140,370,"\\\\\\\\\\============06IT29============///////"); getch(); }

Figure shows that how the program effects have been shown on the screen.

Page 13: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 1

Practical – 04

Object: The Ellipse drawing technique in C. (activity #1) Tool: Turbo C++ compiler of Borland Language: C.

Usage in C:

The ellipse function in C graphics is to enhance the function of Circle, when you need more then a circle like a shape of an egg. Then you need to draw through ellipse, Because in ellipse you can able to declare vertical and horizontal radius which is not possible through circle function , also you can draw a traditional circle through this function , it possible when you make equal both vertical & horizontal radius of same size. Syntax of Ellipse in C: Ellipse (maxx, maxy, StAngle, EndAngle, xRadius, Yradius); Program # 1 #include<conio.h> #include<stdio.h> #include<graphics.h> void main (void) { clrscr (); int gd=DETECT, gm; initgraph (&gd, &gm,"c:\\tc\\bgi"); int xE=319,yE=239; /* Center of the Ellipse*/ int xRAD=150,yRAD; int stANGLE=0,endANGLE=360; for(yRAD=0;yRAD<100;yRAD+=10) { ellipse(xE,yE,stANGLE,endANGLE,xRAD,yRAD); } getch(); clrscr(); for(xRAD=0;xRAD<100;xRAD+=10) { ellipse(xE,yE,stANGLE,endANGLE,xRAD,yRAD); } getch();

Page 14: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 2

}

Output

Figure 1.a: Shows horizontally greater then Vertical Radius.

1.b: Shows vertically greater then Horizontal Radius.

A graphical animation with this Program of Ellipse:

Program # 2 #include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> void main(void) { int gd=DETECT, gm; initgraph(&gd,&gm,"C:\\tc\\bgi"); int xE=350,yE=200; // Center of the ellipse. int xRAD=150,yRAD; int STANGLE=0,ENDANGLE=360; for(yRAD=0;yRAD<100;yRAD+=10) {

ellipse(xE,yE,STANGLE,ENDANGLE,xRAD,yRAD); delay(2000); } //getch(); delay(2000); clrscr(); for(xRAD=0;xRAD<100;xRAD+=10) { setcolor(xRAD); ellipse(xE,yE,STANGLE,ENDANGLE,xRAD,yRAD); delay(2000); } getch();

Page 15: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 3

setcolor(yRAD); }

Figure 2: Shows colorful ellipse

Activity # 2

Object: Working with drawpoly (), fillpoly (), setfillstyle ().

Syntax

void far drawpoly(int numpoints, int far *polypoints); drawpoly draws the outline of a polygon

void far fillpoly(int numpoints, int far *polypoints); fillpoly draws and fills a polygon

void far setfillstyle(int pattern, int color); Sets the fill pattern and color

• fillpoly draws the outline of a polygon using the current line style and color, then fills the

polygon using the current fill pattern and fill color. • Each pair of integers gives the x and y coordinates of a point on the polygon.

Page 16: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 4

• To draw a closed figure with N vertices, you must pass N+1 coordinates to drawpoly, where the Nth coordinate == the 0th coordinate

• setfillstyle sets the current fill pattern and fill color. • To set a user-defined fill pattern, do not give a pattern of 12 (USER_FILL) to setfillstyle;

instead, call setfillpattern. • The enumeration fill_patterns, defined in GRAPHICS.H, gives names for the predefined fill

patterns, plus an indicator for a user-defined pattern. • Return Value: None

If invalid input is passed to setfillstyle, graphresult returns -11 (grError), and the current fill pattern and fill color remain unchanged. A simple program to draw on screen with all these three statements.

Program : #include<conio.h> #include<stdio.h> #include<graphics.h> void main (void) { clrscr(); int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); int LEFT=50,TOP=50,RIGHT=150,BOTTOM=180; int RIGHT_para[]={150,50,180,20,180,135,150,180,150,50}; int TOP_para[]={50,47,150,47,180,17,95,17,50,47}; int rect[]={50,50,150,50,150,180,50,180,50,50};

rectangle(LEFT,TOP,RIGHT,BOTTOM); drawpoly(4,RIGHT_para); drawpoly(5,TOP_para); getch(); setfillstyle(11,YELLOW); fillpoly(5,rect); setfillstyle(1,GREEN); fillpoly(5,RIGHT_para); setfillstyle(2,WHITE+RED); fillpoly(5,TOP_para); getch();

Figure 3: Shows both conditions whether figure in outline or in colors

Page 17: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 1

Practical – 05

Object: Working with “floodfill” as painting tool in C. Tool: Turbo C++ compiler of Borland Language: C.

Usage in C

The floodfill is use to paint the screen or specific area within boundaries through a point , i.e.-e in Circl ,Ellipse,Box, or any kind of shape which is fully locked a area or two dimensional . It is cannot applicable on open polygons and one dimensional image like a line between two points or arc (curve). With some programs I can show you how floodfill work and what the technique to use this statement in C. Statements of graphics that we are use & work in this practical are “floodfill, setfillstyle, and setcolor”. All other statements of drawing we are use previous practicals.

Syntax Floodfill Floodfill (int x, int y, int border);

(x,y) is a "seed point" If the seed is within an enclosed area, the inside will be filled. If the seed is outside the enclosed area, the exterior will be filled.

Setfillstyle Setfillstyle (int pattern, int color);

Page 18: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 2

Setcolor

Setcolor sets the current drawing color Setcolor (int color);

Program - 1

#include<graphics.h> #include<dos.h> #include<math.h> void main(void) { int gd=DETECT,gm; initgraph(&gd,&gm,"C:\\tc\\bgi"); int star[]={200,200,280,100,360,200,180,120,380,120,200,200}; setcolor(2); drawpoly(7,star); setfillstyle(1,3); floodfill(280,130,2); setfillstyle(2,1); floodfill(210,195,2); setfillstyle(3,2); floodfill(280,110,2);

setfillstyle(4,5); floodfill(350,190,2); setfillstyle(5,4); floodfill(190,121,2); setfillstyle(2,5); floodfill(370,121,2); setcolor(WHITE); outtextxy(330,125,"E"); outtextxy(220,125,"B"); outtextxy(275,110,"A"); outtextxy(240,160,"C"); outtextxy(310,160,"D"); for(int i=1;i<32;i++) { setcolor(i); outtextxy(263,135,"QUEST"); outtextxy(258,145,"06IT29"); delay(600); } getch(); }

Figure of program – 1

Page 19: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 3

Program – 2

#include<conio.h> #include<graphics.h> #include<stdio.h> #include<dos.h> void main(void) { int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); int y=10; /////////////////////////Top Line for(int r=10;r<=629;r+=20) { setcolor(YELLOW); circle(r,y,10); setfillstyle(1,RED+r); floodfill(r,y,YELLOW); delay(200); } /////////////////////////Left side line for(y=29;y<=469;y+=20) { setcolor(GREEN); circle(10,y,10); setfillstyle(1,y+RED); floodfill(10,y,GREEN); delay(200); } ///////////////////////Bottom Line for(r=29;r<=639;r+=20) { setcolor(GREEN); circle(r,469,10); setfillstyle(1,RED + r -19 ); floodfill(r,468,GREEN); delay(200); } ////////////////////////////Right Side Line for(y=9;y<=459;y+=20) { setcolor(GREEN); circle(629,y,10); setfillstyle(1,y+RED); floodfill(629,y,GREEN); delay(200); } ///////////////////////////////////// for(int j=1;j<12;j++) { setfillstyle(j+1,j+GREEN); bar(40,40,50,160);//Zero bar(40,40,100,50); bar(100,40,110,160); bar(40,160,110,150); setfillstyle(j,j+RED+0); bar(130,40,140,160);//SIX bar(130,40,190,50); bar(130,160,190,150); bar(190,150,180,100); bar(180,100,130,110); setfillstyle(j-5,j+YELLOW+1); bar(210,40,270,50);//I

bar(235,40,245,160); bar(210,160,270,150); setfillstyle(j-6,j+WHITE+0); bar(290,40,350,50);//T bar(315,40,325,160); setfillstyle(j-8,j+BLUE+3); bar(370,40,430,50);//2 bar(430,40,420,105); bar(420,105,370,95); bar(370,95,380,160); bar(380,160,430,150); setfillstyle(j-12,j+BLACK+7); bar(450,40,510,50);//9 bar(450,40,460,105); bar(460,105,510,95); bar(510,95,500,40); bar(510,105,500,160); bar(500,160,450,150); delay(300); } //////////////////////////////////////////// for(int s=1;s<=360;s++) { setcolor(13); ellipse(319,310,0,s,200,100); ellipse(319,310,0,s,180,90); ellipse(319,310,0,s,160,80); ellipse(319,310,0,s,140,70); ellipse(319,310,0,s,120,60); ellipse(319,310,0,s,210,110); delay(20); } /////////////////////////////////////////// setcolor(13); for(j=0;j<12;j++) { setfillstyle(1,j-1); floodfill(320,415,13); setfillstyle(1,1+j); floodfill(340,405,13); setfillstyle(1,j+6); floodfill(360,395,13); setfillstyle(1,RED+j); floodfill(380,380,13); setfillstyle(1,j-2); floodfill(400,360,13); setfillstyle(1,j); floodfill(319,310,13); delay(1000); } ////////////////////////////////////// getch(); }

Page 20: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 4

Program 2 pictures when program Run

Page 21: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 1

Practical – 06

Object: Working with “bar3d” as chart(graph) and 3d BOX making tool in C. Tool: Turbo C++ compiler of Borland Language: C.

Usage in C

The bar3d is a function of graphics.h header file which can simply draw 3D images on the screen. In this function we give value of x,y points which are for “upper left” and “bottom right” also we give it 3d values with next values or attributes of it of depth and height which make this 2D shape 3D. bar3d draws a three-dimensional rectangular bar, then fills it using the current fill pattern and fill color. If topflag is non-zero, a top is put on the bar. If topflag is 0, no top is put on the bar: This makes it possible to stack several bars on top of one another. To calculate a typical depth for bar3d, take 25% of the width of the bar, like this:

bar3d(left, top, right, bottom, (right-left)/4, 1);

Syntax

bar3d draws a three-dimensional rectangular bar, then fills it using the current fill pattern and fill color.

BAR3d bar3d(int left, int top, int right, int bottom,int depth, int topflag); int left & int top: These are initial points where the bar3d start to draw. int right & int bottom: These points are ending position where the image is limit. int depth & topglag: These points are main attributes of this function which are make image 3-D . Bar's depth in pixels. Topflag Governs whether a three-dimensional

Page 22: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 2

Program 6.0

OUTPUT:

Page 23: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 1

Practical – 07

Object: How can we use “bar3d” as chart(graph) for showing weekly earnings. Tool: Turbo C++ compiler of Borland Language: C.

Usage in C

The bar3d function is can be use to create graphs and charts to show reports, analysis etc, in a graphical picture .Mostly it is use as a 3-d picture/figure making tool in C language. But here we are trying to use it as chart making tool. With some changes in the practice # 6 are make a graph of weekly report earning through bar-3d. To calculate a typical depth for bar3d, take 25% of the width of the bar, like this:

bar3d(left, top, right, bottom, (right-left)/4, 1);

Syntax

bar3d draws a three-dimensional rectangular bar, then fills it using the current fill pattern and fill color.

BAR3d bar3d(int left, int top, int right, int bottom,int depth, int topflag); int left & int top: These are initial points where the bar3d start to draw. int right & int bottom: These points are ending position where the image is limit. int depth & topglag: These points are main attributes of this function which are make image 3-D . Bar's depth in pixels. Topflag Governs whether a three-dimensional

Page 24: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 2

Program 7.0

/* 29_PR_7.cpp*/ /* generaqtes Bar Chart graph */ #include<stdio.h> #include<conio.h> #include<graphics.h> #define N 6 /* number of values to graph */ #define BWIDTH 30 /* Width of each bar */ #define SEP 52 /* Separation between bars */ #define DI (BWIDTH+SEP) /* Distance from bar to bar */ #define SHFT 15 /* Between border and first bar */ #define WIDTH ((N+1) * DI) /* Width of CHART*/ #define LEFT 5 /* Left side of graph */ #define DEPTH 9 /* Depth of the bar */ #define TOPFLAG 3 /* Put 3d top on bar */ #define BOT 430 /* Bottome of Graph */ #define TOP 5 /* Top of the graph */ #define PPD (float) (BOT-TOP)/100 /* Pixel per data unit*/ void main(void) { int gd=DETECT,gm=CGAC1,error,j;/*Data to display */ int data[N]={41,47,76,73,52,65}; initgraph(&gd,&gm,"c:\\tc\\bgi"); rectangle(LEFT,TOP,LEFT+WIDTH,BOT); for(j=0;j<N;j++) /*Draw Bars*/ { setfillstyle(j+1,1+j%3); bar3d(LEFT+SHFT+j*DI,BOT-data[j]*PPD,LEFT+SHFT+j*DI+BWIDTH,BOT,DEPTH,TOPFLAG); } setcolor(LIGHTBLUE); outtextxy(20,440,"MON"); outtextxy(30,220,"4100"); setcolor(LIGHTGREEN); outtextxy(100,440,"TUE"); outtextxy(110,200,"4700"); setcolor(LIGHTCYAN); outtextxy(190,440,"WED"); outtextxy(200,80,"7600");

setcolor(LIGHTMAGENTA); outtextxy(270,440,"THU"); outtextxy(280,90,"7300"); setcolor(BROWN); outtextxy(350,440,"FRI"); outtextxy(360,180,"5200"); setcolor(LIGHTGRAY); outtextxy(430,440,"SAT"); outtextxy(440,120,"6500"); setcolor(RED); outtextxy(480,440,"=>>>WEEKDAYS"); setcolor(RED); setfillstyle(1,LIGHTGREEN); rectangle(10,10,575,70); floodfill(20,20,RED); setcolor(BLACK); settextstyle(GOTHIC_FONT,HORIZ_DIR,4); outtextxy(180,15,"3-D"); settextstyle(TRIPLEX_FONT,HORIZ_DIR,4); outtextxy(260,15,"BAR"); outtextxy(340,15,"CHART"); setcolor(15); rectangle(480,80,565,420); setfillstyle(1,7); floodfill(500,100,15); setcolor(16); settextstyle(SANS_SERIF_FONT,VERT_DIR,3); outtextxy(480,300,"06IT29"); outtextxy(480,220,"QUEST"); outtextxy(480,80,"NAWABSHAH"); setcolor(LIGHTRED); settextstyle(SANS_SERIF_FONT,VERT_DIR,6); outtextxy(500,130,"CG-PR#7"); getch(); closegraph(); }

Page 25: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 3

OUTPUT:

Figure 7.0:Shows how we can use BAR-3D to make CHARTS.

Page 26: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 1

Practical – 08

Object: How can we restore Image from Memory to Screen and make logics to move that image? Tool: Turbo C++ compiler of Borland Language: C. Different graphics function are use in this program which are :

Pointers , unsigned , getimage, putimage ,kbhit(). POINTERS Pointers can also be declared as void.Void pointers can't be dereferenced without explicit casting. This is because the compiler can't determine the size of the object the pointer points to. Void *ballbuff

Unsigned (declaration) A type modifier alters the meaning of the base data type to yield a new type .Each of these type modifiers can be applied to the base type int. The modifier unsigned can also be applied to the base type char. i-e unsigned size;

PutImage & GetImage getimage, putimage getimage saves a bit image of the specified region into memory putimage outputs a bit image onto the screen

Syntax void far getimage(int left, int top, int right, int bottom, void far *bitmap ; void far putimage(int left, int top, void far *bitmap, int op); Note: The function is given the top left-hand cornor of the area where image will go, the address in memory holding the image , and an operator value. The possible values for this operator, as defined in graphics.h, are:

Page 27: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 2

Value Constant Comment 0 1 2 3 4

COPY_PUT XOR_PUT OR_PUT AND_PUT NOT_PUT

Replace the old image with new Old and new Images OR old and new images AND old and new images Replace with inverse of new image

The OPERATOR can be used to achieve different effects . Typicallya new image is drawn with the operator set COPY_PUT,and the old imageis erased with it set to XOR_PUT. The effect of XORing one image with the same image to erase it. getimage copies an image from the screen to memory. putimage puts the bit image previously saved with getimage back onto the screen, with the upper left corner of the image placed at (left,top). Argument:What It Is/Does Bitmap:Points to the area in memory where the bit image is stored. The first two words of this area are used for the width and height of the rectangle. The remainder holds the image itself. Bottom:(left, top) and (right, bottom) define the rectangular screen Left: area from which getimage copies the bit image. Right (left, top) is where putimage places the upper left corner of top the stored image. Op: Specifies a combination operator that controls how the color for each destination pixel onscreen is computed, based on the pixel already onscreen and the corresponding source pixel in memory. The enumeration putimage_ops, defined in GRAPHICS.H, gives names to the putimage combination operators.

KBHIT() Checks for currently available keystrokes SYNTAX : int kbhit(void); NOTE: kbhit checks to see if a keystroke is currently available. Any available keystrokes can be retrieved with getch or getche.

Page 28: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 3

Program # 8.1

imagecpp/* . */ bouncingbal l created frombi t image/* */

i nclude graphicsh# < . > i nclude conioh# < . > i nclude stdioh# < . > i nclude mal loch# < . > i nclude dosh# < . > defineLEFT# 0 define# TOP 0 defineRIGHT# 639 defineBOTTOM# 479 defineRADIUS# 8 defineCGAC LIGHTGREEN# 0_ 1

voidmainvoid ( ) { cl rscr(); i nt gd DETECTgm = , ; i nt xydxdyoldxoldy , , , , , ; bal l coordinates/* */ void bal lbuff * ; pointer to/* buffer */ unsignedsize ; sizeof/* buffer */ i ni tgraph gd gmc(& ,& ," :\\tc\\bgi "); rectangleLEFTTOPRIGHTBOTTOM( , , , ); x y RADIUS= = +10; setcolor CGAC LIGHTGREEN( 0_ );

setfi l l styleSOLIDFILLCGAC LIGHTGREEN( _ , 0_ ); ci rclexyRADIUS( , , ); floodfi l l xyCGAC LIGHTGREEN( , , 0_ ); size imagesizex = ( -RADIUSy, -RADIUSx RADIUSy RADIUS, + , + ); bal lbuff void mal locsize = ( *) ( ); get memory/* for image */ /* place image intomemory */ getimagex( -RADIUSy, -RADIUSx RADIUSy RADIUSbal lbuff, + , + , ); dx speedof bal l=2; /* */ dy=1; whi le kbhi t(! ()) { putimagex( -RADIUSy, -RADIUSba, l lbuff COPYPUT, _ ); oldx x oldy y= ; = ; remeber where i t was/* */ x dx y dy+= ; += ; move i ts coordinates/* */ reflect i t at edges/* */ i f x LEFT RADIUS x RIGHT( <= + +2 || >= -RADIUS-2) dx=-dx; i f y TOPRADIUS y BOTTOM( <= + +1 || >= -RADIUS-1) dy=-dy; putimageoldx( -RADIUSoldy, -RADIUSbal lbuff XORPUT, , _ ); delay(10); } closegraph(); }

Page 29: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 4

Page 30: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 1

Practical – 09

Object # 1: How can we use PIESLICE function for graphical chart functions? Object # 2: How can we apply SINE (trigonometric function) with C graphics in putpixel function? Tool: Turbo C++ compiler of Borland Language: C. The SIN functions in C. For using sin trigonometric function we need COMPLEX.H & MATH.H header files.

DECLARETION

REAL COMPLEX

long double sinl(long double x); complex sin(complex z);

double sin(double x);

Real versions sin and sinl compute the sine of the input value Angles are specified in radians. Complex versions Complex sine: sin(z) = (exp(i * z) - exp(-i * z)) / (2i) Return Value: On success, sin and sinl return the sine of the input value (in the range -1 to 1) Error handling for these routines can be modified via matherr and _matherrl.

Page 31: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 2

Activity # 1 #include<stdio.h> #include<graphics.h> #include<conio.h> void main(void) { clrscr(); int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); int x=getmaxx()/2+150,y=getmaxy()/2,radius=150;

/////////////INFO////////////////// setcolor(YELLOW); rectangle(10,10,20,20); outtextxy(25,13,"20% Children"); setfillstyle(3,YELLOW); floodfill(15,15,YELLOW); setcolor(LIGHTGREEN); rectangle(10,30,20,40); outtextxy(25,33,"20% Female"); setfillstyle(4,LIGHTGREEN); floodfill(15,35,LIGHTGREEN); setcolor(BROWN);

rectangle(10,50,20,60); outtextxy(25,53,"30% Educated"); setfillstyle(5,BROWN); floodfill(15,55,BROWN); setcolor(LIGHTRED); rectangle(10,70,20,80); outtextxy(25,73,"30% Male"); setfillstyle(2,LIGHTRED); floodfill(15,75,LIGHTRED); /////////////////////// setcolor(15); setfillstyle(2,LIGHTRED); pieslice(x,y,0,108,radius); setfillstyle(3,YELLOW); pieslice(x,y,108,180,radius); setfillstyle(4,LIGHTGREEN); pieslice(x,y,180,252,radius); setfillstyle(5,BROWN); pieslice(x,y,252,360,radius); getch(); }

Figure 9.1: Shows how you can use PIESLICE for making graphical reports in C language.

Page 32: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 3

Activity # 2

#include<stdio.h> #include<graphics.h> #include<conio.h> #include<math.h> #include<dos.h> void main(void) { clrscr(); int gd=DETECT,gm; double angle,sinofA; int x,y; initgraph(&gd,&gm,"c:\\tc\\bgi");

int lx=getmaxx(),ly=getmaxy()/2; line(0,ly,lx,ly); for(x=0;x<lx;x++) { angle=((double)x/lx)*(2*3.142); sinofA=sin(angle); y=ly-ly*sinofA; delay(10); putpixel(x,y,x); setcolor(x); outtextxy(x+20,y,"06IT29"); } getch(); }

Figure 9.2: shows how you can draw a sinusoidal wave with help of sine function and putpixel.

Page 33: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 4

An extra graphical example by using activity #2 technique, just replacing putpixel with out text and I have made this!

Page 34: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 1

Practical – 10 Object: How to create a shape with user defined function and replicate it on whole screen? Tool: Turbo C++ compiler of Borland Language: C. New function which we are using in this practical are:

moveto moveto moves the CP to (x, y) Syntax: void far moveto(int x, int y); moveto moves the current position (CP) to viewport position (x, y).

linerel

linerel draws a line from the CP to a point that is a relative distance (dx, dy) from the CP, then advances the CP by (dx, dy). Syntax: void far linerel(int dx, int dy); Program : 10.1 #include<graphics.h> #include<stdio.h> #include<conio.h> #define MAX 640 #define GRID 80 #define SIDE 72 void square(int side); void main(void) {int gd=DETECT,gm; int x,y; initgraph(&gd,&gm,"c:\\tc\\bgi"); for(y=0; y<MAX; y+=GRID) for(x=0;x<MAX;x+=GRID) {setcolor(15);

moveto(x,y); square(SIDE); setcolor(15); setfillstyle(1,LIGHTGREEN); floodfill(x+10,y+5,15); setcolor(BROWN); settextstyle(2,1,2); outtextxy(x+4,y+22,"06IT29"); setcolor(LIGHTRED); settextstyle(2,0,3); outtextxy(x+2,y+2,"Computer Graphics"); setcolor(LIGHTBLUE); settextstyle(2,1,2);

outtextxy(x+64,y+5,"Syed Salman Mehdi"); setcolor(BLACK); settextstyle(2,0,3); outtextxy(x+3,y+62,"QUEST NAWABSHAH"); setcolor(BLUE+RED); settextstyle(2,0,6); outtextxy(x+15,y+20,"PR-10");} getche();} void square(int side) {linerel(side,0); linerel(0,side); linerel(-side,0); linerel(0,-side);}

Page 35: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 2

Fig 10.1: Drawing boxes like checkers on the screen.

Fig10.2 :Filling the boxes with flood fill and writing text on them.

Page 36: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 1

Practical – 11 Object: Rotation in graphical pictures with some conditional codes. Tool: Turbo C++ compiler of Borland Language: C. Activity#1 : Rotating a COIN #include<graphics.h> #include<alloc.h> #include<conio.h> #include<dos.h> #define CGAC0_LIGHTGREEN 1 #define XC 160 #define YC 100 #define RAD 25 #define N 8 #define DELAY 20 void main(void) { int xRad; void *buff[N]; unsigned size; int j,mode; int driver=DETECT; initgraph(&driver,&mode,"c:\\tc\\bgi"); size=imagesize(XC-RAD,YC-RAD,XC+RAD,YC+RAD); setcolor(CGAC0_LIGHTGREEN); for(j=0; j<N; j++) { xRad=j*RAD/(N-1);

if(j==0) xRad=1; cleardevice(); ellipse(XC,YC,0,360,xRad,RAD); buff[j]=(void *)malloc(size); getimage(XC-RAD,YC-RAD,XC+RAD,YC+RAD,buff[j]); } while(!kbhit()) { for(j=0; j<N; j++) { putimage(XC-RAD,YC-RAD,buff[j],COPY_PUT); delay(DELAY); putimage(XC-RAD,YC-RAD,buff[j],XOR_PUT);} for(j=N-1; j>0; j--) { putimage(XC-RAD,YC-RAD,buff[j],COPY_PUT); delay(DELAY); putimage(XC-RAD,YC-RAD,buff[j],XOR_PUT);}} closegraph();}

Fig: 11.1 shows how coin display on screen and rotate.

Page 37: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 2

Activity#2 : Rotating a Circle on the wedge /* Program to show Animation */ #include<graphics.h> #include<stdio.h> #include<conio.h> #include<dos.h> #include<math.h> int dx,dy,s1,s2,dp,swap=0; void bressenham(int x1,int x2,int y1,int y2) {dx=abs(x2-x1); dy=abs(y2-y1); if(x2<x1) s1=-1; else if(x2>x1) s1=1; else s1=0; if(y2<y1) s2=-1; else if(y2>y1) s2=1; else s2=0; dp=2*dy-dx; if(dy>dx) { int temp=dy; dy=dx; dx=temp; swap=1;}} void erase(int x,int y,int r,int x3,int y3,int x4,int y4 ,int x5,int y5,int x6,int y6) { setcolor(BLACK); circle(x,y,r); line(x3,y3,x4,y4); line(x5,y5,x6,y6);} void main() { clrscr(); int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); setcolor(WHITE); int x1=10,y1=50,h,b,x3,y3,x4,y4,x5,y5,x6,y6,x,y,r; printf("Enter the height of the wedge : "); scanf("%d",&h); printf("Enter the width of the edge : "); scanf("%d",&b); printf("Enter the radius of the circle : "); scanf("%d",&r); int x2=x1+b,y2=y1+h; cleardevice();

line(x1,y1,x2,y2); line(x1,y1,x1,y2); line(x1,y2,x2,y2); float angle=atan((float)(y2-y1)/(x2-x1)); x=x1+r*sin(angle); y=y1-r*cos(angle); x3=x; y3=y+r; x4=x; y4=y-r; x5=x+r; y5=y; x6=x-r; y6=y; line(x3,y3,x4,y4); line(x5,y5,x6,y6); circle(x,y,r); float ang=1/r; bressenham(x1,x2,y1,y2); while(x<(x2+r*sin(angle)) && y<(y2-r*cos(angle))) { ang+=(float)1/r; erase(x,y,r,x3,y3,x4,y4,x5,y5,x6,y6); setcolor(WHITE); line(x1,y1,x2,y2); if(dp<0) { if(swap) y=y+s2; else x=x+s1; dp+=2*dy;} else { x=x+s1; y=y+s2; dp=dp+2*dy-2*dx;} x3=x+(float)r*cos(-ang); x4=x-(float)r*cos(-ang); y3=y-(float)r*sin(-ang); y4=y+(float)r*sin(-ang); x5=x-(float)r*sin(+ang); x6=x+(float)r*sin(+ang); y5=y+(float)r*cos(+ang); y6=y-(float)r*cos(+ang); line(x3,y3,x4,y4); line(x5,y5,x6,y6); circle(x,y,r); delay(10);} getch();}

Fig: 11.2 shows the program first get the input user then show the rotation on the wedge.

Page 38: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 3

Activity#3 : Rotation of a Circular shape on x-axis : #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> #define PI 3.1415 #include<dos.h> void rotate_wheel(int xc,int yc,int t) { int x,y; for(t=t;t<180;t=t+60) { setcolor(RED); x=50*cos(t*PI/180); y=50*sin(t*PI/180); line(xc+x,yc+y,xc-x,yc-y); //bar(xc+x,yc+y,xc-x,yc-y);} setcolor(LIGHTBLUE); circle(xc,yc,50); circle(xc,yc,53); setcolor(YELLOW);

outtextxy(xc-36,yc,"-<06IT29>-");} void main() { int d=0,m=0,x; int y1=240; initgraph(&d,&m,"c:\\tc\\bgi"); while(!kbhit()) { for(x=0;x<640;x++) { rotate_wheel(x,y1,x%60); sound(6*x); delay(6); nosound(); cleardevice(); rotate_wheel(x,240,x%60);} if(x>=639) { x=0;} } getch(); closegraph();}

Fig: 11.3 shows how a circle shown on the screen, which from left to right till any key press.

Page 39: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 1

Practical – 12 Object: How put text on screen in graphical mode with varieties? Tool: Turbo C++ compiler of Borland Language: C. Activity#1 : Draw Text Wider,Taller , normal & mix style on the screen. /*Different Varieties of TEXT*/ #include<graphics.h> #include<conio.h> #include<stdio.h> void main(void) {int gd=DETECT,gm,j; int multx,divx,multy,divy; initgraph(&gd,&gm,"c:\\tc\\bgi"); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,USER_CHAR_SIZE); outtext("Normal"); moveto(0,60);

setusercharsize(multx=2,divx=1,multy=1,divy=1); outtext("Wider"); moveto(0,120); setusercharsize(multx=1,divx=1,multy=3,divy=2); outtext("Taller"); moveto(0,180); setusercharsize(multx=1,divx=2,multy=1,divy=2); outtext("Half as Wide and Tall"); getch();}

Fig 12.1 , shows how you can draw text in different varieties .

Activity#2 : Draw text in different font style & Directions. /*How to write text with different style and directions in graphics mode */ #include<stdio.h> #include<conio.h> #include<graphics.h> #define FONT_SIZE 4 void main(void) {clrscr(); int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); settextstyle(GOTHIC_FONT,HORIZ_DIR,FONT_SIZE); outtext("Gothic "); settextstyle(TRIPLEX_FONT,HORIZ_DIR,FONT_SIZE);

outtext("Triplex"); moveto(0,60); settextstyle(SMALL_FONT,HORIZ_DIR,FONT_SIZE); outtext("small "); moveto(60,60); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,FONT_SIZE); outtext("Sanserif"); moveto(0,120); settextstyle(DEFAULT_FONT,HORIZ_DIR,FONT_SIZE); outtext("Default"); moveto(250,0); settextstyle(TRIPLEX_FONT,VERT_DIR,FONT_SIZE); outtext("Triplex"); getch();}

Page 40: Computer Graphics Practicals

Computer Graphics Practical By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 2

Fig: 12.2 shows how you can draw text in vertical and horizontal direction. Activity#3 : Justify Text in LEFT,RIGHT or CENTER manner. /*How to justify text */ #include<graphics.h> #include<conio.h> #include<stdio.h> #define CL 150 #define LEAD 40 #define FONT_SIZE 3 void main(void) {int gd=DETECT,gm,j; initgraph(&gd,&gm,"c:\\tc\\bgi"); settextstyle(TRIPLEX_FONT,HORIZ_DIR,FONT_SIZE); line(CL,0,CL,200); for(j=0;j<LEAD*5;j+=LEAD) line(0,j,300,j);

moveto(CL,LEAD); settextjustify(LEFT_TEXT,TOP_TEXT); outtext("Left-Top"); moveto(CL,LEAD*2); settextjustify(RIGHT_TEXT,TOP_TEXT); outtext("Right-Top"); moveto(CL,LEAD*3); settextjustify(CENTER_TEXT,TOP_TEXT); outtext("Center-Top"); moveto(CL,LEAD*4); settextjustify(CENTER_TEXT,BOTTOM_TEXT); outtext("Center-BOTTOM"); getch();}

Fig: 12.3 shows how you can justify text in different position of the screen.

Page 41: Computer Graphics Practicals

Computer Graphics Project With respect of Sir. Mukhtiar Ahmed Kori

06 IT 29/33/43/44/tb-03 1

HIT ARCHER (GAME) Tool: Turbo C++ compiler of Borland Language: C.

About the Game. The game is simply designed in the BORLAND C++ Compiler with using simple C language functions of “<GRAPHI.H>” library. We have made some graphical functions and call them simultaneously, also the polygon shapes also with drawpoly function and pointers also use in this program. We have use in it an EGA Driver & EGAHI mode to create display in High Quality. This is the representation of all the activities which we had practices and learn during the class , from drawing a simple LINE, CIRCLE, ELLPSE , LOOPS, POINTERS , MOVING OBJECT , ARC etc . We use approximately all function in this program are combined together. In this game an egg (ellipse) is below up from bottom to top in straight direction from right side of the screen. On the left the archer is function , when the egg is come out from the right bottom , prepare for hitting it , at correct time hit it , then it can explode , otherwise it will cross the screen from top. You have ten chances to hit the ball. The score is showing below on left bottom side of the screen. After targeting all ten hits game is over and ask you that “Play again y/n”. If you want to play again press y then press enter otherwise press n the any other key to terminate the game.

Page 42: Computer Graphics Practicals

Computer Graphics Project With respect of Sir. Mukhtiar Ahmed Kori

06 IT 29/33/43/44/tb-03 2

Starting of the Project.

The figure shows how EGG is moving from right-bottom to right-top & the arrow is ready to hit the EGG.

Page 43: Computer Graphics Practicals

Computer Graphics Project With respect of Sir. Mukhtiar Ahmed Kori

06 IT 29/33/43/44/tb-03 3

The figure shows after hitting all ten hits, screen will look this and the points which you scored are shown below on left-bottom position.

After completing a game session it will ask for “play again” or want to terminate it here.

Page 44: Computer Graphics Practicals

Computer Graphics Project With respect of Sir. Mukhtiar Ahmed Kori

06 IT 29/33/43/44/tb-03 4

The Program CODE

/* This project is made by Syed Salman Mehdi (06IT29) */ #include<graphics.h> #include<stdlib.h> #include<stdio.h> #include<conio.h> #include<dos.h> #include<string.h> void score(int i) { setcolor(10); settextstyle(2,0,1); outtextxy(25+i+5,(getmaxy())-25,"*");} void erase(int x) {setcolor(9); settextstyle(2,0,1); outtextxy(25+x+5,(getmaxy())-25,"*");} void main() {/* select a driver and mode that supports */ /*multiple background colors*/ int gdriver = EGA, gmode = EGAHI, errorcode; int poly1[20],poly2[20],i,j,k,m=0,n=225,x,y,z,p; int poly[20],a=0,b,s=0; char ch,ans='y',msg[80]; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); /* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ {printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* terminate with an error code */} ///////////////////// setcolor(LIGHTRED); circle(319,239,200); setfillstyle(1,LIGHTGREEN); floodfill(319,239,LIGHTRED); settextstyle(1,0,5); outtextxy(10,10,"Project of Computer Graphics"); settextstyle(1,0,1); outtextxy(10,70,"Re-Edit by : Syed Salman Mehdi (06IT29)"); outtextxy(10,100,"Subject Teacher : Sir Mukhtiar Ahmed Kori"); getch(); cleardevice(); //////////////////////////////////////

poly[13]=m+26; poly[14]=i; poly[15]=m+26; poly[16]=i+2; poly[17]=m+25; setcolor(4); setfillstyle(1,4); fillpoly(9,poly); m=m+25; a++;} /*baloon*/ setcolor(12); setfillstyle(1,BROWN); fillellipse(x-50,y-50,15,20); b=10; setcolor(10); //getch(); while(b>0) {setcolor(4); setfillstyle(1,4); i=5; poly[0]=i+2; poly[1]=25; poly[2]=i; poly[3]=24; poly[4]=i+55; poly[5]=24; poly[6]=i+50; poly[7]=20; poly[8]=i+60; poly[9]=25; poly[10]=i+50; poly[11]=30; poly[12]=i+55; poly[13]=26; poly[14]=i; poly[15]=26; poly[16]=i+2; poly[17]=25; fillpoly(9,poly); setcolor(14); line(5,25,39,3); line(5,25,39,47); for(k=y-50;k>0&&(!kbhit());k--) {setcolor(10); setfillstyle(1,10); fillellipse(x-50,k,15,20); delay(15); setcolor(9); setfillstyle(1,9); fillellipse(x-50,k,20,20);} j=5; if(k==0&&b==1) {setcolor(9); line(5,25,39,3); line(5,25,39,47); setcolor(14); line(39,3,39,47); line(50,24,50,26);} else if(k==0) { setcolor(4); setfillstyle(1,4);

poly[15]=26; poly[16]=i+2; poly[17]=25; fillpoly(9,poly); setcolor(10); setfillstyle(1,10); fillellipse(x-50,z,15,20); //-------- sound(4000); delay(15); nosound(); if(j<35) {setcolor(9); line(j,25,39,3); line(j,25,39,47); j++; } //burst if(poly[8]>=x-70&&poly[8]<x&&z<=45&&z>=10) { setcolor(9); setfillstyle(1,9); fillellipse(x-50,z,20,20); setcolor(10); setfillstyle(1,10); poly1[0]=x-70; poly1[1]=z+5; poly1[2]=x-50; poly1[3]=z-10; poly1[4]=x-30; poly1[5]=z+5; poly1[6]=x-70; poly1[7]=z+5; poly2[0]=x-70; poly2[1]=z-5; poly2[2]=x-30; poly2[3]=z-5; poly2[4]=x-50; poly2[5]=z+10; poly2[6]=x-70; poly2[7]=z-5; fillpoly(4,poly1); fillpoly(4,poly2); s=s+5; delay(5); score(s); setcolor(9); setfillstyle(1,9); fillpoly(4,poly1); fillpoly(4,poly2); fillpoly(9,poly); break;} setcolor(9); setfillstyle(1,9); fillpoly(9,poly); fillellipse(x-50,z,20,20); line(39,3,39,47); line(50,24,50,26);} } setcolor(9); setfillstyle(1,9); fillellipse(x-

Page 45: Computer Graphics Practicals

Computer Graphics Project With respect of Sir. Mukhtiar Ahmed Kori

06 IT 29/33/43/44/tb-03 5

/* for centering text messages */ settextjustify(CENTER_TEXT, CENTER_TEXT); x = getmaxx();// / 2; y = getmaxy();// / 2; setbkcolor(9); getch(); /*score board*/ setcolor(16); rectangle(5,y-55,105,y-5); setcolor(10); rectangle(8,y-52,102,y-8); setcolor(14); settextstyle(1,0,1); outtextxy(55,y-45,"POINTS"); setcolor(12); /*name*/ setcolor(LIGHTRED); settextstyle(2,0,4); outtextxy(x/2,y-10,"Re-Edited By: Syed Salman Mehdi (06IT29)"); setcolor(12); while(ans=='y') {/*bow*/ j=5; i=5; setcolor(14); arc(5,25,315,45,45); line(5,25,39,3); line(5,25,39,47); setcolor(9); line(39,3,39,47); line(50,24,50,26); a=0; m=0; n=225; s=0; for(p=5;p<65;p=p+5) {erase(p);} while(a<10) { poly[0]=i+2; poly[1]=m+25; poly[2]=i; poly[3]=m+24; poly[4]=i+55; poly[5]=m+24; poly[6]=i+50; poly[7]=m+20; poly[8]=i+60; poly[9]=m+25; poly[10]=i+50; poly[11]=m+30; poly[12]=i+55;

fillpoly(9,poly);} else {for(i=5,z=k;i<x||z>0;z--,i=i+5) {//if(z==0) //break; if(j<35) {setcolor(14); line(j,25,39,3); line(j,25,39,47);} if(j==35) { setcolor(14); line(39,3,39,47); line(50,24,50,26);} setcolor(4); setfillstyle(1,4); //m=0; poly[0]=i+2; poly[1]=25; poly[2]=i; poly[3]=24; poly[4]=i+55; poly[5]=24; poly[6]=i+50; poly[7]=20; poly[8]=i+60; poly[9]=25; poly[10]=i+50; poly[11]=30; poly[12]=i+55; poly[13]=26; poly[14]=i;

50,z,20,20); line(39,3,39,47); line(50,24,50,26); i=5; poly[0]=i+2; poly[1]=n+25; poly[2]=i; poly[3]=n+24; poly[4]=i+55; poly[5]=n+24; poly[6]=i+50; poly[7]=n+20; poly[8]=i+60; poly[9]=n+25; poly[10]=i+50; poly[11]=n+30; poly[12]=i+55; poly[13]=n+26; poly[14]=i; poly[15]=n+26; poly[16]=i+2; poly[17]=n+25; delay(10); fillpoly(9,poly); n=n-25; b--; fflush(stdin); getch(); }//end of 1 game setcolor(14); line(39,3,39,47); line(50,24,50,26); setcolor(14); settextstyle(2,0,4); outtextxy(x/2,y/2,"Play Again y/n"); ans=getch(); setcolor(9); settextstyle(2,0,4); outtextxy(x/2,y/2,"Play Again y/n"); } //clean up getch(); restorecrtmode(); closegraph();}