0% found this document useful (0 votes)
17 views

practical Example-file (1)

Uploaded by

mishiyama00000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

practical Example-file (1)

Uploaded by

mishiyama00000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Function Used in Graphics Program:

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;

initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

outtext("Press any key to close the graphics mode...");

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

outtext("To display text at a particular position on the screen use outtextxy");

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;

initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

putpixel(25, 25, RED);

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;

initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

arc(100, 100, 0, 135, 50);

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

circle(100, 100, 50);

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

ellipse(100, 100, 0, 360, 50, 25);

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;

initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

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

outtext("Press any key to clear the screen.");


getch();
cleardevice();
outtext("Press any key to exit...");

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

circle(100,100,50); /* drawn in white color */


setcolor(RED);
circle(200,200,50); /* drawn in red color */

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

outtext("Press any key to change the background color to GREEN.");


getch();

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.

Example 1: Drawing a triangle using fillpoly:


int arr[] = {320, 150, 400, 250, 250, 350, 320, 150};

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

int gd = DETECT, gm;


// coordinates of polygon
int arr[] = {320, 150, 400, 250,
250, 350, 320, 150};

initgraph(&gd, &gm, "C:\\Turboc3\\BGI");


setfillstyle(XHATCH_FILL, RED);
fillpoly(4, arr);
getch();
closegraph();
return 0;
}
//polygon filling:
#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};

initgraph(&gd, &gm, "C:\\Turboc3\\BGI");


setfillstyle(XHATCH_FILL, RED);
fillpoly(4, arr);
getch();
closegraph();
return 0;
}

2. Write a C program for moving circle in Graphics


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm;
int i;
initgraph(&gd,&gm,"C://turboC3//BGI");
for(i=0;i<200;i++)
{
circle(100,100+i,50);

14
delay(100);
cleardevice();
}
getch();
closegraph();
}

3. DDA Line Drawing Algorithm:


#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <dos.h>

void main( )
{
float x,y,x1,y1,x2,y2,dx,dy,step;
int i,gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\turboc3\\bgi");

printf("Enter the value of x1 and y1 : ");


scanf("%f%f",&x1,&y1);
printf("Enter the value of x2 and y2: ");
scanf("%f%f",&x2,&y2);

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

4. Bresenhams Line Drawing Algorithm:


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

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

5. Mid- Point Circle Drawing Algorithm


#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
void main()
{
int r,x,y,gd=DETECT,gm;
float d;
initgraph(&gd,&gm,"c://Turboc3//BGI");
printf("enter the value of radius:");
scanf("%d",&r);
x=0;
y=r;
d=1-r;
do
{
putpixel(350+x,350+y,GREEN);
putpixel(350-x,350+y,WHITE);
putpixel(350+x,350-y,WHITE);
putpixel(350-x,350-y,RED);
putpixel(350+y,350+x,RED);
putpixel(350-y,350+x,GREEN);
putpixel(350+y,350-x,BLUE);
putpixel(350-y,350-x,WHITE);
if(d<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();
}

6. Write a C program to show 2D Translation in Graphics:


//2D Translation
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
int graphdriver=DETECT,graphmode;
int i;
int x2,y2,x1,y1,x,y;
printf("Enter the 2 line end points:");
printf("x1,y1,x2,y2");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");
line(x1,y1,x2,y2);
printf("Enter translation co-ordinates ");
printf("x,y");
scanf("%d%d",&x,&y);
x1=x1+x;
y1=y1+y;
x2=x2+x;
y2=y2+y;

19
printf("Line after translation");
line(x1,y1,x2,y2);
getch();
closegraph();
}

7. Write a C program for 2D Scaling Graphics:


//Program for 2D Scaling:
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
int graphdriver=DETECT,graphmode;
int i;
int x2,y2,x1,y1,x,y;
printf("Enter the 2 line end points:");
printf("x1,y1,x2,y2");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
initgraph(&graphdriver,&graphmode,"C:\\turboc3\\BGI");
line(x1,y1,x2,y2);
getch();
printf("Enter scaling co-ordinates ");
printf("x,y");
scanf("%d%d",&x,&y);
x1=(x1*x);
y1=(y1*y);
x2=(x2*x);
y2=(y2*y);
printf("Line after scaling");
line(x1,y1,x2,y2);
getch();
closegraph();
}

8. Write a C program for 2D Rotation in Graphics


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

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

9. C Program to Implement Window to view port transformation in graphics


// C program to implement Window to ViewPort Transformation
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
//line(100, 100, 200, 200);
// Function for window to viewport transformation
void WindowtoViewport(int x_w, int y_w, int x_wmax,
int y_wmax, int x_wmin, int y_wmin,
int x_vmax, int y_vmax, int x_vmin,
int y_vmin)
{

21
// point on viewport
int x_v, y_v;

// scaling factors for x coordinate and y coordinate


float sx, sy;

// calculating Sx and Sy
sx = (float)(x_vmax - x_vmin) / (x_wmax - x_wmin);
sy = (float)(y_vmax - y_vmin) / (y_wmax - y_wmin);

// calculating the point on viewport


x_v = x_vmin + (float)((x_w - x_wmin) * sx);
y_v = y_vmin + (float)((y_w - y_wmin) * sy);
printf("The point on viewport: (%d, %d )\n ", x_v, y_v);
}

// Driver Code
void main()
{
// boundary values for window
int x_wmax = 80, y_wmax = 80, x_wmin = 20, y_wmin = 40;

// boundary values for viewport


int x_vmax = 60, y_vmax = 60, x_vmin = 30, y_vmin = 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();

//these are left,top,right,bottom parameters for bar3d function


int maxx,maxy,midx,midy;
void main()
{
int ch;
int gd=DETECT,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C://Turboc3//BGI ");
trans();
//return 0;
}
//function for translation of a 3d object
void trans()
{
int x,y,z,x1,x2,y1,y2;
midx=200;
midy=200;
//function to draw 3D rectangular box

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;

// Add in your BGI folder path


// like below initgraph(&gd, &gm,
// "C:\\TURBOC3\\BGI");
initgraph(&gd, &gm, "C:\\turboc3\\BGI");
cleardevice();

// Draw the graph


line(getmaxx() / 2, 0, getmaxx() / 2,
getmaxy());
line(0, getmaxy() / 2, getmaxx(),
getmaxy() / 2);

// Object initially at 2nd quadrant


printf("Before Reflection Object"
" in 2nd Quadrant");

// Set the color


setcolor(14);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
getch();

26
// After reflection
printf("\nAfter Reflection");

// Reflection along origin i.e.,


// in 4th quadrant
setcolor(4);
line(getmaxx() - x1, getmaxy() - y1,
getmaxx() - x2, getmaxy() - y2);

line(getmaxx() - x2, getmaxy() - y2,


getmaxx() - x3, getmaxy() - y3);

line(getmaxx() - x3, getmaxy() - y3,


getmaxx() - x1, getmaxy() - y1);

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

// Stop the screen for 10 secs


delay(100);

28
// Clear the screen
cleardevice();
}
getch();
closegraph();
return 0;
}

The End

29

You might also like