Here is a C program to draw a rectangle and a bar on screen using graphics.h header file. In this program, we will draw a rectangle and a bar on screen. We will use rectangle and bar functions of graphics.h header file to draw rectangle and bar on screen. Below is the detailed descriptions if these two functions.
Function | Description |
---|---|
initgraph | initgraph is a fundamental function in Turbo C graphics that initializes the graphics system. It sets up the graphics mode, graphics driver, and the location of the BGI (Borland Graphics Interface) driver file. This function must be called before any graphics operation is performed. |
rectangle | The rectangle function is used to draw a rectangle on the screen. It takes two pairs of coordinates, representing the diagonally opposite corners of the rectangle, and draws the outline of the rectangle. |
bar | The bar function is used to draw a filled rectangle (bar) on the screen. It takes two pairs of coordinates, representing the diagonally opposite corners of the filled rectangle, and fills the interior with the current drawing color. |
closegraph | closegraph terminates the graphics mode and deallocates any system resources that were acquired during the initialization of the graphics system. It should be called before exiting a graphics program. |
These functions collectively provide essential capabilities for drawing and manipulating graphical elements in Turbo C. Understanding and effectively utilizing them empower programmers to create diverse visual elements and designs in their graphics applications.
C program to draw rectangle and bar using graphics
#include<stdio.h> #include<graphics.h> int main(){ int gd = DETECT,gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); /* Draw rectangle on screen */ rectangle(150, 50, 400, 150); /* Draw Bar on screen */ bar(150, 200, 400, 350); closegraph(); return 0; }Program Output
Conclusion
In conclusion, this tutorial has guided you through the process of creating a dynamic and visually engaging C program using Turbo C graphics to draw both rectangles and filled bars. By delving into fundamental graphics functions, you've learned how to set up the Turbo C environment, draw various shapes, add vibrant colors, and even make your program interactive by dynamically taking user input.
Graphics programming serves as a fascinating bridge between logical thinking and creative expression. Turbo C, with its nostalgic charm, offers an ideal playground for learning and experimenting with graphics concepts. The knowledge gained from this tutorial provides a solid foundation for further exploration into the captivating world of visual programming.
As you progress in your graphics programming journey, consider expanding your horizons by exploring advanced topics such as animations, user interactions, and integration of multimedia elements. Turbo C graphics, although considered a classic, lays the groundwork for understanding core graphics principles that seamlessly translate into modern programming environments.
The skills acquired here are transferable, empowering you to venture into other graphics libraries and frameworks with confidence. Whether you're aspiring to develop games, simulations, or interactive applications, Turbo C graphics provides a nostalgic yet robust platform for your creative endeavors.
Remember, the joy of graphics programming lies in the fusion of logic and creativity. Embrace the endless possibilities, and let your imagination flourish in the realm of visual programming. Happy coding!
Related Topics