Professional Documents
Culture Documents
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.
QUEST Nawabshah
Create a triangle
line (20, 80, 60, 30); //triangle line (60, 30, 100, 80); line (20, 80, 100, 80); //complete triangle
line (60, 90, 20,150); //triangle line (60, 90,100,150); //complete triangle
Output:
QUEST Nawabshah
Output:
Figure 1.3: How to draw Cross Line Triangle which intersects each other.
Output:
QUEST Nawabshah
{ 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
QUEST Nawabshah
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.
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);
Output:
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.
QUEST Nawabshah
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); line(250,140,290,210); line(210,210,290,160); line(290,160,210,160); line(210,160,290,210); //Star //left line diagonal //Horizontal //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"); //////////////////////////////////////////////////////////
QUEST Nawabshah
Output:
QUEST Nawabshah
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.
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");
QUEST Nawabshah
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;
QUEST Nawabshah
Figure shows that how the program effects have been shown on the screen.
QUEST Nawabshah
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();
QUEST Nawabshah
Output
1.b: Shows vertically greater then Horizontal Radius. A graphical animation with this Program of Ellipse:
Program # 2 #include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> void main(void) { int gd=DETECT, gm; initgraph(&gd,&gm,"C:\\tc\\bgi"); int xE=350,yE=200; // Center of the ellipse. int xRAD=150,yRAD; int STANGLE=0,ENDANGLE=360; for(yRAD=0;yRAD<100;yRAD+=10) { ellipse(xE,yE,STANGLE,ENDANGLE,xRAD,yRAD); delay(2000); } //getch(); delay(2000); clrscr(); for(xRAD=0;xRAD<100;xRAD+=10) { setcolor(xRAD); ellipse(xE,yE,STANGLE,ENDANGLE,xRAD,yRAD); delay(2000); } getch();
QUEST Nawabshah
Figure 2: Shows colorful ellipse Activity # 2 Object: Working with drawpoly (), fillpoly (), setfillstyle (). Syntax
drawpoly draws the outline of a polygon fillpoly draws and fills a polygon 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. QUEST Nawabshah 3
To draw a closed figure with N vertices, you must pass N+1 coordinates to drawpoly, where the Nth coordinate == the 0th coordinate setfillstyle sets the current fill pattern and fill color. To set a user-defined fill pattern, do not give a pattern of 12 (USER_FILL) to setfillstyle; instead, call setfillpattern. The enumeration fill_patterns, defined in GRAPHICS.H, gives names for the predefined fill patterns, plus an indicator for a user-defined pattern. Return Value: None
If invalid input is passed to setfillstyle, graphresult returns -11 (grError), and the current fill pattern and fill color remain unchanged. A simple program to draw on screen with all these three statements.
Program :
#include<conio.h> #include<stdio.h> #include<graphics.h> void main (void) { clrscr(); int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); int LEFT=50,TOP=50,RIGHT=150,BOTTOM=180; int RIGHT_para[]={150,50,180,20,180,135,150,180,150,50}; int TOP_para[]={50,47,150,47,180,17,95,17,50,47}; int rect[]={50,50,150,50,150,180,50,180,50,50}; rectangle(LEFT,TOP,RIGHT,BOTTOM); drawpoly(4,RIGHT_para); drawpoly(5,TOP_para); getch(); setfillstyle(11,YELLOW); fillpoly(5,rect); setfillstyle(1,GREEN); fillpoly(5,RIGHT_para); setfillstyle(2,WHITE+RED); fillpoly(5,TOP_para); getch();
QUEST Nawabshah
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);
QUEST Nawabshah
Setcolor Setcolor sets the current drawing color Setcolor (int color);
Program
#include<graphics.h> #include<dos.h> #include<math.h> void main(void) { int gd=DETECT,gm; initgraph(&gd,&gm,"C:\\tc\\bgi"); int star[]={200,200,280,100,360,200,180,120,380 ,120,200,200}; setcolor(2); drawpoly(7,star); setfillstyle(1,3); floodfill(280,130,2);
- 1
setcolor(WHITE); outtextxy(330,125,"E"); outtextxy(220,125,"B"); outtextxy(275,110,"A"); outtextxy(240,160,"C"); outtextxy(310,160,"D"); for(int i=1;i<32;i++) { setcolor(i); outtextxy(263,135,"QUEST"); outtextxy(258,145,"06IT29"); delay(600); } getch(); }
Figure of program 1
QUEST Nawabshah
Program 2
#include<conio.h> #include<graphics.h> #include<stdio.h> #include<dos.h> void main(void) { int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); int y=10; /////////////////////////Top Line for(int r=10;r<=629;r+=20) { setcolor(YELLOW); circle(r,y,10); setfillstyle(1,RED+r); floodfill(r,y,YELLOW); delay(200); } /////////////////////////Left side line for(y=29;y<=469;y+=20) { setcolor(GREEN); circle(10,y,10); setfillstyle(1,y+RED); floodfill(10,y,GREEN); delay(200); } ///////////////////////Bottom Line for(r=29;r<=639;r+=20) { setcolor(GREEN); circle(r,469,10); setfillstyle(1,RED + r -19 ); floodfill(r,468,GREEN); delay(200); } ////////////////////////////Right Side Line for(y=9;y<=459;y+=20) { setcolor(GREEN); circle(629,y,10); setfillstyle(1,y+RED); floodfill(629,y,GREEN); delay(200); } ///////////////////////////////////// for(int j=1;j<12;j++) { setfillstyle(j+1,j+GREEN); bar(40,40,50,160);//Zero bar(40,40,100,50); bar(100,40,110,160); bar(40,160,110,150); setfillstyle(j,j+RED+0); bar(130,40,140,160);//SIX bar(130,40,190,50); bar(130,160,190,150); bar(190,150,180,100); bar(180,100,130,110); setfillstyle(j-5,j+YELLOW+1); bar(210,40,270,50);//I bar(235,40,245,160); bar(210,160,270,150); setfillstyle(j-6,j+WHITE+0); bar(290,40,350,50);//T bar(315,40,325,160); setfillstyle(j-8,j+BLUE+3); bar(370,40,430,50);//2 bar(430,40,420,105); bar(420,105,370,95); bar(370,95,380,160); bar(380,160,430,150); setfillstyle(j-12,j+BLACK+7); bar(450,40,510,50);//9 bar(450,40,460,105); bar(460,105,510,95); bar(510,95,500,40); bar(510,105,500,160); bar(500,160,450,150); delay(300); } //////////////////////////////////////////// for(int s=1;s<=360;s++) { setcolor(13); ellipse(319,310,0,s,200,100); ellipse(319,310,0,s,180,90); ellipse(319,310,0,s,160,80); ellipse(319,310,0,s,140,70); ellipse(319,310,0,s,120,60); ellipse(319,310,0,s,210,110); delay(20); } /////////////////////////////////////////// setcolor(13); for(j=0;j<12;j++) { setfillstyle(1,j-1); floodfill(320,415,13); setfillstyle(1,1+j); floodfill(340,405,13); setfillstyle(1,j+6); floodfill(360,395,13); setfillstyle(1,RED+j); floodfill(380,380,13); setfillstyle(1,j-2); floodfill(400,360,13); setfillstyle(1,j); floodfill(319,310,13); delay(1000); } ////////////////////////////////////// getch(); }
QUEST Nawabshah
QUEST Nawabshah
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:
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
QUEST Nawabshah
Program
6.0
OUTPUT:
QUEST Nawabshah
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:
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
QUEST Nawabshah
Program
/* 29_PR_7.cpp*/ /* generaqtes Bar Chart graph */ #include<stdio.h> #include<conio.h> #include<graphics.h> #define N 6 /* number of values to graph */ #define BWIDTH 30 /* Width of each bar */ #define SEP 52 /* Separation between bars */ #define DI (BWIDTH+SEP) /* Distance from bar to bar */ #define SHFT 15 /* Between border and first bar */ #define WIDTH ((N+1) * DI) /* Width of CHART*/ #define LEFT 5 /* Left side of graph */ #define DEPTH 9 /* Depth of the bar */ #define TOPFLAG 3 /* Put 3d top on bar */ #define BOT 430 /* Bottome of Graph */ #define TOP 5 /* Top of the graph */ #define PPD (float) (BOT-TOP)/100 /* Pixel per data unit*/ void main(void) { int gd=DETECT,gm=CGAC1,error,j;/*Data to display */ int data[N]={41,47,76,73,52,65}; initgraph(&gd,&gm,"c:\\tc\\bgi"); rectangle(LEFT,TOP,LEFT+WIDTH,BOT); for(j=0;j<N;j++) /*Draw Bars*/ { setfillstyle(j+1,1+j%3); bar3d(LEFT+SHFT+j*DI,BOTdata[j]*PPD,LEFT+SHFT+j*DI+BWIDTH,BOT,DEPTH,TOPFLAG ); } setcolor(LIGHTBLUE); outtextxy(20,440,"MON"); outtextxy(30,220,"4100"); setcolor(LIGHTGREEN); outtextxy(100,440,"TUE"); outtextxy(110,200,"4700"); setcolor(LIGHTCYAN); outtextxy(190,440,"WED"); outtextxy(200,80,"7600");
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
OUTPUT:
QUEST Nawabshah
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 :
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;
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:
QUEST Nawabshah
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 : NOTE: kbhit checks to see if a keystroke is currently available. Any available keystrokes can be retrieved with getch or getche. int kbhit(void);
QUEST Nawabshah
Program # 8.1
/*i m ecp */ a . p g /* #i n u e g p i csh cl d < ra h . > #i n u e co i oh cl d < n . > #i n u e std oh cl d < i . > #i n u e d sh cl d < o . > #d fi n L F 0 e e ET #d fi n T P 0 e e O
b u ci n b l l cre te fro i t i mg */ on g a a d mb ae
se l l styl eSL _ IL , GC L HGEN tfi ( OIDF LCA 0_ IGT RE ); ci rcl exyRD S ( , , A IU); fl o d l l (xyCA0_L HGEN o fi , , GC IGT RE); si ze = i mg si zex-RD Sy-RD Sx+RD Sy+RD S a e ( AIU, AIU, AIU, AIU); b l l b ff = (vo d *)ml l o (si ze a u i a c ); /* g t m o e e ry m fo i mg */ r ae p a i mg i n mmry */ l ce a e to e o g ti mg ( -RD Sye a ex AIU, RD Sx+RD Sy+RD Sb l l b ff ); AIU, AIU, AIU, a u d =2; x d =1; y
/* /*
#i n u e m l o . > cl d < a ch l
sp e o b l l ed f a
vo d mi nvo d i a ( i )
{
*/
wi l e kb i t ()) h (! h b l l co rd n te */ a o i a s /* p i n r to o te
/* {
si ze o f
x+= d ; y+=d ; x y
*/ */
/* /*
mve i ts co rd n te o o i a s re e i t a e g s fl ct t de
i n tg p (&g ,&g ," :\\tc\\b i "); i ra h d mc g re n l eL F , O, IGTBT O); cta g ( ETT PR H, OT M x=y=RD S AIU+10; se l o (CA0_L HGEN tco r GC IGT RE);
i f (x<=L F +RD S || x>=R H-RD S ET AIU+2 IGT AIU-2) d =-d ; x x i f (y<=T P RD S || y>=BT O-RD S O+ AIU+1 OT M AIU-1) d =-d ; y y p ti mg ( l d -RD So d -RD Sb l l b ff ,XRPT u a eo x AIU, l y AIU, a u O_ U); d l a (10); e y
}
cl o g p (); se ra h
QUEST Nawabshah
QUEST Nawabshah
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 long double sinl(long double x); double sin(double x); COMPLEX complex sin(complex z);
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
Activity # 1
#include<stdio.h> #include<graphics.h> #include<conio.h> void main(void) { clrscr(); int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); int x=getmaxx()/2+150,y=getmaxy()/2,radius=150; /////////////INFO////////////////// setcolor(YELLOW); rectangle(10,10,20,20); outtextxy(25,13,"20% Children"); setfillstyle(3,YELLOW); floodfill(15,15,YELLOW); setcolor(LIGHTGREEN); rectangle(10,30,20,40); outtextxy(25,33,"20% Female"); setfillstyle(4,LIGHTGREEN); floodfill(15,35,LIGHTGREEN); setcolor(BROWN); rectangle(10,50,20,60); outtextxy(25,53,"30% Educated"); setfillstyle(5,BROWN); floodfill(15,55,BROWN); setcolor(LIGHTRED); rectangle(10,70,20,80); outtextxy(25,73,"30% Male"); setfillstyle(2,LIGHTRED); floodfill(15,75,LIGHTRED); /////////////////////// setcolor(15); setfillstyle(2,LIGHTRED); pieslice(x,y,0,108,radius); setfillstyle(3,YELLOW); pieslice(x,y,108,180,radius); setfillstyle(4,LIGHTGREEN); pieslice(x,y,180,252,radius); setfillstyle(5,BROWN); pieslice(x,y,252,360,radius); getch(); }
Figure 9.1: Shows how you can use PIESLICE for making graphical reports in C language.
QUEST Nawabshah
Activity # 2
#include<stdio.h> #include<graphics.h> #include<conio.h> #include<math.h> #include<dos.h> void main(void) { clrscr(); int gd=DETECT,gm; double angle,sinofA; int x,y; initgraph(&gd,&gm,"c:\\tc\\bgi"); int lx=getmaxx(),ly=getmaxy()/2; line(0,ly,lx,ly); for(x=0;x<lx;x++) { angle=((double)x/lx)*(2*3.142); sinofA=sin(angle); y=ly-ly*sinofA; delay(10); putpixel(x,y,x); setcolor(x); outtextxy(x+20,y,"06IT29"); } getch(); }
Figure 9.2: shows how you can draw a sinusoidal wave with help of sine function and putpixel.
QUEST Nawabshah
An extra graphical example by using activity #2 technique, just replacing putpixel with out text and I have made this!
QUEST Nawabshah
Practical 10
Object: How to create a shape with user defined function and replicate it on whole screen? Tool: Turbo C++ compiler of Borland Language: C. New function which we are using in this practical are:
moveto
moveto moves the CP to (x, y)
Syntax:
void far moveto(int x, int y); moveto moves the current position (CP) to viewport position (x, y).
linerel
linerel draws a line from the CP to a point that is a relative distance (dx, dy) from the CP, then advances the CP by (dx, dy).
Syntax:
void far linerel(int dx, int dy); Program : 10.1
#include<graphics.h> #include<stdio.h> #include<conio.h> #define MAX 640 #define GRID 80 #define SIDE 72 void square(int side); void main(void) {int gd=DETECT,gm; int x,y; initgraph(&gd,&gm,"c:\\tc\\bgi"); for(y=0; y<MAX; y+=GRID) for(x=0;x<MAX;x+=GRID) {setcolor(15); moveto(x,y); square(SIDE); setcolor(15); setfillstyle(1,LIGHTGREEN); floodfill(x+10,y+5,15); setcolor(BROWN); settextstyle(2,1,2); outtextxy(x+4,y+22,"06IT29"); setcolor(LIGHTRED); settextstyle(2,0,3); outtextxy(x+2,y+2,"Computer Graphics"); setcolor(LIGHTBLUE); settextstyle(2,1,2); outtextxy(x+64,y+5,"Syed Salman Mehdi"); setcolor(BLACK); settextstyle(2,0,3); outtextxy(x+3,y+62,"QUEST NAWABSHAH"); setcolor(BLUE+RED); settextstyle(2,0,6); outtextxy(x+15,y+20,"PR-10");} getche();} void square(int side) {linerel(side,0); linerel(0,side); linerel(-side,0); linerel(0,-side);}
QUEST Nawabshah
Fig10.2 :Filling the boxes with flood fill and writing text on them.
QUEST Nawabshah
Practical 11
Object: Rotation in graphical pictures with some conditional codes. Tool: Turbo C++ compiler of Borland Language: C.
Activity#1 :
Rotating a COIN
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; j<N; j++) { putimage(XC-RAD,YCRAD,buff[j],COPY_PUT); delay(DELAY); putimage(XC-RAD,YCRAD,buff[j],XOR_PUT);} for(j=N-1; j>0; j--) { putimage(XC-RAD,YCRAD,buff[j],COPY_PUT); delay(DELAY); putimage(XC-RAD,YCRAD,buff[j],XOR_PUT);}} closegraph();}
#include<graphics.h> #include<alloc.h> #include<conio.h> #include<dos.h> #define CGAC0_LIGHTGREEN 1 #define XC 160 #define YC 100 #define RAD 25 #define N 8 #define DELAY 20 void main(void) { int xRad; void *buff[N]; unsigned size; int j,mode; int driver=DETECT; initgraph(&driver,&mode,"c:\\tc\\bgi"); size=imagesize(XC-RAD,YCRAD,XC+RAD,YC+RAD); setcolor(CGAC0_LIGHTGREEN); for(j=0; j<N; j++) { xRad=j*RAD/(N-1);
QUEST Nawabshah
Activity#2 :
/* Program to show Animation */ #include<graphics.h> #include<stdio.h> #include<conio.h> #include<dos.h> #include<math.h> int dx,dy,s1,s2,dp,swap=0; void bressenham(int x1,int x2,int y1,int y2) {dx=abs(x2-x1); dy=abs(y2-y1); if(x2<x1) s1=-1; else if(x2>x1) s1=1; else s1=0; if(y2<y1) s2=-1; else if(y2>y1) s2=1; else s2=0; dp=2*dy-dx; if(dy>dx) { int temp=dy; dy=dx; dx=temp; swap=1;}} void erase(int x,int y,int r,int x3,int y3,int x4,int y4 ,int x5,int y5,int x6,int y6) { setcolor(BLACK); circle(x,y,r); line(x3,y3,x4,y4); line(x5,y5,x6,y6);} void main() { clrscr(); int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); setcolor(WHITE); int x1=10,y1=50,h,b,x3,y3,x4,y4,x5,y5,x6,y6,x,y,r; printf("Enter the height of the wedge : "); scanf("%d",&h); printf("Enter the width of the edge : "); scanf("%d",&b); printf("Enter the radius of the circle : "); scanf("%d",&r); int x2=x1+b,y2=y1+h; cleardevice();
Fig: 11.2 shows the program first get the input user then show the rotation on the wedge.
QUEST Nawabshah
Activity#3 :
#include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> #define PI 3.1415 #include<dos.h> void rotate_wheel(int xc,int yc,int t) { int x,y; for(t=t;t<180;t=t+60) { setcolor(RED); x=50*cos(t*PI/180); y=50*sin(t*PI/180); line(xc+x,yc+y,xc-x,yc-y); //bar(xc+x,yc+y,xc-x,yc-y);} setcolor(LIGHTBLUE); circle(xc,yc,50); circle(xc,yc,53); setcolor(YELLOW);
Fig: 11.3 shows how a circle shown on the screen, which from left to right till any key press.
QUEST Nawabshah
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.
setusercharsize(multx=2,divx=1,multy=1,divy=1); outtext("Wider"); moveto(0,120); setusercharsize(multx=1,divx=1,multy=3,divy=2); outtext("Taller"); moveto(0,180); setusercharsize(multx=1,divx=2,multy=1,divy=2); outtext("Half as Wide and Tall"); getch();}
/*Different Varieties of TEXT*/ #include<graphics.h> #include<conio.h> #include<stdio.h> void main(void) {int gd=DETECT,gm,j; int multx,divx,multy,divy; initgraph(&gd,&gm,"c:\\tc\\bgi"); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,USER_CHAR_SIZE); outtext("Normal"); moveto(0,60);
Fig 12.1 , shows how you can draw text in different varieties .
Activity#2 : Draw text in different font style & Directions.
/*How to write text with different style and directions in graphics mode */ #include<stdio.h> #include<conio.h> #include<graphics.h> #define FONT_SIZE 4 void main(void) {clrscr(); int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); settextstyle(GOTHIC_FONT,HORIZ_DIR,FONT_SIZE); outtext("Gothic "); settextstyle(TRIPLEX_FONT,HORIZ_DIR,FONT_SIZE); outtext("Triplex"); moveto(0,60); settextstyle(SMALL_FONT,HORIZ_DIR,FONT_SIZE); outtext("small "); moveto(60,60); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,FONT_SIZE); outtext("Sanserif"); moveto(0,120); settextstyle(DEFAULT_FONT,HORIZ_DIR,FONT_SIZE); outtext("Default"); moveto(250,0); settextstyle(TRIPLEX_FONT,VERT_DIR,FONT_SIZE); outtext("Triplex"); getch();}
QUEST Nawabshah
Fig: 12.2 shows how you can draw text in vertical and horizontal direction.
Activity#3 : Justify Text in LEFT,RIGHT or CENTER manner.
/*How to justify text */ #include<graphics.h> #include<conio.h> #include<stdio.h> #define CL 150 #define LEAD 40 #define FONT_SIZE 3 void main(void) {int gd=DETECT,gm,j; initgraph(&gd,&gm,"c:\\tc\\bgi"); settextstyle(TRIPLEX_FONT,HORIZ_DIR,FONT_SIZE); line(CL,0,CL,200); for(j=0;j<LEAD*5;j+=LEAD) line(0,j,300,j); moveto(CL,LEAD); settextjustify(LEFT_TEXT,TOP_TEXT); outtext("Left-Top"); moveto(CL,LEAD*2); settextjustify(RIGHT_TEXT,TOP_TEXT); outtext("Right-Top"); moveto(CL,LEAD*3); settextjustify(CENTER_TEXT,TOP_TEXT); outtext("Center-Top"); moveto(CL,LEAD*4); settextjustify(CENTER_TEXT,BOTTOM_TEXT); outtext("Center-BOTTOM"); getch();}
Fig: 12.3 shows how you can justify text in different position of the screen.
Syed Salman Mehdi - 06IT29
QUEST Nawabshah
06 IT 29/33/43/44/tb-03
The figure shows how EGG is moving from right-bottom to righttop & the arrow is ready to hit the EGG.
06 IT 29/33/43/44/tb-03
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
06 IT 29/33/43/44/tb-03
06 IT 29/33/43/44/tb-03