The Wayback Machine - https://web.archive.org/web/20240703235300/https://www.geeksforgeeks.org/draw-rectangle-c-graphics/
Open In App

Draw Rectangle in C graphics

Last Updated : 04 Oct, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

rectangle() 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.
Syntax :

rectangle(int left, int top, int right, int bottom);

Examples:

Input : left = 150, top = 250, right = 450, bottom = 350;
Output : 


Input : left = 150, top = 150, right = 450, bottom = 450;
Output : 

Below is the implementation of the rectangle function :




// C program to draw a rectangle
#include <graphics.h>
  
// Driver code
int main()
{
    // gm is Graphics mode which is a computer display
    // mode that generates image using pixels.
    // DETECT is a macro defined in "graphics.h" header file
    int gd = DETECT, gm;
  
    // location of left, top, right, bottom
    int left = 150, top = 150;
    int right = 450, bottom = 450;
  
    // initgraph initializes the graphics system
    // by loading a graphics driver from disk
    initgraph(&gd;, &gm;, "");
  
    // rectangle function
    rectangle(left, top, right, bottom);
  
    getch();
  
    // closegraph function closes the graphics
    // mode and deallocates all memory allocated
    // by graphics system .
    closegraph();
  
    return 0;
}


Output:



Previous Article
Next Article

Similar Reads

Draw ellipse in C graphics
Program to draw ellipse in C using graphics.h header file. graphics.h library is used to include and facilitate graphical operations in program. C graphics using graphics.h functions can be used to draw different shapes, display text in different fonts, change colors and many more. Using functions of graphics.h you can make graphics programs, anima
2 min read
Draw a line in C++ graphics
graphics.h library is used to include and facilitate graphical operations in program. graphics.h functions can be used to draw different shapes, display text in different fonts, change colors and many more. Using functions of graphics.h you can make graphics programs, animations, projects and games. You can draw circles, lines, rectangles, bars and
2 min read
Draw circle in C graphics
The header file graphics.h contains circle() function which draws a circle with center at (x, y) and given radius. Syntax : circle(x, y, radius); where, (x, y) is center of the circle. 'radius' is the Radius of the circle. Examples : Input : x = 250, y = 200, radius = 50 Output : Input : x = 300, y = 150, radius = 90 Output : Below is the implement
1 min read
Draw a moving car using computer graphics programming in C
In computer graphics, use graphics.h which provide direct functions to draw different coordinate shapes (like circle, rectangle etc). By using these functions we can draw different objects like car, hut, trees, etc. In this program, we will draw a moving car using line and circles. Functions used in program: delay(n): This function is used for hold
4 min read
Program to draw India Gate using computer graphics in C
In C graphics, the graphics.h functions are used to draw different shapes like circles, rectangles, etc, display text(any message) in a different format (different fonts and colors). By using graphics.h one can make programs, animations, and also games. Function Used: rectangle(l, t, r, b): A function from graphics.h header file which is used to dr
6 min read
C program to draw a cricket ground using computer graphics
In this article, we will discuss how to draw a 2D cricket ground is being designed using computer graphics. Approach: Draw a circle using the circle() function. This will act as the Ground Outline.Color the above circle with green using the setfillstyle() and floodfill() functions.Then to implement 30 yards Outline using the ellipse() function.Then
2 min read
Draw a Chess Board using Graphics Programming in C
Prerequisite: graphics.h, How to include graphics.h in CodeBlocks? In Computer Graphics, we use graphics.h which provide direct functions to draw different coordinate shapes(like circle, rectangle etc). By using these functions we can draw different objects like car, hut, trees etc. In this program, the task is to draw a Chess Board using the funct
4 min read
Draw a smiley face using Graphics in C language
Prerequisite: graphics.h, How to include graphics.h in CodeBlocks? The task is to write a C program to draw a smiley face using graphics in C.To run the program we have the include the below header file: #include &lt;graphic.h&gt; Approach: We will create a Smiley Face with the help below functions: fillellipse(int x, int y, int x_radius, int y_rad
2 min read
Draw a triangle in C++ graphics
Prerequisite: graphics.h, How to include graphics.h in CodeBlocks? The task is to write a C program to make a triangle with the line function of graphics. To run the program we have to include the below header file: #include &lt;graphic.h&gt; Approach: The idea is to create a triangle with the help of several lines. We will draw a line in graphics
2 min read
C program to draw the Olympics Logo using graphics
In this article, we will discuss how to design the Olympics Logo using Graphics. Approach:Draw five circles according to positions in the logo using the function circle().To achieve the outline effect, draw 5 smaller circles over it.There is a black circle as well in the logo and to prevent it from blending in, change the background color.Color all
2 min read
three90RightbarBannerImg