Loading....
Coupon Accepted Successfully!

 

arc();

This function is useful to draw a circular arc. It requires five arguments. The syntax would be

arc ( int x, int y, int a, int b , int c );

Here, the first two arguments, that is, x and y, serve as the center point. The arguments a and b are the starting and ending angles, respectively, and finally, c is the radius. The following programming example can be referred for drawing an arc.

22.2 Write a program to draw an arc using arc() function.

#include<stdio.h>

#include<conio.h>

#include<iostream.h>

#include<graphics.h>

int main()

{

int gd =DETECT,gm;

 

initgraph(&gd,&gm,“c:\\tc\\bgi”);

arc(200,175,70,180,50);

getch();

closegraph();

restorecrtmode();

return 0;

}

Explanation: Consider the statement initgraph(&gd,&gm,”c:\\tc\\bgi”); which initializes the graphics mode. The required adapter files are present in the specified directory. The arc() function draws an arc by simply using parameter values. The function closegraph() shuts the graphics mode. The function restorecrtmode(); restores the screen mode to the original video mode. The DETECT is the enum data type of BGI graphics drivers.

22.3 Write a program to draw an arc using arc() function. Create a class containing private and public members.

#include<conio.h>

#include<graphics.h>

#include<dos.h>

#include<stdlib.h>

#include<iostream.h>

class Graphics

{

private:

int gd,gm;

public:

void getdata(int x,int y,int s,int e,int r);

void grmode(void);

};

void Graphics::grmode()

{

gd=DETECT;

gm=VGA;

initgraph(&gd,&gm,“c:\\tc\\bgi”);

setpalette(2,3);

setcolor(5);

getche();

}

void Graphics::getdata(int x,int y,int s,int e,int r)

{

arc(x,y,s,e,r);

}

int main()

{

Graphics P;

 

clrscr();

P.grmode();

P.getdata(100,100,10,175,40);

getch();

return 0;

}

Explanation: The concept of class is applied in this program. Private and public members are declared, and the same members are accessed with an object P. The output is an arc with given coordinates. The programmer can view the output on the screen. Similarly, the programmer can run the following programs with an object-oriented approach.

circle():

This function draws a circle at (x, y) of the given radius. This function is declared as below.

void far circle (int x, int y, int radius);

In the above function, x and y are the co-ordinates; that is, they represent the center of the circle.

22.4 Write a program to draw a circle.

#include“graphics.h”

#include“conio.h”

int main()

{

int gd=DETECT,gm,x=130,y=160,radius=100;

initgraph(&gd, &gm, “c:/tc/bgi”);

circle(x,y,radius);

getch();

closegraph();

restorecrtmode();

return 0;

}

Explanation: The function circle() is used to draw the circle().

line():

This function draws a line from (a,b) to (c,d).

22.5 Write a program to draw a line.

#include<stdio.h>

#include<conio.h>

#include<iostream.h>

#include<graphics.h>

 

int main()

{

int gd=DETECT,gm;

clrscr();

initgraph(&gd,&gm,“c:/tc/bgi”);

line(20,20,20,200);

getche();

return 0;

}

Explanation: The function line() is used to draw a line. This function draws a vertical line as per the coordinates. The user may change the starting and ending coordinates and observe the position of the line on the screen.

arc(), circle() and line():

22.6 Write a program to draw circle, line, and arc using graphics function.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

int main()

{

int gd=DETECT,gm,x,y;

initgraph(&gd,&gm,“c:\\tc\\bgi”);

x=getmaxx();

y=getmaxy();

setcolor(WHITE);

outtextxy(1,20,“Circle”);

circle(40,100,40);

outtextxy(200,10,“Arc”);

arc(x/3,y/8,180,70,30);

line (210,150,110,150);

outtextxy(150,140,»Line»);

ellipse(215,150,0,70,150,150);

outtextxy(290,10,»Ellipse»);

getche();

closegraph();

restorecrtmode();

return 0;

}

Explanation: The above program draws circles, arcs, ellipses, and lines depending on the given values. The user can change the arguments in the function and view the results.

line() and outtextxy():

22.7 Write a program to display rectangle by using line() function and display some text within it using outtextxy() function.

#include<stdio.h>

#include<conio.h>

#include<iostream.h>

#include<graphics.h>

int main()

{

int gd=DETECT,gm;

clrscr();

initgraph(&gd,&gm,“c:/tc/bgi”);

line(20,20,20,200);

outtextxy(22,35,“Rectangle made up of lines”);

line(230,20,230,200);

line(230,20,20,20);

line(230,200,20,200);

getche();

return 0;

}

Explanation: In the above program, we have used the line() function to draw a rectangle.

In order to display a rectangle using line(), we have to use the line() function four times.

rectangle():

22.8 Write a program to display rectangle by using rectangle() function .

#include<stdio.h>

#include<conio.h>

#include<iostream.h>

#include<graphics.h>

int main()

{

int gd=DETECT,gm;

clrscr();

initgraph(&gd,&gm,“c:/tc/bgi”);

setcolor(GREEN);

rectangle (50,30,150,200);

getch();

closegraph();

 

restorecrtmode();

return 0;

}

Explanation: This program draws the rectangle using the given arguments. The setcolor() sets the color of the rectangle.

Use of setviewport(), outtextxy() and setcolor():

22.9 Use setviewport(), outtextxy(), setcolor() , closegraph() and display some message in 15 different colors.

#include<stdio.h>

#include<conio.h>

#include<iostream.h>

#include<graphics.h>

int main()

{

int gd =DETECT,gm,j;

initgraph(&gd,&gm,“c:\\tc\\bgi”);

char st[50];

clrscr();

setviewport(00,00,400,400,1);

outtextxy(10,10,“View port”);

for(j=1;j<=15;j++)

{

setcolor(j);

sprintf(st,“Existing Color number is :%d press any key”,j);

outtextxy(5,20+j*20,st);

getch();

}

closegraph();

return 0;

}

OUTPUT:

View port

Existing Color number is :1 press any key

Existing Color number is :2 press any key

Existing Color number is :3 press any key

Existing Color number is :4 press any key

 

Existing Color number is :5 press any key

Existing Color number is :6 press any key

Existing Color number is :7 press any key

Existing Color number is :8 press any key

Existing Color number is :9 press any key

Existing Color number is :10 press any key

Existing Color number is :11 press any key

Existing Color number is :12 press any key

Existing Color number is :13 press any key

Existing Color number is :14 press any key

Existing Color number is :15 press any key

Explanation: In the above program, setviewport(), setcolor(), and outtextxy() are the various functions included. The function setviewport() sets the current viewport for graphics output. The syntax of viewport() is as follows:

viewport(int left, int top , int right, int bottom, int clip)

The parameters at the left and top are used to specify the x and y co-ordinates of the top left corner of the screen.

The parameters at the right and bottom are used to specify the x and y co-ordinates of the bottom right corner of the screen.

The fifth parameter of the viewport(), that is, the clip, is set to 1. The clip = 1 means that no printing takes place outside the viewport.

The function outtextxy() is used to print the text at given co-ordinates, and the function setcolor() is used to set different colors. The programmer can view the output message in different colors by pressing any key.

Use of setfillstyle(), setcolor() and fillellipse()

22.10 The following program illustrates a moving moon.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

int main()

{

int gd =DETECT,gm,x;

initgraph(&gd,&gm,“c:\\tc\\bgi”);

clrscr();

setcolor(15);

for (x=30;x<200;x+=10)

{

setfillstyle(SOLID_FILL,WHITE);

fillellipse (x,50,30,30);

 

sleep(1);

clrscr();

}

closegraph();

restorecrtmode();

return 0;

}

Explanation: In this program using the fillellipse() function, a moon-like shape is drawn. Using the loop, the drawn figure keeps on moving on the screen. The sleep() function halts the execution for a second. The setfillstyle() function sets the current fill pattern and fill color.

Besides the functions as cited earlier in this chapter, some additional functions as mentioned below can be used.

1.     getdrivername() : This function displays the name of graphics driver name. For example EGAVGA, EGA VGA etc.

2.     getmaxcolor() : returns the maximum color available with the adapter.

3.     getmaxmode() : Returns number of display modes available.

4.     getmodename(int) : This function returns name of the graphics mode.

5.     lineto() : This function draws a line. It requires two integer arguments. It draws a line from current position to ( x,y) where x and y are the coordinates to be specified by the user.

6.     moveto() : This function moves the current position to (x,y) where x and y are the coordinates are to be specified by the user.

Following program illustrates some of the above graphics functions..

Use of moveto(), line() and lineto():

22.11 Write a program to shift the line coordinates to other positions with movto() and lineto() functions.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

int main()

{

int gd =DETECT,gm;

initgraph(&gd,&gm,“c:\\tc\\bgi”);

line(20,30,200,30);

moveto(250,50);

lineto(200,50);

getch();

closegraph();

restorecrtmode();

return 0;

}

Explanation: In this program line() function is used for drawing a line Also the effect of movto() and lineto() functions can be seen by executing this program. You will observe that the line coordinates get changed with these functions.





Test Your Skills Now!
Take a Quiz now
Reviewer Name