C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori
Practical – 01
Object: Perform a LINE statement activity. 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: statement: line(x1, y1, x2, y2);
The syntax shows four values x1, y1, x2, y2. 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
#include #include void main (void) { clrscr (); int gd,gm; gd=DETECT; initgrap initgraph h (& gd, &gm,"c: &gm,"c:\\ \ tc\ \ bgi"); / / initiali initializing zing graphi graphics cs line (10, 10, 600, 10); / / simple line }/ end of program. program.
Output: Figure 1.1: How to draw Line in C Graphics mode.
Syed Salman Mehdi - 06IT29
QUEST Nawabshah
1
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori
Create a triangle line (20, 80, 60, 30); / / triangle line (6 0, 30, 100, 80); line (20, 80, 100, 80); / / complete triangle triangle
Create a Triangle in a Square. line line line line
(20, 90, 100, 90); 90); / / square w ith triangle triangle (20, 90, 20,150); 20,150); / / square ( 20,150,100,150); 20,150,100,150); (100, 150, 100, 90); / / square complete
line (60, 90, 20,150); 20,150); / / triangle line (60, 90,100, 90,100,150); 150); / / complete triangle 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 line line line
(20,160, (20,160,200, 200,160); 160); / / square ( 20, 160, 20,300); 20,300); ( 20,300,200,300); 20,300,200,300); (200,160,200, (200,160,200,300); 300); / / square complete
line line line line line line
(110, 170, 45,290); 45,290); / / triangle (1 10,170,175,290); 10,170,175,290); ( 45,290,175,290); 45,290,175,290); (1 08,290,185,200); 08,290,185,200); (1 85, 200, 35,200); 35,200); (35,200,108, (35,200,108,290); 290); / / triangle complete. complete.
Syed Salman Mehdi - 06IT29
QUEST Nawabshah
2
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l 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); 45,440); / / only star line (1 10,320,175,440); 10,320,175,440); line ( 45,440,185,350); 45,440,185,350); line (1 85, 350, 35,350); 35,350); line (35,350,175, (35,350,175,440); 440); / / star complete
Output:
Figure 1.4: How to draw a Star.
How to make a hut with line Statement How
U
#include #include #include void main (void)
Syed Salman Mehdi - 06IT29
QUEST Nawabshah
3
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori
{ int gd,gm; gd=DETECT; initgra initgraph(&gd, ph(&gd,&gm,"C &gm,"C::\ \ tc\ \ bgi"); line(140,80, line(140,80,480, 480,80); 80); / / top horizontal line(140,80, line(140,80,72, 72,180); 180); / / left point diagonal line(480 ,80,412,180); 80,412,180); / / right point diagonal -1 -1 line(480 ,80,548,180); 80,548,180); / / right point diagonal -2 -2 line(72,180, line(72,180,548, 548,180); 180); / / mid Hut horizontal line(72,180, line(72,180,72, 72,360); 360); / / mid Hut horizontal horizontal to vertical vertical dow n line(412,180,412,360);/ line(412,180,412,360);/ / second vertical line(548,180,548,360); line(548,180,548,360); / / third vertical line(72,360, line(72,360,548, 548,360); 360); / / Bottom Line horizontal line(450,250, line(450,250,510, 510,250); 250); / / door of the HUT line(450,250,450,360); line(510,250,510,360); line(462,120, line(462,120,495, 495,120); 120);// / w indow line(462,120,462,150); line(495,120,495,150); line(462,150, line(462,150,495, 495,150); 150); / / complete HUT getch(); } / / e n d of of P r o g ra ra m .
Output:
Figure 1.5: How to make a Hut through Line statement on C Graphics mode
Syed Salman Mehdi - 06IT29
QUEST Nawabshah
4
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l 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 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). r adius). 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 #include #include void main(void) { clrscr(); int gd=DETECT,gm; initgrap initgraph(&gd, h(&gd,&gm, &gm,"c: "c:\\ \ tc\ \ bgi"); / *how draw an arc*/ arc*/ Syed Salman Mehdi - 06IT29
QUEST Nawabshah
1
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori
arc(320,240,0,135,50); getch(); }/ / end of progr program am Output:
Figure 2.1 : How ARC draw on screen
PIESLICE shape drawing in graphical mode.
It is also a function 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 #include #include void main(void) { clrscr(); int gd=DETECT,gm; initgrap initgraph(&gd, h(&gd,&gm, &gm,"c: "c:\\ \ tc\ \ bgi"); / *how draw an pieslice*/ pieslice*/ pieslice(320,240,0,135,50); getch(); } Syed Salman Mehdi - 06IT29
QUEST Nawabshah
2
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l 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. circle. #include #include #include void main(void) { clrscr(); int gd=DETECT,gm; initgrap initgraph(&gd, h(&gd,&gm, &gm,"c: "c:\\ \ tc\ \ bgi"); / *how draw an circle circle*/ */ circle(320,240,50); getch(); } Output:
Syed Salman Mehdi - 06IT29
QUEST Nawabshah
3
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l 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 #include #include void main(void) { clrscr(); int gd=DETECT,gm; initgraph(&gd, initgraph(&gd,&gm,"C &gm,"C::\ \ tc\\ bgi"); arc(250,180,90,270, arc(250,180,90,270,105); 105);// / moon arc(220,180,90,270,100); ///////// ////////// ////////// ////////// ////////// ////// line(250,140,210,210); line(250,140,210,210); / / Star line(250,140,290,210); line(210,210,290,160); line(210,210,290,160); / / left line diagonal line(290,160,210,160); line(290,160,210,160); / / Horizontal line(210,160,290,210); 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
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori setcolor(YELLOW+RED+BLACK); pieslice(200,390,215,350,40); outtextxy(200,390,"PIESLICE"); ///////// ////////// ////////// ////////// ////////// ////////// /// setcolor(GREEN); outtextxy(340,320,"Sye outtextxy(340,320,"Syed d Salman Mehdi" ); setcolor(GREEN+BLUE); outtextxy(340,340,"06IT29 outtextxy(340,340,"06IT29 QUEST Nawabshah" ); setcolor(RED+WHITE+BLUE); outtextxy(340,360,"Practical outtextxy(340,360,"Practical #2,Computer #2,Computer Graphics" ); getch(); }
Output:
Figure 2.4 : How make shape combination on screen
Syed Salman Mehdi - 06IT29
QUEST Nawabshah
5
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l 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 #include #include #define _num 0 void main(void) { clrscr(); int gd=DETECT,gm; gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); setcolor(GREEN + WHITE); outtextxy(20,10,"Solid outtextxy(20,10,"Solid line"); setlinestyle(0,_num,1); setlinestyle(0,_num,1); // Simple / Solid line line(20,30,490,30); setcolor(GREEN + RED); outtextxy(20,50,"Dotted outtextxy(20,50,"Dotted line"); setlinestyle(1,_num,1); setlinestyle(1,_num,1); // Dotted line line(20,70,490,70); setcolor(RED + BLUE); outtextxy(20,90,"Center outtextxy(20,90,"Center line"); setlinestyle(2,_num,1); setlinestyle(2,_num,1); //Center line line(20,110,490,110); setcolor(BLUE + CYAN); outtextxy(20,130,"Dashed outtextxy(20,130,"Dashed line");
Syed Salman Mehdi - 06IT29
QUEST Nawabshah
1
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori setlinestyle(3,_num,1); setlinestyle(3,_num,1); //Dashed line line(20,150,490,150); setcolor(15); outtextxy(140,170,"\\\\\\\ outtextxy(140,170,"\\\\\\\\\\============ \\\============06IT29====== 06IT29============////// ======///////"); /"); getch(); }
Figure shows how lines can be shown on the screen T h e " D R A W P O L Y " S T A T EM EM E N T . 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 #include #include #define _num 0 void main(void) { clrscr(); int gd=DETECT,gm; gd=DETECT,gm;
Syed Salman Mehdi - 06IT29
QUEST Nawabshah
2
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori initgraph(&gd,&gm,"c:\\tc\\bgi"); setcolor(29 + 33); outtextxy(153,130,"RECTANG outtextxy(153,130,"RECTANGLE LE by DRAWPOLY Function"); setcolor(43 + 44 + 3); setlinestyle(0,_num,1); setlinestyle(0,_num,1); // Rectangle rectangle(180,140,360,260); setcolor(33 + 43); outtextxy(235,195,"Hexagonal") outtextxy(235,195,"Hexagonal");; //Hexagonal int points[]={200,200,240,160,300,1 points[]={200,200,240,160,300,160,340,200,30 60,340,200,300,240,240,240 0,240,240,240,200,200}; ,200,200}; setcolor(29 + 33 + 43); drawpoly(7,points); setcolor(43 + 44 + 3); outtextxy(10,145,"Parallelogram outtextxy(10,145,"Parallelogram"); "); //Parallelogram int points1[]={10,200,80,160,80,260 points1[]={10,200,80,160,80,260,10,300,10,20 ,10,300,10,200}; 0}; setcolor(43 + 33); drawpoly(5,points1); setcolor(3 + 44); outtextxy(340,80,"Pantagonal"); int points2[]={320,80,380,40,440,80, points2[]={320,80,380,40,440,80,410,120,350,1 410,120,350,120,320,80}; 20,320,80}; setcolor(33 + 3); drawpoly(6,points2);
setcolor(15 + 29 + 33 + 43 + 44); outtextxy(140,370,"\\\\\\\ outtextxy(140,370,"\\\\\\\\\\============ \\\============06IT29====== 06IT29============////// ======///////"); /"); getch(); }
Figure shows that how the program effects have been shown on the screen.
Syed Salman Mehdi - 06IT29
QUEST Nawabshah
3
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l 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 #include #include void main (void) { clrscr (); int g d=DETECT, d=DETECT, gm; initgraph (&gd, &gm,"c :\\tc\\bgi"); :\\tc\\bgi"); int x E=319,yE=23 E=319,yE=239; 9; /* Center of the Elli pse*/ int xRAD=150,yRAD; xRAD=150,yRAD; int stANGLE=0,endANGLE=3 stANGLE=0,endANGLE=360; 60;
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
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori }
Output
Figure 1.a: Shows horizontally greater then Vertical Radius.
1.b: 1.b: Shows vertically v ertically grea g reater ter then Horizontal Radius.
A graphical animation animation wit h this Program of Ellipse: Program # 2 #include #include #include #include void main(void) { int g d=DETECT, d=DETECT, gm; initgraph(&gd,&gm,"C:\\tc\\bgi"); int x E=350,yE=20 E=350,yE=200; 0; // Center of the ell ipse. int xRAD=150,yRAD; xRAD=150,yRAD; int STANGLE=0,ENDANGLE=360; for(yRAD=0;yRAD<100;yRAD+=10) {
Syed Salman Mehdi - 06IT29
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();
QUEST Nawabshah
2
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori setcolor(yRAD);
}
Figure 2: 2: Shows colorful color ful ellipse Activity # 2 Object: Working with drawpoly (), fillpoly (), setfillstyle (). Syntax
void far drawpoly(int numpoint s, int far *polypoints);
drawpoly draws dr aws the outline outlin e of a polygon polyg on void far fillpol y(int numpoints, int far *polypoints);
fillpoly draws and fills a polygon void far setfillsty le(int le(int pattern, int color);
Sets Se ts the th e 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
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l 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 #include #include
rectangle(LEFT,TOP,RIGHT,BOTTOM); drawpoly(4,RIGHT_para); drawpoly(5,TOP_para);
void main (void) { clrscr();
getch();
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,13 T_para[]={150,50,180,20,180,135,150,180,150 5,150,180,150,50}; ,50}; int TOP_para[]={50,47,150,47 _para[]={50,47,150,47,180,17,95,17,50 ,180,17,95,17,50,47}; ,47}; int rect[]={50,50,150,50,150,180 rect[]={50,50,150,50,150,180,50,180,50,50}; ,50,180,50,50};
setfillstyle(11,YELLOW); fillpoly(5,rect); setfillstyle(1,GREEN); fillpoly(5,RIGHT_para); setfillstyle(2,WHITE+RED); fillpoly(5,TOP_para); getch();
Figure 3: 3: Shows both con ditions ditio ns whether figure in outlin e or in colors
Syed Salman Mehdi - 06IT29
QUEST Nawabshah
4
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori
Practical – 05 Object: 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
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori
Setcolor Setcolor sets the current drawing color Setcolor (int color);
Program #include #include #include void main(void)
- 1
setfillstyle(4,5); floodfill(350,190,2); setfillstyle(5,4); floodfill(190,121,2);
{ 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,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();
setfillstyle(2,1); floodfill(210,195,2); setfillstyle(3,2); floodfill(280,110,2);
}
Figure of program – 1
Syed Salman Mehdi - 06IT29
QUEST Nawabshah
2
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori
Program – 2 #include #include #include #include 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); }
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);
///////////////////////////////////// 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
getch(); }
Syed Salman Mehdi - 06IT29
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); }
QUEST Nawabshah
3
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori
Program 2 pictures when program Run
Syed Salman Mehdi - 06IT29
QUEST Nawabshah
4
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori
Practical – 06
Object:
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
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori
Program
6.0
OUTPUT:
Syed Salman Mehdi - 06IT29
QUEST Nawabshah
2
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori
Practical – 07 Object:
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
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori
Program /* 29_PR_7.cpp*/ /* generaqtes Bar Chart graph */ #include #include #include #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
Syed Salman Mehdi - 06IT29
7.0 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_DI R,3); outtextxy(480,300,"06IT29"); outtextxy(480,220,"QUEST"); outtextxy(480,80,"NAWABSHAH"); setcolor(LIGHTRED); settextstyle(SANS_SERIF_FONT,VERT_DI R,6); outtextxy(500,130,"CG-PR#7"); getch(); closegraph(); }
QUEST Nawabshah
2
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l 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
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori
Practical – 08
Object:
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
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori Value 0 1 2 3 4
Constant COPY_PUT XOR_PUT OR_PUT AND_PUT NOT_PUT
Comment 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.
Syed Salman Mehdi - 06IT29
QUEST Nawabshah
2
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori
Program # 8.1 /*i m age.cpp*/
bouncin ci ng bal l creat ed f rombi t i m age */ #i nclu cl ude #i ncl ude #i ncl ude #i nclu cl ude #i nclu cl ude #def i ne LE FT 0 #def i ne TO P0 #defi efi ne RI GHT 639 #defi defi ne BOTTOM479 #def i neR ne RADI US8 #defi f i ne CGAC0_LIG I GHTG REEN1 /*
, G setf setf i l l styl st yl e(SOLI D_FI LLC AC0_LIG LIGHTGREEN); cir ci rcle cl e(x,y,RADI US); f l oodfi dfi l l (x,y,CGAC0_LIG LIGHTGREEN);
si ze = i m agesi ze(x-RADI US,y-RADI US,x+RADI US,y+RADI US); /* get m bal l buf f = (voi d *)m al l oc(si ze); em ory ry f or i m age */ /*
pl ace i m age i nt o m em ory */ get i m age(x-RADI US,y, +R , +R , RADI USx ADI USy ADI USbal l buff buff ); dx=2; dy=1;
voi d m ai n(voi d)
/*
speed of bal l
*/
{
cl rscr(); whi l e(!kbhi t ()) ; i nt gd=DETECT,gm i nt x,y,dx,dy,ol dx,ol dy; voi d *bal l buf f ; buf f er */ unsig si gned size si ze; buf f er */
{ /*
bal l coordi nat es */ /* poi nt er t o
put i m age(x-RADI US,y-RADI US,bal l buf f ,COPY_PUT); /* rem ol dx=x; ol dy=y; eber where i t was */
/*
si ze of x+= dx; y+=dy;
/*
m ove i t s coordi nat es
/*
ref l ect i t at edges
*/
i ni t grap raph(&gd,&gm,"c:\\tc\\bgi "); */ ); rect angl e(LEFT,TOP,RI GHT,BOTTOM x=y=RADI US+10; set col or(CGAC0_LIG I GHTG REEN);
i f (x<=LEFT+RADI US+2 || x>=RI GHT-RADI US-2) dx=-dx; >=B i f (y<=TOP+RADI US+1 || y>= OTTOM-RADI US-1) dy=-dy; , put i m age(ol dx-RADI US,ol dy-RADI USbal l buff buff ,XOR_PUT); del ay(10); }
cl osegraph();
Syed Salman Mehdi - 06IT29
}
QUEST Nawabshah
3
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori
Syed Salman Mehdi - 06IT29
QUEST Nawabshah
4
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori
Practical – 09
Object # 1:
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 mod ified via matherr and _matherrl. Syed Salman Mehdi - 06IT29
QUEST Nawabshah
1
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori
#include #include #include void main(void) { clrscr(); int gd=DETECT,gm; initgraph(&gd,&gm, initgraph(&gd,&gm,"c: "c:\\ \ tc\\ bgi"); int x=getmaxx()/ 2+150,y=getmaxy()/ 2+150,y=getmaxy()/ 2,radius=150; / / / / / / / / / / / / / I NF NF O / / / / / / / / / / / / / / / / / / 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: Show s how you can use PI ESLICE for making graphical reports in C language.
Syed Salman Mehdi - 06IT29
QUEST Nawabshah
2
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori
Figure 9.2: 9.2: show s how you can draw a sinusoidal wave w ith help of sine function and putpixel.
Syed Salman Mehdi - 06IT29
QUEST Nawabshah
3
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori
An extra graphic al example by using activity #2 technique, just replacing replacing putpixel w ith out text and I have made this!
Syed Salman Mehdi - 06IT29
QUEST Nawabshah
4
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori
Practical – 10
Object:
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 #include #include #define MAX 640 #define GRID 80 #define SIDE 72 void square(int side); void main(void) {int gd=DETECT,gm; gd=DETECT,gm; int x,y; initgraph(&gd,&gm,"c:\\tc\\bgi"); for(y=0; y
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);
Syed Salman Mehdi - 06IT29
outtextxy(x+64,y+5,"Syed 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);}
QUEST Nawabshah
1
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori
Fig 10.1: Draw ing boxes like check ers on the screen.
Fig10.2 Fig10.2 :Filling :Filling the boxes w ith flood fill and w riting text on them.
Syed Salman Mehdi - 06IT29
QUEST Nawabshah
2
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l 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 #include #include #include #define CGAC0_LIGHTGREEN CGAC0_LIGHTGRE EN 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,YCRAD,XC+RAD,YC+RAD); setcolor(CGAC0_LIGHTGREEN); for(j=0; j
if(j==0) xRad=1; cleardevice(); ellipse(XC,YC,0,360,xRad,RAD); buff[j]=(void *)malloc(size); getimage(XC-RAD,YCRAD,XC+RAD,YC+RAD,buff[j]); } while(!kbhit()) { for(j=0; j0; j--) { putimage(XC-RAD,YCRAD,buff[j],COPY_PUT); delay(DELAY); putimage(XC-RAD,YCRAD,buff[j],XOR_PUT);}} closegraph();}
Fig: 11.1 shows how coin display on screen and rotate.
Syed Salman Mehdi - 06IT29
QUEST Nawabshah
1
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori
Activity#2 :
Rotating a Circle on the wedge
/* Program to show Animation */ #include #include #include #include #include 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(x2x1) s1=1; else s1=0; if(y2y1) 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.
Syed Salman Mehdi - 06IT29
QUEST Nawabshah
2
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l By Mukhtiar Ahmed Kori
Activity#3 :
Rotation of a Circular shape on x-axis :
#include #include #include #include #define PI 3.1415 #include 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.
Syed Salman Mehdi - 06IT29
QUEST Nawabshah
3
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l 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*/ #include #include #include void main(void) {int gd=DETECT,gm,j; int multx,divx,multy,divy; multx,divx,multy,divy; initgraph(&gd,&gm,"c:\\tc\\bgi"); settextstyle(SANS_SERIF_FONT,HORI settextstyle(SANS_SERIF_FONT,HORIZ_DIR,USER_CHA Z_DIR,USER_CHAR_SIZE); R_SIZE); outtext("Normal"); moveto(0,60);
setusercharsize(multx=2,divx=1,multy setusercharsize(multx=2,d ivx=1,multy=1,divy=1) =1,divy=1);; outtext("Wider"); moveto(0,120); setusercharsize(multx=1,divx=1,multy setusercharsize(multx=1,d ivx=1,multy=3,divy=2) =3,divy=2);; outtext("Taller"); moveto(0,180); setusercharsize(multx=1,divx=2,multy setusercharsize(multx=1,d ivx=2,multy=1,divy=2) =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 #include #include #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);
Syed Salman Mehdi - 06IT29
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();}
QUEST Nawabshah
1
C o m p u t e r Gr G r a p h i c s P r ac ac t i c a l 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 */ #include #include #include #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
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.
Syed Salman Mehdi - 06IT29
QUEST Nawabshah
2
C o m p u t e r G r a p h i c s P r o je je c t With respect of Sir. Mukhtiar Ahmed Kori
HIT ARCHER (GAME) Tool: Turbo C++ compiler of Borland Language: C.
A b o u t th th e G a m e . The game is simply designed in the BORLAND C++
C o m p i l e r with using simple C l a ng n g u a g e f u n c t i o n s of “” 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 EGA A Driver & EG EGAHI AHI mode to create display in High an EG Quality. This is the representation of all the activities which we had practices and learn during CIRCLE, the class , from drawing a simple LIN E, CIRCLE, ELLPSE , LOOPS, POINT ERS , MOVI NG OBJ ECT , 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 s c o r e is showing below on left bottom side of the screen. After targeting all ten hits game is over and ask you that “ P l a y a g a i n y /n /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
C o m p u t e r G r a p h i c s P r o je je c t With respect of Sir. Mukhtiar Ahmed Kori
Starting Starting of the P roject.
The figure show s how EGG is moving from right-bottom to righttop & the arrow is ready to hit the EGG. EGG.
06 IT 29/33/43/44/tb-03
2
C o m p u t e r G r a p h i c s P r o je je c t With respect of Sir. Mukhtiar Ahmed Kori
The figure show s after hitting all ten hits, screen w ill look this and the points which you scored are shown below on left-bottom left-bottom position.
After completing a game session it will ask for “ play again” or w ant to terminate it here.
06 IT 29/33/43/44/tb-03
3
C o m p u t e r G r a p h i c s P r o je je c t With respect of Sir. Mukhtiar Ahmed Kori
The Progr am CO CODE DE /* This project is made by Syed Salman Mehdi (06IT29) */ #include #include #include #include #include #include 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,y50,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=y50;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);
06 IT 29/33/43/44/tb-03
poly[15]=26; poly[16]=i+2; poly[17]=25; fillpoly(9,poly); setcolor(10); setfillstyle(1,10); fillellipse(x50,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]>=x70&&poly[8]= 10) { setcolor(9); setfillstyle(1,9); fillellipse(x50,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(x50,z,20,20); line(39,3,39,47); line(50,24,50,26);} } setcolor(9); setfillstyle(1,9); fillellipse(x-
4
C o m p u t e r G r a p h i c s P r o je je c t With respect of Sir. Mukhtiar Ahmed Kori /* for centering text messages fillpoly(9,poly);} */ else settextjustify(CENTER_TEXT, {for(i=5,z=k;i0;z-CENTER_TEXT); ,i=i+5) x = getmaxx();// / 2; {//if(z==0) y = getmaxy();// / 2; //break; setbkcolor(9); if(j<35) getch(); {setcolor(14); /*score board*/ line(j,25,39,3); setcolor(16); line(j,25,39,47);} if(j==35) rectangle(5,y-55,105,y-5); setcolor(10); { setcolor(14); rectangle(8,y-52,102,y-8); line(39,3,39,47); setcolor(14); line(50,24,50,26);} setcolor(4); settextstyle(1,0,1); outtextxy(55,y-45,"POINTS"); setfillstyle(1,4); setcolor(12); //m=0; /*name*/ poly[0]=i+2; setcolor(LIGHTRED); poly[1]=25; settextstyle(2,0,4); poly[2]=i; outtextxy(x/2,y-10,"Re-Edited poly[3]=24; By: Syed Salman Mehdi poly[4]=i+55; (06IT29)"); poly[5]=24; setcolor(12); poly[6]=i+50; while(ans=='y') poly[7]=20; {/*bow*/ poly[8]=i+60; j=5; poly[9]=25; i=5; poly[10]=i+50; setcolor(14); poly[11]=30; arc(5,25,315,45,45); poly[12]=i+55; line(5,25,39,3); poly[13]=26; line(5,25,39,47); poly[14]=i; 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;
06 IT 29/33/43/44/tb-03
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();}
5