Draw different shapes using drawpoly function in c graphics.

C graphics program to draw different shapes using drawpoly function.
To draw different shapes by using drawpoly function in c graphics and its also explain that how to get coordinate value to draw different shapes in drawpoly function like line, trianlgle,rectangle, rhombus, hexagon and etc.  For that first we need to understand its syntax and that how its take arguments and values in function. You can get brief introduction of drawpoly function of c graphics from here.

Lets consider the basic syntax of drawpoly function.

Syntax of drawpoly
                                           drawpoly(int num, int points[]);

drawpoly function takes two arguments first is num which is the total number of vertices + 1 on the figure. For example if we draw rectangle than there is 4 vertices in rectangle so here num is 5 (num + 1). Than after second argument is an array of points which represent the x and y coordinates of every vertices.  The size of array is num * 2. For example rectangle has 4 vertices so in that case the value of num is 5 (4+1). and the size of array is 5*2= 10. Which represent x and y coordinates of each vertices.


C program to draw a line using drawpoly function.

Let draw a simple line by using drawpoly function so you will get better idea that how drawpoly function actually works. To draw line we need 2 vertices A and B. So num is 2. Remember one thing and don't be confused here we could not add 1 with num because we could not join end vertices with origin vertices. So the size of array is num * 2 (2*2=4) . Now in points array your first two arguments are the x and y coordinates of first vertices A respectively. and 3rd and 4th arguments are the x and y coordinates of second vertices B respectively. Note that we could not add 1 in num while we draw a line because there is we need to draw line from A to B only there is no need to draw line again from B to A. Lets consider one example of c graphics.
#include <graphics.h>
#include <stdio.h>

int main( )
{
    int gd=DETECT, gm;
    int points[]={50,70,300,70};
    initgraph(&gd,&gm,"C:\\tc\\bgi");

    drawpoly(2,points);
    getch();
    closegraph();

    return 0;
}




Now lets consider the arguments in above example num is 2. its vertices of line. here we can not add 1 because we did not connect any line from end vertices to origin vertices. Now consider the point array in which first value is 50 its represent value of x coordinate of first vertices of line. second is 70 its a value of y coordinates of first vertices. Than 3rd value is 300 which represent x coordinate of second vertices and 4th value is 70 which represent y coordinate of second vertices. In which drawpoly function draw line from first two value that is x and y coordinate of first vertices to its immediate two value that is x and y coordinate of second vertices.


C graphics program to draw triangle by using drawpoly function. 
To draw triangle using drawpoly function. Let first assign value to the arguments. In triangle there is 3 vertices and all are connect with each other like from A to B, B to C, and C to A so here value of num is 4 (3 + 1). In line function we could not add 1 because there is a line from A to B only we could not require line from B to A. So the value of num is 4, So the size of points array is 4*2= 8. Lets see c graphics example.
#include <graphics.h>
#include <stdio.h>

int main( )
{
    int gd=DETECT, gm;
    int points[]={200,120,50,220,350,220,200,120};
    initgraph(&gd,&gm,"C:\\tc\\bgi");

    drawpoly(4,points);
    getch();
    closegraph();

    return 0;
}




Now examine the arguments of above example. in which num is 4 because there is 3 vertices in triangle A, B, and C. We need to add one with 3 because we need to connect A to B, B to C and C to A. so value of num is 4. Now points array in which there are 8 values that is num*2= 8. In which first two arguments are the origin of triangle and x and y coordinate of first vertices. Than after 3rd and 4th argument are x and y coordinate of second vertices respectively. Than 5th and 6th argument is the value x and y coordinate of third vertices of triangle respectively. and the 7th and 8th argument are same as first and second because its join the C and A. confused???  Let me simply it First two arguments is the origin than 3rd and 4th is second vertices so it connect A vertices with B vertices. than after 5th and 6th value its x and y coordinate of C vertices. so it connect B to C. now we need to connect C to A so we already specify the x and y coordinate of C now we need to connect C with A so we need to specify the x and y coordinate of A again because in drawpoly function it draw line to its immediate x and y coordinate.


C graphics program to draw rectangle by using drawpoly function.     

To draw rectangle by using drawpoly function in c graphics. lets consider the value of arguments of drawpoly function. In rectangle there are 4 vertices so value of num is 4+1 =5. So the points array size is num * 2 =10. And last two arguments are the same as first two arguments. Now lets try one example of c graphics to draw rectangle.

#include <graphics.h>
#include <stdio.h>

int main( )
{
    int gd=DETECT, gm;
    int points[]={150,130,350,130,350,400,150,400,150,130};
    initgraph(&gd,&gm,"C:\\tc\\bgi");


    drawpoly(5,points);
    getch();
    closegraph();

    return 0;
}



Now before understand that how to assign coordinate value to draw rectangle in drawpoly function you need to just remember some of the facts that in rectangle every side is equal to its parallel side or opposite side. Now understand it in terms of coordinate value, lets consider 4 vertices A,B,C and D. Its start from top-left corner. So AB side is equal to DC side and BC side is DA. In simple term the distance between A to B is equal to the distance of D to C. And distance between B to C and D to A are equal.

                                          Now observe the value of points array to draw rectangle that last two argument is same as first two argument that is  150 and 30 that is x and y coordinate of A. than after 3rd and 4th argument is 350 and 130 that is x and y coordinate of second vertices that is B. 5th and 6th argument 350 and 400 that is x and y coordinate of third vertices that is C. And 7th and 8th arguments is 150 and 400 that is x and y coordinate of D. and in last 9th and 10th argument is same as first and second argument respectively as x and y coordinate of A. Now over here lets find distance between A to B is 200. (350 and 150 is the x coordinate of B and A respectively) so its distance is equal to its opposite side that is DC so the distance between the D to C is 200. (350-150=200, 350 and 150 is the x coordinate of C and D respectively). now consider another two sides that is BC and AD. difference between B to C and A to D is 270. To find distance in horizontal sides consider the value x coordinate and to find distance of vertical sides consider the value of y coordinate.



0 comments:

Post a Comment