Thoughts Of The Day

RECTANGLE


                         To draw Rectangle in C/C++, firstly we have to initialize graphics and also include “graphics.h” header file in our program. If you don’t know how to initialize graphics, read previous chapters Graphics Basic – 2.

                        In C/C++ programming there is a function called “rectangle” for draw a rectangle.

 Let’s see prototype of the function –

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

To draw rectangle you will have to pass all 4 parameters in this function.


 First two parameter left and top are represented as a “upper left” corner of the rectangle.
 Similarly right and bottom is represented as a “lower right” corner of the rectangle.

See a program for example -

#include <conio.h>
 #include <graphics.h>

Void main()
{
int gd = DETECT , gm ;
initgraph( &gd, &gm, “ ” );

rectangle(100,100,200,200);


getch();
closegraph();

}

 Rectangle function does not return any value.

                      Now attention here – I passed 100, 100 as first two parameters of the rectangle function and 200, 200 as last two parameters.
  
                      If you pass same values in all parameter, like “rectangle (100,100,100,100);” so you will not see rectangle on output screen. Don’t worry it is there. Look carefully it is showing as a point on your output screen.

                      Now come to the point - How to pass values in the rectangle function

                      Here I am trying to explain in a very simple way that how to pass values in rectangle function.

Let’s have a look –

                     When you pass any value in parameter of rectangle function, compiler takes it as a distance. Such as I passed 100 in first parameters, so it means that compiler will skip 100 pixels from left side to draw the left arm of rectangle.
     
                     Same as when we write 100 in top so it skip 100 pixel from top and draw top arm of the rectangle.

                     So Now I hope you would understand how to draw rectangle.




                     Just try by yourself. And if you have any doubt or any confusion to draw a rectangle so you can freely ask your question in comment box. And don’t forget to like our facebook page
  
                     In next discussion we will learn some important text formatting functions in graphics.  


Programming World -Index

No comments:

Post a Comment


For MNCs Interview Preparation And Practice set of C,C++,JAVA,PHP Questions Like our page.