Screen, Viewport and Graphics Utility Functions in C - Syntax and Examples

The BGI graphics library provides several utility functions to manage the graphics screen, define drawing areas, control drawing operations and retrieve graphics coordinates. In this post, we will learn about cleardevice(), clearviewport(), setviewport(), setwritemode(), getmaxx(), getmaxy(), getx(), gety() and getarccoords() functions.

BGI ग्राफिक्स लाइब्रेरी में ग्राफिक्स स्क्रीन को मैनेज करने, drawing area निर्धारित करने, drawing operations को नियंत्रित करने तथा ग्राफिक्स कोऑर्डिनेट प्राप्त करने के लिए कई utility functions उपलब्ध होते हैं। इस पोस्ट में हम cleardevice(), clearviewport(), setviewport(), setwritemode(), getmaxx(), getmaxy(), getx(), gety() तथा getarccoords() फंक्शन के बारे में जानेंगे।

1. cleardevice() Function

The cleardevice() function is used to clear the entire graphics screen and fill it with the current background color.

cleardevice() फंक्शन का उपयोग पूरी ग्राफिक्स स्क्रीन को साफ करने तथा उसे वर्तमान background color से भरने के लिए किया जाता है।

Syntax:

void cleardevice(void);

Example:

cleardevice();

This statement clears the graphics screen.

यह स्टेटमेंट ग्राफिक्स स्क्रीन को साफ करता है।

2. clearviewport() Function

The clearviewport() function is used to clear the currently defined viewport. It fills the viewport area with the current background color.

clearviewport() फंक्शन का उपयोग वर्तमान में निर्धारित viewport को साफ करने के लिए किया जाता है। यह viewport के क्षेत्र को वर्तमान background color से भर देता है।

Syntax:

void clearviewport(void);

Example:

setviewport(50, 50, 400, 300, 1);
clearviewport();

This statement clears the currently defined viewport.

यह स्टेटमेंट वर्तमान में निर्धारित viewport को साफ करता है।

Difference Between cleardevice() and clearviewport()

cleardevice()clearviewport()
Clears the entire graphics screen.Clears only the current viewport.
Used to clear the whole graphics display.Used to clear a selected drawing area.
cleardevice()clearviewport()
पूरी ग्राफिक्स स्क्रीन को साफ करता है।केवल वर्तमान viewport को साफ करता है।
पूरी ग्राफिक्स डिस्प्ले को साफ करने के लिए उपयोग होता है।निर्धारित drawing area को साफ करने के लिए उपयोग होता है।

3. setviewport() Function

The setviewport() function is used to define a rectangular area of the graphics screen in which graphics operations will be performed.

setviewport() फंक्शन का उपयोग ग्राफिक्स स्क्रीन पर एक आयताकार क्षेत्र निर्धारित करने के लिए किया जाता है, जिसके अंदर graphics operations किए जाते हैं।

Syntax:

void setviewport(int left, int top, int right,
int bottom, int clip);

Parameters:

  • left: Left boundary of the viewport.

  • top: Top boundary of the viewport.

  • right: Right boundary of the viewport.

  • bottom: Bottom boundary of the viewport.

  • clip: If non-zero, drawing is clipped to the viewport boundaries.

पैरामीटर्स:

  • left: viewport की बाईं सीमा।

  • top: viewport की ऊपरी सीमा।

  • right: viewport की दाईं सीमा।

  • bottom: viewport की निचली सीमा।

  • clip: यदि इसका मान non-zero है, तो drawing viewport की सीमाओं के अंदर सीमित रहती है।

Example:

setviewport(100, 100, 400, 300, 1);
rectangle(0, 0, 200, 100);

This statement defines a viewport and draws a rectangle using coordinates relative to that viewport.

यह स्टेटमेंट एक viewport निर्धारित करता है तथा उस viewport के सापेक्ष कोऑर्डिनेट का उपयोग करके आयत बनाता है।

Important: After calling setviewport(), drawing coordinates are interpreted relative to the viewport's top-left corner. The clip parameter controls whether drawing outside its boundaries is clipped.

महत्वपूर्ण: setviewport() को कॉल करने के बाद drawing coordinates viewport के ऊपरी-बाएँ कोने के सापेक्ष माने जाते हैं। clip पैरामीटर यह नियंत्रित करता है कि उसकी सीमाओं के बाहर की drawing को रोका जाएगा या नहीं।

4. setwritemode() Function

The setwritemode() function is used to control how graphics are written onto the screen. It provides two commonly used writing modes: COPY_PUT and XOR_PUT.

setwritemode() फंक्शन का उपयोग यह नियंत्रित करने के लिए किया जाता है कि ग्राफिक्स स्क्रीन पर किस प्रकार लिखे जाएंगे। इसमें सामान्यतः दो writing modes उपलब्ध होते हैं: COPY_PUT तथा XOR_PUT।

Syntax:

void setwritemode(int mode);

Parameters:

  • COPY_PUT: Draws graphics normally by replacing the existing pixels.

  • XOR_PUT: Combines the drawing color with existing pixels using the XOR operation.

पैरामीटर्स:

  • COPY_PUT: सामान्य रूप से drawing करता है तथा पहले से मौजूद pixels को नए pixels से बदलता है।

  • XOR_PUT: XOR operation का उपयोग करके नए drawing color को पहले से मौजूद pixels के साथ combine करता है।

Example:

setwritemode(COPY_PUT);
line(100, 100, 300, 100);
setwritemode(XOR_PUT);
line(100, 150, 300, 150);

The first line is drawn using normal copy mode, while the second line uses XOR mode.

पहली लाइन सामान्य copy mode में तथा दूसरी लाइन XOR mode में बनाई जाती है।

5. getmaxx() and getmaxy() Functions

The getmaxx() and getmaxy() functions return the maximum X and Y coordinates of the graphics screen, respectively.

getmaxx() तथा getmaxy() फंक्शन क्रमशः ग्राफिक्स स्क्रीन के अधिकतम X तथा Y कोऑर्डिनेट प्रदान करते हैं।

Syntax:

int getmaxx(void);
int getmaxy(void);

Example:

int maxx, maxy;
maxx = getmaxx();
maxy = getmaxy();

These statements store the maximum screen coordinates in maxx and maxy.

ये स्टेटमेंट स्क्रीन के अधिकतम कोऑर्डिनेट को maxx तथा maxy में स्टोर करते हैं।

6. getx() and gety() Functions

The getx() and gety() functions return the current graphics position along the X-axis and Y-axis, respectively.

getx() तथा gety() फंक्शन क्रमशः X-axis तथा Y-axis पर वर्तमान ग्राफिक्स पोजीशन प्रदान करते हैं।

Syntax:

int getx(void);
int gety(void);

Example:

moveto(150, 200);
int x, y;
x = getx();
y = gety();

The above statements set the current graphics position to (150, 200) and retrieve its coordinates.

उपरोक्त स्टेटमेंट वर्तमान ग्राफिक्स पोजीशन को (150, 200) पर सेट करते हैं तथा उसके कोऑर्डिनेट प्राप्त करते हैं।

7. getarccoords() Function

The getarccoords() function is used to retrieve the coordinates of the center, starting point and ending point of the last arc drawn using the arc() function.

getarccoords() फंक्शन का उपयोग arc() फंक्शन द्वारा बनाई गई अंतिम arc के केंद्र, प्रारंभिक पॉइंट तथा अंतिम पॉइंट के कोऑर्डिनेट प्राप्त करने के लिए किया जाता है।

Syntax:

void getarccoords(struct arccoordstype *arccoords);

The arccoordstype structure contains the following members:

arccoordstype स्ट्रक्चर में निम्नलिखित members होते हैं:

MemberDescription
xX-coordinate of arc center
yY-coordinate of arc center
xstartX-coordinate of starting point
ystartY-coordinate of starting point
xendX-coordinate of ending point
yendY-coordinate of ending point

Example:

struct arccoordstype arcinfo;
arc(200, 200, 0, 90, 100);
getarccoords(&arcinfo);

The above statements draw an arc and retrieve its coordinates into the arcinfo structure.

उपरोक्त स्टेटमेंट एक arc बनाते हैं तथा उसके कोऑर्डिनेट arcinfo स्ट्रक्चर में प्राप्त करते हैं।

Combined C Program Using Graphics Utility Functions

The following program demonstrates screen clearing, viewport creation, screen coordinates and current graphics position.

निम्न प्रोग्राम में स्क्रीन साफ करने, viewport बनाने, स्क्रीन कोऑर्डिनेट तथा वर्तमान ग्राफिक्स पोजीशन का उपयोग प्रदर्शित किया गया है।

#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main(void)
{
int gd = DETECT, gm;
int maxx, maxy, x, y;
initgraph(&gd, &gm, "C:\\TC\\BGI");
/* Get maximum screen coordinates */
maxx = getmaxx();
maxy = getmaxy();
/* Display screen information */
printf("Maximum X = %d\n", maxx);
printf("Maximum Y = %d\n", maxy);
getch();
/* Clear graphics screen */
cleardevice();
/* Define viewport */
setviewport(50, 50, 400, 300, 1);
/* Draw rectangle inside viewport */
rectangle(0, 0, 200, 100);
/* Set current graphics position */
moveto(100, 150);
/* Get current position */
x = getx();
y = gety();
/* Display current position */
outtextxy(10, 120, "Current Graphics Position:");
getch();
/* Clear viewport */
clearviewport();
/* Close graphics */
closegraph();
return 0;
}

Important Points

  1. cleardevice() clears the entire graphics screen.

    cleardevice() पूरी ग्राफिक्स स्क्रीन को साफ करता है।

  2. clearviewport() clears only the currently defined viewport.

    clearviewport() केवल वर्तमान viewport को साफ करता है।

  3. setviewport() defines a rectangular drawing area.

    setviewport() एक आयताकार drawing area निर्धारित करता है।

  4. setwritemode() controls the graphics writing mode.

    setwritemode() graphics writing mode को नियंत्रित करता है।

  5. getmaxx() and getmaxy() return the maximum screen coordinates.

    getmaxx() तथा getmaxy() स्क्रीन के अधिकतम कोऑर्डिनेट प्रदान करते हैं।

  6. getx() and gety() return the current graphics position.

    getx() तथा gety() वर्तमान ग्राफिक्स पोजीशन प्रदान करते हैं।

  7. getarccoords() retrieves the coordinates of the last arc.

    getarccoords() अंतिम arc के कोऑर्डिनेट प्रदान करता है।

  8. These functions belong to the traditional BGI graphics library and are not standard C functions.

    ये फंक्शन पारंपरिक BGI ग्राफिक्स लाइब्रेरी का हिस्सा हैं, Standard C के फंक्शन नहीं हैं।

Note for Modern C Compilers

The above programs are designed for the traditional Turbo C/BGI graphics environment. The graphics.h library and these graphics functions are not available by default in modern C compilers such as GCC.

ऊपर दिए गए प्रोग्राम पारंपरिक Turbo C/BGI ग्राफिक्स वातावरण के लिए बनाए गए हैं। graphics.h लाइब्रेरी तथा ये ग्राफिक्स फंक्शन GCC जैसे आधुनिक C कंपाइलर में डिफॉल्ट रूप से उपलब्ध नहीं होते हैं।

Comments

Popular posts from this blog

Complete C Programming Language Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025

C Programming for Finance: A Powerful Tool for Financial Modeling and Analysis