tags:

views:

43

answers:

2

in this code i don't understand why teacher used sometimes +value, - value;

/******************************************/
// function void returnSquares(POINT point, int value)
void returnSquares(POINT point, int value) {
    SQUARE tabSquares[4];       // table of squares that we are creating
    int i;

    // getting points of 4 squares
    // for first square input point is point C
    tabSquares[0].pointA.dimX = point.dimX - value;
    tabSquares[0].pointA.dimY = point.dimY + value;
    tabSquares[0].pointB.dimX = point.dimX;
    tabSquares[0].pointB.dimY = point.dimY + value;
    tabSquares[0].pointC.dimX = point.dimX;
    tabSquares[0].pointC.dimY = point.dimY;
    tabSquares[0].pointD.dimX = point.dimX - value;
    tabSquares[0].pointD.dimY = point.dimY; 

    // for 2nd square input point is point D
    tabSquares[1].pointA.dimX = point.dimX;
    tabSquares[1].pointA.dimY = point.dimY + value;
    tabSquares[1].pointB.dimX = point.dimX + value;
    tabSquares[1].pointB.dimY = point.dimY + value;
    tabSquares[1].pointC.dimX = point.dimX + value;
    tabSquares[1].pointC.dimY = point.dimY;
    tabSquares[1].pointD.dimX = point.dimX;
    tabSquares[1].pointD.dimY = point.dimY; 

    // for 3rd square input point is point A
    tabSquares[2].pointA.dimX = point.dimX;
    tabSquares[2].pointA.dimY = point.dimY;
    tabSquares[2].pointB.dimX = point.dimX + value;
    tabSquares[2].pointB.dimY = point.dimY;
    tabSquares[2].pointC.dimX = point.dimX + value;
    tabSquares[2].pointC.dimY = point.dimY - value;
    tabSquares[2].pointD.dimX = point.dimX;
    tabSquares[2].pointD.dimY = point.dimY - value;

    // for 4th square input point is point B
    tabSquares[3].pointA.dimX = point.dimX - value;
    tabSquares[3].pointA.dimY = point.dimY;
    tabSquares[3].pointB.dimX = point.dimX;
    tabSquares[3].pointB.dimY = point.dimY;
    tabSquares[3].pointC.dimX = point.dimX;
    tabSquares[3].pointC.dimY = point.dimY - value;
    tabSquares[3].pointD.dimX = point.dimX - value;
    tabSquares[3].pointD.dimY = point.dimY - value;


    for (i=0; i<4; i++) {
        printf("Square number %d\n",i); // now we print parameters of each point in current Square
        printf("point A x= %0.2f y= %0.2f\n",tabSquares[i].pointA.dimX,tabSquares[i].pointA.dimY); 
        printf("point B x= %0.2f y= %0.2f\n",tabSquares[i].pointB.dimX,tabSquares[i].pointB.dimY);
        printf("point C x= %0.2f y= %0.2f\n",tabSquares[i].pointC.dimX,tabSquares[i].pointC.dimY);
        printf("point D x= %0.2f y= %0.2f\n",tabSquares[i].pointD.dimX,tabSquares[i].pointD.dimY);
    }
}
A: 
thecoshman
thank you :))))
osabri
+1  A: 

Consider the image:

          C
D *-------*
  |       |
  |Square |
  |       |
  *-------* B
  A

The points A,B,C and D are marked as per their order assumed in your SQUARE data type.

Given a point X and the length of the square, the function generates four squares of that length. The order of these squares (as indicated by their index in the tabSquares array) is as follows:

*-------*------*
|       |      |
| Sq-3  | Sq-2 |
|       |      |
*-------X------*
|       |      |
| Sq-0  | Sq-1 |
|       |      |
*-------*------*

Thus, tabSquares[0] is the square marked Sq-0 and its pointA is the bottom left corner. The x coordinate of that point is value units less than the x coordinate of the given point and y coordinate is value units more than y coordinate of the given point. (x increases from left to right and y increases from top to bottom - origin is the top left corner of the drawing area)

Hence:

tabSquares[0].pointA.dimX = point.dimX - value;
tabSquares[0].pointA.dimY = point.dimY + value;
Amarghosh
thank a loot for your explanation :))
osabri