Home »
C programs »
graphics.h header file functions
setviewport() function of graphics.h in C
In this article, we are going to learn about the setviewport() function of graphics.h header file and use it with different parameters.
Submitted by Manu Jemini, on March 21, 2018
This is a demonstration about how can we restrict someone to use a particular portion of the graphical plane. There is a function called setviewport() from the graphics.h, which allows us to make a rectangle on the plane where the program cannot draw anything.
This is particularly useful in making drawings and complex sketches in C. To use this function we have to pass five parameters which will define the- LEFT, TOP, RIGHT, BOTTOM and CLIP in the function,
This is all you have to do to make a call for this function, You can see that we have used the 50, 50 as the center of the circle which makes it draw outside the rectangle are but also restrict the part which comes inside the area.
graphics.h - setviewport() function in C
#include <graphics.h>
#include <conio.h>
int main()
{
//initilizing graphic driver and
//graphic mode variable
int graphicdriver=DETECT,graphicmode;
//calling initgraph with parameters
initgraph(&graphicdriver,&graphicmode,"c:\\turboc3\\bgi");
//Printing message for user
outtextxy(50, 50 + 50, "Program to try setviewport in C graphics");
//declaring variable;
int middleofx, middleofy;
//getting middle of x and y
middleofx = getmaxx()/2;
middleofy = getmaxy()/2;
//setting viewport
setviewport(middleofx - 50, middleofy - 50, middleofx + 50, middleofy + 50, 1);
//creating circle
circle(50, 50, 55);
getch();
return 0;
}
Output