practical Example-file (1)
practical Example-file (1)
1. initgraph ():-
It initialize the graphics system by loading the passed graphics driver then changing the system
into graphics mode.
2. getmaxx:-
getmaxx function returns the maximum X coordinate for current graphics mode and driver.
Declaration: int getmaxx();
3. getmaxy :-
getmaxy function returns the maximum Y coordinate for current graphics mode and driver.
Declaration: int getmaxy();
4. closegraph:-
closegraph function closes the graphics mode, deallocates all memory allocated by graphics
system and restores the screen to the mode it was in before you called initgraph.
Declaration: void closegraph();
//C program code of closegraph:
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
main()
{
int gd = DETECT, gm;
getch();
1
closegraph();
return 0;
}
5. outtext :- outtext function displays text at current position.
Declaration: void outtext(char *string);
//C programming code for outtext
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
getch();
closegraph();
return 0;
}
6. Point:
putpixel function plots a pixel at location (x, y) of specified color.
Declaration: void putpixel(int x, int y, int color);
2
For example,if we want to draw a GREEN color pixel at (35, 45) then we will write putpixel(35,
35, GREEN); in our c program, putpixel function can be used to draw circles, lines and ellipses
using various algorithms.
//C programming code for putpixel:-
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
getch();
closegraph();
return 0;
}
//Output of this program will be a RED pixel on screen at (25, 25) . Try to spot that pixel with
your eyes at left top portion of your computer screen.
7. line :-
line function is used to draw a line from a point(x1,y1) to point(x2,y2) i.e. (x1,y1) and (x2,y2) are
end points of the line.
//Declaration: void line(int x1, int y1, int x2, int y2);
//C programming code for line
#include <graphics.h>
#include <conio.h>
3
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
line(100, 100, 200, 200);
getch();
closegraph();
return 0;
}
//Above program draws a line from (100, 100) to (200, 200).
8. Arc:-
"arc" function is used to draw an arc with center (x, y) and stangle specifies starting angle,
endangle specifies the end angle and last parameter specifies the radius of the arc. arc function
can also be used to draw a circle but for that starting angle and end angle should be 0 and 360
respectively.
//Declaration: void arc(int x, int y, int stangle, int endangle, int radius);
// C programming source code for arc:
#include <graphics.h>
#include <conio.h>
int main()
{
int gd = DETECT, gm;
getch();
4
closegraph();
return 0;
// In the program (100, 100) are coordinates of center of arc, 0 is the starting angle, 135 is the
end angle and radius of the arc is 50.
9. Circle:-
Circle function is used to draw a circle with center (x,y) and third parameter specifies the radius
of the circle.
//Declaration: void circle(int x, int y, int radius);
//C program for circle
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
getch();
closegraph();
return 0;
}
//In the above program (100, 100) are coordinates of center of the circle and 50 is the radius of
circle.
10. Ellipse:-
Declarations of ellipse function :-
5
Ellipse is used to draw an ellipse (x,y) are coordinates of center of the ellipse, stangle is the
starting angle, end angle is the ending angle, and fifth and sixth parameters specifies the X
and Y radius of the ellipse. To draw a complete ellipse strangles and end angle should be 0
and 360 respectively.
void ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius);
//C programming code for ellipse:
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
getch();
closegraph();
return 0;
}
11. rectangle:-
Declaration: void rectangle(int left, int top, int right, int bottom);
rectangle function is used to draw a rectangle. Coordinates of left top and right bottom
corner are required to draw the rectangle. left specifies the X-coordinate of top left corner,
top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right
bottom corner, bottom specifies the Y-coordinate of right bottom corner.
//C programming code for rectangle
#include<graphics.h>
#include<conio.h>
main()
6
{
int gd = DETECT, gm;
rectangle(100,100,200,200);
getch();
closegraph();
return 0;
}
12. Cleardevice:-
Declaration: void cleardevice();
cleardevice function clears the screen in graphics mode and sets the current position to
(0,0). Clearing the screen consists of filling the screen with current background color.
C program for cleardevice:
#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
7
getch();
closegraph();
return 0;
}
13. setcolor:-
Declaration: void setcolor(int color);
In Turbo Graphics each color is assigned a number. Total 16 colors are available. Strictly
speaking number of available colors depends
on current graphics mode and driver.For Example :- BLACK is assigned 0, RED is assigned
4 etc. setcolor function is used to change the current drawing color.e.g. setcolor(RED) or
setcolor(4) changes the current drawing color to RED. Remember that default drawing
color is WHITE.
//C programming code for setcolor
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
getch();
closegraph();
return 0;
}
8
14. Setbkcolor:-
Declaration: void setbkcolor(int color);
setbkcolor function changes current background color e.g. setbkcolor(YELLLOW)
changes the current background color to YELLOW.
Remember that default drawing color is WHITE and background color is BLACK.
//C programming code for setbkcolor:
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
setbkcolor(GREEN);
getch();
closegraph();
return 0;
}
15. setfillstyle:
setfillstyle function sets the current fill pattern and fill color.
Declaration: void setfillstyle( int pattern, int color);
Different fill styles:
enum fill_styles
{
EMPTY_FILL,
SOLID_FILL,
LINE_FILL,
LTSLASH_FILL,
SLASH_FILL,
9
BKSLASH_FILL,
LTBKSLASH_FILL,
HATCH_FILL,
XHATCH_FILL,
INTERLEAVE_FILL,
WIDE_DOT_FILL,
CLOSE_DOT_FILL,
USER_FILL
};
//C programming source code for setfillstyle
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
setfillstyle(XHATCH_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
closegraph();
return 0;
}
16. Floodfill:
Declaration: void floodfill(int x, int y, int border);
10
floodfill function is used to fill an enclosed area. Current fill pattern and fill color is used to fill
the area.(x, y) is any point on the screen if (x,y) lies inside the area then inside will be filled
otherwise outside will be filled,border specifies the color of boundary of area. To change fill
pattern and fill color use setfillstyle.
//C programming code for floodfill
#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
setcolor(RED);
circle(100,100,50);
floodfill(100,100,RED);
getch();
closegraph();
return 0;
}
Note: In the above program a circle is drawn in RED color. Point (100,100) lies inside the circle
as it is the center of circle, third argument to floodfill is RED which is color of boundary of circle.
So the output of above program will be a circle filled with WHITE color as it is the default fill
color.
11
fillpoly() function in C:
The header file graphics.h contains fillpoly() function which is used to draw and fill a polygon
i.e. triangle, rectangle, pentagon, hexagon etc.
12
Array arr contains coordinates of triangle which are (320, 150), (400, 250) and (250, 350). Note
that last point(320, 150) in array is same as first.
// C Implementation for fillpoly():
#include<stdio.h>
#include<conio.h>
#include <graphics.h>
int main()
{
13
int gd = DETECT, gm;
// coordinates of polygon
int arr[] = {120, 250, 400, 250, 400,
350, 450, 200, 120, 250};
14
delay(100);
cleardevice();
}
getch();
closegraph();
}
void main( )
{
float x,y,x1,y1,x2,y2,dx,dy,step;
int i,gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
dx=abs(x2-x1);
dy=abs(y2-y1);
15
if(dx>=dy)
step=dx;
else
step=dy;
dx=dx/step;
dy=dy/step;
x=x1;
y=y1;
i=1;
while(i<=step)
{
putpixel(x,y,5);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
closegraph();
}
16
void drawline(int x0, int y0, int x1, int y1)
{
int dx, dy, p, x, y;
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-dx;
while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;
}
x=x+1;
}
}
int main()
{
int gdriver=DETECT, gmode, x0, y0, x1, y1;
17
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
printf("Enter co-ordinates of first point: ");
scanf("%d%d", &x0, &y0);
printf("Enter co-ordinates of second point: ");
scanf("%d%d", &x1, &y1);
drawline(x0, y0, x1, y1);
getch();
return 0;
}
18
x=x+1;
y=y;
d=d+(2*x)+2;
}
else
{
x=x+1;
y=y-1;
d=d+2*(x-y)+1;
}
}while(x<y);
getch();
closegraph();
//getch();
}
19
printf("Line after translation");
line(x1,y1,x2,y2);
getch();
closegraph();
}
20
#include<dos.h>
#include<graphics.h>
void main()
{
int x, gd=DETECT,gm;
//float d;
initgraph(&gd,&gm,"c://Turboc3//BGI");
long x1=100,y1=100,x2=200,y2=200;
double d1,xt,yt;
cleardevice();
printf("\n\n\n\t enter angle of rotation");
scanf("%lf",&d1);//Asking value in degree
d1=(d1*3.14)/180.0;//formula for converting degree in radiance
xt=x1+((x2-x1)*cos(d1)-(y2-y1)*sin(d1));
yt=y1+((x2-x1)*sin(d1)+(y2-y1)*cos(d1));
line(x1,y1,x2,y2);
getch();
cleardevice();
printf("\n LINE AFTER ROTATION");
line(x1,y1,(int)xt,(int)yt);
getch();
21
// point on viewport
int x_v, y_v;
// calculating Sx and Sy
sx = (float)(x_vmax - x_vmin) / (x_wmax - x_wmin);
sy = (float)(y_vmax - y_vmin) / (y_wmax - y_wmin);
// Driver Code
void main()
{
// boundary values for window
int x_wmax = 80, y_wmax = 80, x_wmin = 20, y_wmin = 40;
// point on window
int x_w = 30, y_w = 80;
WindowtoViewport(30, 80, 80, 80, 20, 40, 60, 60, 30, 40);
}
getch();
closegraph();
return 0;
}
22
10. C program to show 3D Translation in graphics:
//Implementation of 3D traslation.
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
#include<conio.h>
void trans();
23
bar3d(midx+50,midy-100,midx+100,midy-50,20,1);
delay(1000);
printf("Enter translation factor(x and y):");
scanf("%d%d",&x,&y);
printf("After translation:");
bar3d(midx+x+50,midy-(y+100),midx+x+100,midy-(y+50),20,1);
delay(1000);
getch();
}
11. //Program for 3D scaling:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int maxx,maxy,midx,midy;
void axis()
{
getch();
cleardevice();
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
}
void main()
{
int x,y,z,o,x1,x2,y1,y2;
int gd=DETECT,gm;
24
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
//setfillstyle(0,getmaxcolor());
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
axis();
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
printf("Enter scaling factors");
scanf("%d%d%d", &x,&y,&z);
//axis();
printf("After scaling");
bar3d(midx+(x*50),midy-(y*100),midx+(x*60),midy-(y*90),5*z,1);
//axis();
getch();
closegraph();
}
12. Program for 2D Relection:
#include <conio.h>
#include <graphics.h>
#include <stdio.h>
// Driver Code
void main()
{
// Initialize the drivers
25
int gm, gd = DETECT, ax, x1 = 100;
int x2 = 100, x3 = 200, y1 = 100;
int y2 = 200, y3 = 100;
26
// After reflection
printf("\nAfter Reflection");
getch();
// Close the graphics
closegraph();
}
13. Program for drawing cycle moving.
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <iostream.h>
int main()
{
int gd = DETECT, gm, i, a;
27
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
// Move the cycle
for (i = 0; i < 600; i++) {
// Upper body of cycle
line(50 + i, 405, 100 + i, 405);
line(75 + i, 375, 125 + i, 375);
line(50 + i, 405, 75 + i, 375);
line(100 + i, 405, 100 + i, 345);
line(150 + i, 405, 100 + i, 345);
line(75 + i, 345, 75 + i, 370);
line(70 + i, 370, 80 + i, 370);
line(80 + i, 345, 100 + i, 345);
// Wheel
circle(150 + i, 405, 30);
circle(50 + i, 405, 30);
// Road
line(0, 436, getmaxx(), 436);
// Stone
rectangle(getmaxx() - i, 436,
650 - i, 431);
28
// Clear the screen
cleardevice();
}
getch();
closegraph();
return 0;
}
The End
29