Пример #1
0
int main (void)
{
   
   int length = 0;
   int width  = 0;     
   int i      = 0;     
   int rectangleList[NUMRECTANGLES][NUMATTRIBUTES];
   

   PrintGreeting();

   /* Get input from user */

   printf("Please enter the length and width of ");
   printf("%d", NUMRECTANGLES);
   printf(" rectangles\n");
   printf("in the form of length, width (number comma number)\n");
   printf("- one pair on each line\n\n");
   
   /* Read in the length and width values for each rectangle */
   /* Store each value in an element of a 2-dimensional array */
   
   for (i = 0; i < NUMRECTANGLES; i++)
   {
      /* Read length and width and store in array  */
      scanf ("%d,%d", &length, &width);

      rectangleList[i][LENGTH] = length;
      rectangleList[i][WIDTH] = width;


      /******************** STEP 3  ***********************
       * Write a couple lines of code to calculate the    *
       * perimeter and area of the current rectangle,     *
       * and store the perimeter and area in elements 2   *
       * and 3 of the second dimension of the array.      *
       *                                                  *
       * Use the CalculatePerimeter and CalculateArea     *
       * functions from geometry.h                        *
       ***************************************************/
      rectangleList[i][PERIMETER] = CalculatePerimeter(length, width);
      rectangleList[i][AREA] = CalculateArea(length, width);

   }
    
   printf("\nThe properties of the rectangle(s) are:\n\n");

   /* The header for a results table */
   PrintTableHeader();

   /* Print out the length, width, perimeter and area of each rectangle */
   /* Print one per line */

   /******************* STEP 2 ***************************
    * Uncomment the following statement to call the      *
    * function which actually print out a table based on *
    * the values in our array.                           *
    *                                                    *
    * Note that we are PASSING the array here!           *
    *****************************************************/
   
   
     PrintRectangleTable( rectangleList, NUMRECTANGLES );
   
   
   printf("\n");

   return 0;
}
Пример #2
0
void CPatch::Set (uvar32_64 nLength, uvar32_64* pnOffsets) {
	m_dwCntLen = nLength;
	m_pdwContent = pnOffsets;

	uvar16_32	nX, nY,
			nDimX = *(m_pImg->m_pnDimX),
			nDimY = *(m_pImg->m_pnDimY);

	m_pntApex = CPoint (uvar16_32 (-1), uvar16_32 (-1));
	m_rectPatch.SetRectEmpty ();
	m_rectPatch.left = m_rectPatch.top = uvar16_32 (-1);
	m_pntGravCnt = CPoint (0,0);
	for (uvar32_64 dwCntPtr = 0 ; dwCntPtr < m_dwCntLen ; ++dwCntPtr) {
		nX = SRCOFFSET_TO_COORDX (m_pdwContent[dwCntPtr]);
		nY = SRCOFFSET_TO_COORDY (m_pdwContent[dwCntPtr]);
		m_pntGravCnt.x += nX;
		m_pntGravCnt.y += nY;
		if (nX < m_rectPatch.left)	m_rectPatch.left = nX;
		if (nY < m_rectPatch.top)	m_rectPatch.top = nY;
		if (nX > m_rectPatch.right)	m_rectPatch.right = nX;
		if (nY > m_rectPatch.bottom)	m_rectPatch.bottom = nY;
		if (m_pdwContent[dwCntPtr] < COORD_TO_SRCOFFSET (m_pntApex.x, m_pntApex.y))
			m_pntApex = CPoint (nX, nY);
	}
	m_pntGravCnt.x /= m_dwCntLen;
	m_pntGravCnt.y /= m_dwCntLen;

	// Extracting mask
	m_nDimX = m_rectPatch.Width () + 1;
	m_nDimY = m_rectPatch.Height () + 1;
	m_nArea = m_dwMskLen = m_nDimX * m_nDimY;
	m_pbMask = (ubyte*)aimMemoryCommit (m_dwMskLen, "CPatch::Set", "m_pbMask");
	for (dwCntPtr = 0 ; dwCntPtr < m_dwCntLen ; ++dwCntPtr)
		m_pbMask[COORD_TO_MSKOFFSET (SRCOFFSET_TO_COORDX (m_pdwContent[dwCntPtr]) - m_rectPatch.left,
		                             SRCOFFSET_TO_COORDY (m_pdwContent[dwCntPtr]) - m_rectPatch.top)] = 1;
	m_dwConLen = 0;
	for (nX = 0 ; nX < m_nDimX ; ++nX)
		for (nY = 0 ; nY < m_nDimY ; ++nY) {
			if (m_pbMask[COORD_TO_MSKOFFSET (nX, nY)] != 1)
				continue;
			if (nX == 0 || nY == 0 || nX == m_nDimX - 1 || nY == m_nDimY - 1) {
				m_pbMask[COORD_TO_MSKOFFSET (nX, nY)] = 2;
				++m_dwConLen;
			}
			else if (m_pbMask[COORD_TO_MSKOFFSET (nX - 1, nY)] == 0 ||
			         m_pbMask[COORD_TO_MSKOFFSET (nX + 1, nY)] == 0 ||
			         m_pbMask[COORD_TO_MSKOFFSET (nX, nY - 1)] == 0 ||
			         m_pbMask[COORD_TO_MSKOFFSET (nX, nY + 1)] == 0 ) {
				m_pbMask[COORD_TO_MSKOFFSET (nX, nY)] = 2;
				++m_dwConLen;
			}
		}

	// Finding out countour
	m_pdwContour = (uvar32_64*)aimMemoryCommit (m_dwConLen * sizeof (uvar32_64), "CPatch::Set", "m_pdwContour");
	for (DWORD dwMskPtr = 0, dwConPtr = 0 ; dwMskPtr < m_dwMskLen ; ++dwMskPtr)
		if (m_pbMask[dwMskPtr] == 2)
			m_pdwContour[dwConPtr++] = COORD_TO_SRCOFFSET (nX = MSKOFFSET_TO_COORDX (dwMskPtr) + m_rectPatch.left,
			                                               nY = MSKOFFSET_TO_COORDY (dwMskPtr) + m_rectPatch.top);

	// Calculating perimeter
	m_nPerimX = m_nPerimY = m_nPerimXY = 0;
	for (dwMskPtr = 0 ; dwMskPtr < m_dwMskLen ; ++dwMskPtr)
		if (m_pbMask[dwMskPtr] == 2)
			CalculatePerimeter (MSKOFFSET_TO_COORDX (dwMskPtr), MSKOFFSET_TO_COORDY (dwMskPtr));
	m_nPerim = m_nPerimX + m_nPerimY + m_nPerimXY;
	// #### TODO: Add fragments no calculation here
}