Anda di halaman 1dari 45

Computer Graphics Practical

By Mukhtiar Ahmed Kori

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.

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 1


Computer Graphics Practical
By Mukhtiar Ahmed Kori

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.

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 2


Computer Graphics Practical
By Mukhtiar Ahmed Kori

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.

How to make a hut with line Statement


U

#include<graphics.h>
#include<conio.h>
#include<stdio.h>

void main (void)

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 3


Computer Graphics Practical
By Mukhtiar Ahmed Kori
{
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

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 4


Computer Graphics Practical
By Mukhtiar Ahmed Kori

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

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 1


Computer Graphics Practical
By Mukhtiar Ahmed Kori
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();

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 2


Computer Graphics Practical
By Mukhtiar Ahmed Kori

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:

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 3


Computer Graphics Practical
By Mukhtiar Ahmed Kori

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

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 4


Computer Graphics Practical
By Mukhtiar Ahmed Kori
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

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 5


Computer Graphics Practical
By Mukhtiar Ahmed Kori

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

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 1


Computer Graphics Practical
By Mukhtiar Ahmed Kori
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;

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 2


Computer Graphics Practical
By Mukhtiar Ahmed Kori
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.

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 3


Computer Graphics Practical
By Mukhtiar Ahmed Kori

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

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 1


Computer Graphics Practical
By Mukhtiar Ahmed Kori
}

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 ellipse(xE,yE,STANGLE,ENDANGLE,xRAD,yRAD);
#include<stdio.h> delay(2000);
#include<conio.h> }
#include<graphics.h> //getch();
#include<dos.h> delay(2000);
void main(void) clrscr();
{ for(xRAD=0;xRAD<100;xRAD+=10)
int gd=DETECT, gm; {
initgraph(&gd,&gm,"C:\\tc\\bgi"); setcolor(xRAD);
int xE=350,yE=200; // Center of the ellipse. ellipse(xE,yE,STANGLE,ENDANGLE,xRAD,yRAD);
int xRAD=150,yRAD; delay(2000);
int STANGLE=0,ENDANGLE=360; }
for(yRAD=0;yRAD<100;yRAD+=10)
{ getch();

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 2


Computer Graphics Practical
By Mukhtiar Ahmed Kori
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.

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 3


Computer Graphics Practical
By Mukhtiar Ahmed Kori
• 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> rectangle(LEFT,TOP,RIGHT,BOTTOM);
#include<stdio.h> drawpoly(4,RIGHT_para);
#include<graphics.h> drawpoly(5,TOP_para);

void main (void) getch();


{
clrscr(); setfillstyle(11,YELLOW);
fillpoly(5,rect);
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi"); setfillstyle(1,GREEN);
int LEFT=50,TOP=50,RIGHT=150,BOTTOM=180; fillpoly(5,RIGHT_para);

int RIGHT_para[]={150,50,180,20,180,135,150,180,150,50}; setfillstyle(2,WHITE+RED);


int TOP_para[]={50,47,150,47,180,17,95,17,50,47}; fillpoly(5,TOP_para);
int rect[]={50,50,150,50,150,180,50,180,50,50};
getch();

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


colors

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 4


Computer Graphics Practical
By Mukhtiar Ahmed Kori

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

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 1


Computer Graphics Practical
By Mukhtiar Ahmed Kori

Setcolor

Setcolor sets the current drawing color

Setcolor (int color);

Program - 1
#include<graphics.h> setfillstyle(4,5);
#include<dos.h> floodfill(350,190,2);
#include<math.h>
void main(void) setfillstyle(5,4);
floodfill(190,121,2);
{
int gd=DETECT,gm; setfillstyle(2,5);
initgraph(&gd,&gm,"C:\\tc\\bgi"); floodfill(370,121,2);
int
star[]={200,200,280,100,360,200,180,120,380
,120,200,200}; setcolor(WHITE);
outtextxy(330,125,"E");
setcolor(2); outtextxy(220,125,"B");
outtextxy(275,110,"A");
drawpoly(7,star); outtextxy(240,160,"C");
outtextxy(310,160,"D");
setfillstyle(1,3);
floodfill(280,130,2); for(int i=1;i<32;i++)
{
setcolor(i);
setfillstyle(2,1); outtextxy(263,135,"QUEST");
floodfill(210,195,2); outtextxy(258,145,"06IT29");
delay(600);
setfillstyle(3,2); }
floodfill(280,110,2); getch();

Figure of program – 1

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 2


Computer Graphics Practical
By Mukhtiar Ahmed Kori

Program – 2
#include<conio.h> bar(235,40,245,160);
#include<graphics.h> bar(210,160,270,150);
#include<stdio.h>
#include<dos.h> setfillstyle(j-6,j+WHITE+0);
void main(void) bar(290,40,350,50);//T
{ bar(315,40,325,160);
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi"); setfillstyle(j-8,j+BLUE+3);
int y=10; bar(370,40,430,50);//2
/////////////////////////Top Line bar(430,40,420,105);
for(int r=10;r<=629;r+=20) bar(420,105,370,95);
{ bar(370,95,380,160);
setcolor(YELLOW); bar(380,160,430,150);
circle(r,y,10);
setfillstyle(1,RED+r); setfillstyle(j-12,j+BLACK+7);
floodfill(r,y,YELLOW); bar(450,40,510,50);//9
delay(200); bar(450,40,460,105);
} bar(460,105,510,95);
/////////////////////////Left side line bar(510,95,500,40);
for(y=29;y<=469;y+=20) bar(510,105,500,160);
{ bar(500,160,450,150);
setcolor(GREEN); delay(300);
circle(10,y,10); }
setfillstyle(1,y+RED); ////////////////////////////////////////////
floodfill(10,y,GREEN); for(int s=1;s<=360;s++)
delay(200); {
} setcolor(13);
///////////////////////Bottom Line ellipse(319,310,0,s,200,100);
for(r=29;r<=639;r+=20) ellipse(319,310,0,s,180,90);
{ ellipse(319,310,0,s,160,80);
setcolor(GREEN); ellipse(319,310,0,s,140,70);
circle(r,469,10); ellipse(319,310,0,s,120,60);
setfillstyle(1,RED + r -19 ); ellipse(319,310,0,s,210,110);
floodfill(r,468,GREEN); delay(20);
delay(200); }
} ///////////////////////////////////////////
////////////////////////////Right Side Line setcolor(13);
for(y=9;y<=459;y+=20) for(j=0;j<12;j++)
{ {
setcolor(GREEN); setfillstyle(1,j-1);
circle(629,y,10); floodfill(320,415,13);
setfillstyle(1,y+RED);
floodfill(629,y,GREEN); setfillstyle(1,1+j);
delay(200); floodfill(340,405,13);
}
setfillstyle(1,j+6);
///////////////////////////////////// floodfill(360,395,13);
for(int j=1;j<12;j++)
{ setfillstyle(1,RED+j);
setfillstyle(j+1,j+GREEN); floodfill(380,380,13);
bar(40,40,50,160);//Zero
bar(40,40,100,50); setfillstyle(1,j-2);
bar(100,40,110,160); floodfill(400,360,13);
bar(40,160,110,150);
setfillstyle(j,j+RED+0); setfillstyle(1,j);
bar(130,40,140,160);//SIX floodfill(319,310,13);
bar(130,40,190,50); delay(1000);
bar(130,160,190,150); }
bar(190,150,180,100);
bar(180,100,130,110); //////////////////////////////////////

setfillstyle(j-5,j+YELLOW+1); getch();
bar(210,40,270,50);//I }

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 3


Computer Graphics Practical
By Mukhtiar Ahmed Kori

Program 2 pictures when program Run

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 4


Computer Graphics Practical
By Mukhtiar Ahmed Kori

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

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 1


Computer Graphics Practical
By Mukhtiar Ahmed Kori

Program 6.0

OUTPUT:

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 2


Computer Graphics Practical
By Mukhtiar Ahmed Kori

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

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 1


Computer Graphics Practical
By Mukhtiar Ahmed Kori

Program 7.0
/* 29_PR_7.cpp*/ setcolor(LIGHTMAGENTA);
/* generaqtes Bar Chart graph */ outtextxy(270,440,"THU");
#include<stdio.h> outtextxy(280,90,"7300");
#include<conio.h> setcolor(BROWN);
#include<graphics.h> outtextxy(350,440,"FRI");
#define N 6 /* number of values to graph */ outtextxy(360,180,"5200");
#define BWIDTH 30 /* Width of each bar */ setcolor(LIGHTGRAY);
#define SEP 52 /* Separation between bars */ outtextxy(430,440,"SAT");
#define DI (BWIDTH+SEP) /* Distance from bar to bar outtextxy(440,120,"6500");
*/ setcolor(RED);
#define SHFT 15 /* Between border and first outtextxy(480,440,"=>>>WEEKDAYS");
bar */ setcolor(RED);
#define WIDTH ((N+1) * DI) /* Width of CHART*/ setfillstyle(1,LIGHTGREEN);
#define LEFT 5 /* Left side of graph */ rectangle(10,10,575,70);
#define DEPTH 9 /* Depth of the bar */ floodfill(20,20,RED);
#define TOPFLAG 3 /* Put 3d top on bar */ setcolor(BLACK);
#define BOT 430 /* Bottome of Graph */ settextstyle(GOTHIC_FONT,HORIZ_DIR,4
#define TOP 5 /* Top of the graph */ );
#define PPD (float) (BOT-TOP)/100 /* Pixel per data outtextxy(180,15,"3-D");
unit*/ settextstyle(TRIPLEX_FONT,HORIZ_DIR,
void main(void) 4);
{ outtextxy(260,15,"BAR");
int gd=DETECT,gm=CGAC1,error,j;/*Data to display */ outtextxy(340,15,"CHART");
int data[N]={41,47,76,73,52,65}; setcolor(15);
initgraph(&gd,&gm,"c:\\tc\\bgi"); rectangle(480,80,565,420);
rectangle(LEFT,TOP,LEFT+WIDTH,BOT); setfillstyle(1,7);
for(j=0;j<N;j++) /*Draw Bars*/ floodfill(500,100,15);
{ setfillstyle(j+1,1+j%3); setcolor(16);
bar3d(LEFT+SHFT+j*DI,BOT- settextstyle(SANS_SERIF_FONT,VERT_DI
data[j]*PPD,LEFT+SHFT+j*DI+BWIDTH,BOT,DEPTH,TOPFLAG R,3);
); outtextxy(480,300,"06IT29");
} outtextxy(480,220,"QUEST");
outtextxy(480,80,"NAWABSHAH");
setcolor(LIGHTBLUE); setcolor(LIGHTRED);
outtextxy(20,440,"MON"); settextstyle(SANS_SERIF_FONT,VERT_DI
outtextxy(30,220,"4100"); R,6);
setcolor(LIGHTGREEN); outtextxy(500,130,"CG-PR#7");
outtextxy(100,440,"TUE");
outtextxy(110,200,"4700"); getch();
setcolor(LIGHTCYAN); closegraph();
outtextxy(190,440,"WED"); }
outtextxy(200,80,"7600");

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 2


Computer Graphics Practical
By Mukhtiar Ahmed Kori

OUTPUT:

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


make CHARTS.

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 3


Computer Graphics Practical
By Mukhtiar Ahmed Kori

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:

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 1


Computer Graphics Practical
By Mukhtiar Ahmed Kori
Value Constant Comment
0 COPY_PUT Replace the old image with new
1 XOR_PUT Old and new Images
2 OR_PUT OR old and new images
3 AND_PUT AND old and new images
4 NOT_PUT 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.

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 2


Computer Graphics Practical
By Mukhtiar Ahmed Kori

Program # 8.1
/*i m
agecp
. p*/
/* bounci ng bal l created frombi t i mage */ setfi l l styl eS( OLIDF_ ILLC
, G
AC0_LIGHTGREEN);
#i ncl ude<graphi csh
. > ci rcl exyR
( , , AD IUS);
#i ncl ude<coni oh
. > fl oodfi l l (xyC
, , GAC0_LIGHTGREEN);
#i ncl ude<stdi oh
. >
#i ncl ude<m
al l och
. > si ze = i magesi zex
( -RADIUSy , -RADIUSx
, +RADIUSy
, +R ADIUS);
#i ncl ude<dosh
. > bal l buff = (voi d *)mal l oc(si ze); /* get m emory
#defi ne LEFT 0 for i mage */
#defi ne TO
P0 /*
#defi ne R
IGHT 639 pl ace i mage i nto memory */
#defi ne BO
TTOM479 geti magex( -RADIUSy, -
#defi ne R
ADIUS8 RADIUSx, +R ADIUSy, +RADIUSb
, al l buff );
#defi ne C
GAC0_LIGHTGREEN1
dx=2; /* speed of bal l */
voi d mai nvo
( i d) dy=1;
{
cl rscr();
whi l e(!kbhi t ())
i nt gd=DETECTg , m; {
i nt xyd
, , xd
, yo, l dxo
, l dy; /* bal l coordi nates */ puti magex
( -R ADIUSy
, -R
ADIUSb
, al l buff ,C
OPYP
_ UT);
voi d *bal l buff ; /* poi nter to ol dx=x; ol dy=y; /* rem eber where i t was
buffer */ */
unsi gned si ze; /* si ze of
buffer */ x+= dx; y+=dy; /* move i ts coordi nates
*/
i ni tgraph(&gd,&gmc
," :\\tc\\bgi "); /* refl ect i t at edges
*/
rectangl eL
( EFTT
, O
PR
, IGHTB
, O
TTOM); i f (x<=LEFT+RADIUS+2 || x>=RIGHT-RADIUS-2)
x=y=RADIUS+10; dx=-dx;
setcol or(CGAC0_LIGHTGREEN); i f (y<=TOP+RADIUS+1 || y>=BOTTOM-RADIUS-1)
dy=-dy;

puti mageo
( l dx-R
ADIUSo
, l dy-R
ADIUSb
, al l buff ,XO
RP
_ U
T);
del ay(10);
}
cl osegraph(); }

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 3


Computer Graphics Practical
By Mukhtiar Ahmed Kori

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 4


Computer Graphics Practical
By Mukhtiar Ahmed Kori

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.

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 1


Computer Graphics Practical
By Mukhtiar Ahmed Kori
Activity # 1
#include<stdio.h> rectangle(10,50,20,60);
#include<graphics.h> outtextxy(25,53,"30% Educated");
#include<conio.h> setfillstyle(5,BROWN);
void main(void) floodfill(15,55,BROWN);
{ clrscr(); setcolor(LIGHTRED);
int gd=DETECT,gm; rectangle(10,70,20,80);
initgraph(&gd,&gm,"c:\\tc\\bgi"); outtextxy(25,73,"30% Male");
int x=getmaxx()/2+150,y=getmaxy()/2,radius=150; setfillstyle(2,LIGHTRED);
/////////////INFO////////////////// floodfill(15,75,LIGHTRED);
setcolor(YELLOW); ///////////////////////
rectangle(10,10,20,20); setcolor(15);
outtextxy(25,13,"20% Children"); setfillstyle(2,LIGHTRED);
setfillstyle(3,YELLOW); pieslice(x,y,0,108,radius);
floodfill(15,15,YELLOW); setfillstyle(3,YELLOW);
setcolor(LIGHTGREEN); pieslice(x,y,108,180,radius);
rectangle(10,30,20,40); setfillstyle(4,LIGHTGREEN);
outtextxy(25,33,"20% Female"); pieslice(x,y,180,252,radius);
setfillstyle(4,LIGHTGREEN); setfillstyle(5,BROWN);
floodfill(15,35,LIGHTGREEN); pieslice(x,y,252,360,radius);
setcolor(BROWN); getch(); }

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

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 2


Computer Graphics Practical
By Mukhtiar Ahmed Kori

Activity # 2
#include<stdio.h> int lx=getmaxx(),ly=getmaxy()/2;
#include<graphics.h> line(0,ly,lx,ly);
#include<conio.h> for(x=0;x<lx;x++)
#include<math.h> { angle=((double)x/lx)*(2*3.142);
#include<dos.h> sinofA=sin(angle);
void main(void) y=ly-ly*sinofA;
{ clrscr(); delay(10);
int gd=DETECT,gm; putpixel(x,y,x);
double angle,sinofA; setcolor(x);
int x,y; outtextxy(x+20,y,"06IT29"); }
initgraph(&gd,&gm,"c:\\tc\\bgi"); getch(); }

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

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 3


Computer Graphics Practical
By Mukhtiar Ahmed Kori

An extra graphical example by using activity #2 technique, just


replacing putpixel with out text and I have made this!

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 4


Computer Graphics Practical
By Mukhtiar Ahmed Kori

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> moveto(x,y); outtextxy(x+64,y+5,"Syed Salman
#include<stdio.h> square(SIDE); Mehdi");
#include<conio.h> setcolor(15); setcolor(BLACK);
#define MAX 640 setfillstyle(1,LIGHTGREEN); settextstyle(2,0,3);
#define GRID 80 floodfill(x+10,y+5,15); outtextxy(x+3,y+62,"QUEST
#define SIDE 72 setcolor(BROWN); NAWABSHAH");
void square(int side); settextstyle(2,1,2); setcolor(BLUE+RED);
void main(void) outtextxy(x+4,y+22,"06IT29"); settextstyle(2,0,6);
{int gd=DETECT,gm; setcolor(LIGHTRED); outtextxy(x+15,y+20,"PR-10");}
int x,y; settextstyle(2,0,3); getche();}
initgraph(&gd,&gm,"c:\\tc\\bgi"); outtextxy(x+2,y+2,"Computer void square(int side)
for(y=0; y<MAX; y+=GRID) Graphics"); {linerel(side,0);
for(x=0;x<MAX;x+=GRID) setcolor(LIGHTBLUE); linerel(0,side);
{setcolor(15); settextstyle(2,1,2); linerel(-side,0);
linerel(0,-side);}

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 1


Computer Graphics Practical
By Mukhtiar Ahmed Kori

Fig 10.1: Drawing boxes like checkers on the screen.

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

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 2


Computer Graphics Practical
By Mukhtiar Ahmed Kori

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> if(j==0) xRad=1;
#include<alloc.h> cleardevice();
#include<conio.h> ellipse(XC,YC,0,360,xRad,RAD);
#include<dos.h> buff[j]=(void *)malloc(size);
#define CGAC0_LIGHTGREEN 1 getimage(XC-RAD,YC-
#define XC 160 RAD,XC+RAD,YC+RAD,buff[j]); }
#define YC 100 while(!kbhit())
#define RAD 25 { for(j=0; j<N; j++)
#define N 8 { putimage(XC-RAD,YC-
#define DELAY 20 RAD,buff[j],COPY_PUT);
void main(void) delay(DELAY);
{ int xRad; putimage(XC-RAD,YC-
void *buff[N]; RAD,buff[j],XOR_PUT);}
unsigned size; for(j=N-1; j>0; j--)
int j,mode; { putimage(XC-RAD,YC-
int driver=DETECT; RAD,buff[j],COPY_PUT);
initgraph(&driver,&mode,"c:\\tc\\bgi"); delay(DELAY);
size=imagesize(XC-RAD,YC- putimage(XC-RAD,YC-
RAD,XC+RAD,YC+RAD); RAD,buff[j],XOR_PUT);}}
setcolor(CGAC0_LIGHTGREEN); closegraph();}
for(j=0; j<N; j++)
{ xRad=j*RAD/(N-1);

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

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 1


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

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

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 2


Computer Graphics Practical
By Mukhtiar Ahmed Kori

Activity#3 : Rotation of a Circular shape on x-axis :

#include<stdio.h> outtextxy(xc-36,yc,"-<06IT29>-");}
#include<conio.h> void main()
#include<graphics.h> { int d=0,m=0,x;
#include<math.h> int y1=240;
#define PI 3.1415 initgraph(&d,&m,"c:\\tc\\bgi");
#include<dos.h> while(!kbhit())
void rotate_wheel(int xc,int yc,int t) { for(x=0;x<640;x++)
{ int x,y; { rotate_wheel(x,y1,x%60);
for(t=t;t<180;t=t+60) sound(6*x);
{ setcolor(RED); delay(6);
x=50*cos(t*PI/180); nosound();
y=50*sin(t*PI/180); cleardevice();
line(xc+x,yc+y,xc-x,yc-y); rotate_wheel(x,240,x%60);}
//bar(xc+x,yc+y,xc-x,yc-y);} if(x>=639)
setcolor(LIGHTBLUE); { x=0;} }
circle(xc,yc,50); getch();
circle(xc,yc,53); closegraph();}
setcolor(YELLOW);

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

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 3


Computer Graphics Practical
By Mukhtiar Ahmed Kori

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*/ setusercharsize(multx=2,divx=1,multy=1,divy=1);


#include<graphics.h> outtext("Wider");
#include<conio.h> moveto(0,120);
#include<stdio.h> setusercharsize(multx=1,divx=1,multy=3,divy=2);
void main(void) outtext("Taller");
{int gd=DETECT,gm,j; moveto(0,180);
int multx,divx,multy,divy; setusercharsize(multx=1,divx=2,multy=1,divy=2);
initgraph(&gd,&gm,"c:\\tc\\bgi"); outtext("Half as Wide and Tall");
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,USER_CHAR_SIZE); getch();}
outtext("Normal");
moveto(0,60);

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 */ outtext("Triplex");
#include<stdio.h> moveto(0,60);
#include<conio.h> settextstyle(SMALL_FONT,HORIZ_DIR,FONT_SIZE);
#include<graphics.h> outtext("small ");
#define FONT_SIZE 4 moveto(60,60);
void main(void) settextstyle(SANS_SERIF_FONT,HORIZ_DIR,FONT_SIZE);
{clrscr(); outtext("Sanserif");
int gd=DETECT,gm; moveto(0,120);
initgraph(&gd,&gm,"c:\\tc\\bgi"); settextstyle(DEFAULT_FONT,HORIZ_DIR,FONT_SIZE);
settextstyle(GOTHIC_FONT,HORIZ_DIR,FONT_SIZE); outtext("Default");
outtext("Gothic "); moveto(250,0);
settextstyle(TRIPLEX_FONT,HORIZ_DIR,FONT_SIZE); settextstyle(TRIPLEX_FONT,VERT_DIR,FONT_SIZE);
outtext("Triplex");
getch();}

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 1


Computer Graphics Practical
By Mukhtiar Ahmed Kori

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 */ moveto(CL,LEAD);


#include<graphics.h> settextjustify(LEFT_TEXT,TOP_TEXT);
#include<conio.h> outtext("Left-Top");
#include<stdio.h> moveto(CL,LEAD*2);
#define CL 150 settextjustify(RIGHT_TEXT,TOP_TEXT);
#define LEAD 40 outtext("Right-Top");
#define FONT_SIZE 3 moveto(CL,LEAD*3);
void main(void) settextjustify(CENTER_TEXT,TOP_TEXT);
{int gd=DETECT,gm,j; outtext("Center-Top");
initgraph(&gd,&gm,"c:\\tc\\bgi"); moveto(CL,LEAD*4);
settextstyle(TRIPLEX_FONT,HORIZ_DIR,FONT_SIZE); settextjustify(CENTER_TEXT,BOTTOM_TEXT);
line(CL,0,CL,200); outtext("Center-BOTTOM");
for(j=0;j<LEAD*5;j+=LEAD) getch();}
line(0,j,300,j);

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

Syed Salman Mehdi - 06IT29 QUEST Nawabshah 2


Computer Graphics Project
With respect of Sir. Mukhtiar Ahmed Kori

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.

06 IT 29/33/43/44/tb-03 1
Computer Graphics Project
With respect of Sir. Mukhtiar Ahmed Kori
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.

06 IT 29/33/43/44/tb-03 2
Computer Graphics Project
With respect of Sir. Mukhtiar Ahmed Kori
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.

06 IT 29/33/43/44/tb-03 3
Computer Graphics Project
With respect of Sir. Mukhtiar Ahmed Kori

The Program CODE

/* This project is made by Syed poly[13]=m+26; poly[15]=26;


Salman Mehdi (06IT29) */ poly[14]=i; poly[16]=i+2;
#include<graphics.h> poly[15]=m+26; poly[17]=25;
#include<stdlib.h> poly[16]=i+2; fillpoly(9,poly);
#include<stdio.h> poly[17]=m+25; setcolor(10);
#include<conio.h> setcolor(4); setfillstyle(1,10);
#include<dos.h> setfillstyle(1,4); fillellipse(x-
#include<string.h> fillpoly(9,poly); 50,z,15,20);
void score(int i) m=m+25; //--------
{ setcolor(10); a++;} sound(4000);
settextstyle(2,0,1); /*baloon*/ delay(15);
outtextxy(25+i+5,(getmaxy())- setcolor(12); nosound();
25,"*");} setfillstyle(1,BROWN); if(j<35)
void erase(int x) fillellipse(x-50,y- {setcolor(9);
{setcolor(9); 50,15,20); line(j,25,39,3);
settextstyle(2,0,1); b=10; line(j,25,39,47);
outtextxy(25+x+5,(getmaxy())- setcolor(10); j++; }
25,"*");} //getch(); //burst
void main() while(b>0) if(poly[8]>=x-
{/* select a driver and mode {setcolor(4); 70&&poly[8]<x&&z<=45&&z>=
that supports */ setfillstyle(1,4); 10)
/*multiple background colors*/ i=5; { setcolor(9);
int gdriver = EGA, gmode = poly[0]=i+2; setfillstyle(1,9);
EGAHI, errorcode; poly[1]=25; fillellipse(x-
int poly[2]=i; 50,z,20,20);
poly1[20],poly2[20],i,j,k,m=0,n= poly[3]=24; setcolor(10);
225,x,y,z,p; poly[4]=i+55; setfillstyle(1,10);
int poly[20],a=0,b,s=0; poly[5]=24; poly1[0]=x-70;
char ch,ans='y',msg[80]; poly[6]=i+50; poly1[1]=z+5;
/* initialize graphics and local poly[7]=20; poly1[2]=x-50;
variables */ poly[8]=i+60; poly1[3]=z-10;
initgraph(&gdriver, &gmode, poly[9]=25; poly1[4]=x-30;
"c:\\tc\\bgi"); poly[10]=i+50; poly1[5]=z+5;
/* read result of initialization poly[11]=30; poly1[6]=x-70;
*/ poly[12]=i+55; poly1[7]=z+5;
errorcode = graphresult(); poly[13]=26; poly2[0]=x-70;
if (errorcode != grOk) poly[14]=i; poly2[1]=z-5;
/* an error occurred */ poly[15]=26; poly2[2]=x-30;
{printf("Graphics error: %s\n", poly[16]=i+2; poly2[3]=z-5;
grapherrormsg(errorcode)); poly[17]=25; poly2[4]=x-50;
printf("Press any key to fillpoly(9,poly); poly2[5]=z+10;
halt:"); setcolor(14); poly2[6]=x-70;
getch(); line(5,25,39,3); poly2[7]=z-5;
exit(1); line(5,25,39,47); fillpoly(4,poly1);
/* terminate with an error code for(k=y- fillpoly(4,poly2);
*/} 50;k>0&&(!kbhit());k--) s=s+5;
///////////////////// {setcolor(10); delay(5);
setcolor(LIGHTRED); setfillstyle(1,10); score(s);
circle(319,239,200); fillellipse(x-50,k,15,20); setcolor(9);
setfillstyle(1,LIGHTGREEN); delay(15); setfillstyle(1,9);
floodfill(319,239,LIGHTRED); setcolor(9); fillpoly(4,poly1);
settextstyle(1,0,5); setfillstyle(1,9); fillpoly(4,poly2);
outtextxy(10,10,"Project of fillellipse(x-50,k,20,20);} fillpoly(9,poly);
Computer Graphics"); j=5; break;}
settextstyle(1,0,1); if(k==0&&b==1) setcolor(9);
outtextxy(10,70,"Re-Edit by : {setcolor(9); setfillstyle(1,9);
Syed Salman Mehdi (06IT29)"); line(5,25,39,3); fillpoly(9,poly);
outtextxy(10,100,"Subject line(5,25,39,47); fillellipse(x-
Teacher : Sir Mukhtiar Ahmed setcolor(14); 50,z,20,20);
Kori"); line(39,3,39,47); line(39,3,39,47);
getch(); line(50,24,50,26);} line(50,24,50,26);} }
cleardevice(); else if(k==0) setcolor(9);
//////////////////////////////// { setcolor(4); setfillstyle(1,9);
////// setfillstyle(1,4); fillellipse(x-

06 IT 29/33/43/44/tb-03 4
Computer Graphics Project
With respect of Sir. Mukhtiar Ahmed Kori
/* for centering text messages fillpoly(9,poly);} 50,z,20,20);
*/ else line(39,3,39,47);
settextjustify(CENTER_TEXT, {for(i=5,z=k;i<x||z>0;z-- line(50,24,50,26);
CENTER_TEXT); ,i=i+5) i=5;
x = getmaxx();// / 2; {//if(z==0) poly[0]=i+2;
y = getmaxy();// / 2; //break; poly[1]=n+25;
setbkcolor(9); if(j<35) poly[2]=i;
getch(); {setcolor(14); poly[3]=n+24;
/*score board*/ line(j,25,39,3); poly[4]=i+55;
setcolor(16); line(j,25,39,47);} poly[5]=n+24;
rectangle(5,y-55,105,y-5); if(j==35) poly[6]=i+50;
setcolor(10); { setcolor(14); poly[7]=n+20;
rectangle(8,y-52,102,y-8); line(39,3,39,47); poly[8]=i+60;
setcolor(14); line(50,24,50,26);} poly[9]=n+25;
settextstyle(1,0,1); setcolor(4); poly[10]=i+50;
outtextxy(55,y-45,"POINTS"); setfillstyle(1,4); poly[11]=n+30;
setcolor(12); //m=0; poly[12]=i+55;
/*name*/ poly[0]=i+2; poly[13]=n+26;
setcolor(LIGHTRED); poly[1]=25; poly[14]=i;
settextstyle(2,0,4); poly[2]=i; poly[15]=n+26;
outtextxy(x/2,y-10,"Re-Edited poly[3]=24; poly[16]=i+2;
By: Syed Salman Mehdi poly[4]=i+55; poly[17]=n+25;
(06IT29)"); poly[5]=24; delay(10);
setcolor(12); poly[6]=i+50; fillpoly(9,poly);
while(ans=='y') poly[7]=20; n=n-25;
{/*bow*/ poly[8]=i+60; b--;
j=5; poly[9]=25; fflush(stdin);
i=5; poly[10]=i+50; getch();
setcolor(14); poly[11]=30; }//end of 1 game
arc(5,25,315,45,45); poly[12]=i+55; setcolor(14);
line(5,25,39,3); poly[13]=26; line(39,3,39,47);
line(5,25,39,47); poly[14]=i; line(50,24,50,26);
setcolor(9); setcolor(14);
line(39,3,39,47); settextstyle(2,0,4);
line(50,24,50,26); outtextxy(x/2,y/2,"Play
a=0; m=0; n=225; s=0; Again y/n");
for(p=5;p<65;p=p+5) ans=getch();
{erase(p);} setcolor(9);
while(a<10) settextstyle(2,0,4);
{ poly[0]=i+2; poly[1]=m+25; outtextxy(x/2,y/2,"Play
poly[2]=i; poly[3]=m+24; Again y/n"); }
poly[4]=i+55; poly[5]=m+24; //clean up
poly[6]=i+50; poly[7]=m+20; getch();
poly[8]=i+60; poly[9]=m+25; restorecrtmode();
poly[10]=i+50; poly[11]=m+30; closegraph();}
poly[12]=i+55;

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

Anda mungkin juga menyukai