C++ rectangle() - Draw a rectangle on Screen

- July 12, 2018
rectangle function cspp


In C++, the rectangle() function is used to draw a rectangle between two given points on the screen. The general syntax of this function are looks like this:

rectangle(x1, y1, x2, y2);

Where:
x1 & y1 represent the x and y co-ordinates of the first point.
x2 & y2 represent the x and y co-ordinates of the second point.

All the above parameters are of "int" data type. These may be integer type values or variables.

For example, to draw a rectangle between upper left corner and bottom right corner of the screen, the statement will be as follows:

rectangle(0, 0, 639, 479);
OR
rectangle(639, 479, 0, 0);

In the above statement, point (0, 0) represents the upper left corner of the screen and point (639, 479) represents the bottom right corner of the VGA screen.

Example of rectangle() Function

The following C++ program draws a rectangle between two points (150, 300) & (500, 250).

#include<iostream>
#include<canio>
#include<graphics>
using namespace std;
  main()
   {
      int driver, mode;
      driver = DETECT;
      initgraph (&driver, &m, " ");
      cleardevice();
      rectangle(150, 300, 500, 250);
      getch();
      closegraph();
   }

Leave your reply.
EmoticonEmoticon

 

Start typing and press Enter to search