![]() |
Salt Lake City Monday, 2008.09.08 09:04 MST [GMT-7] |
| Home - IT: IT Security - Programming - OS - HW - SW - Internet - IT News - Technology - Science - Communication - News: World - USA - USA States - Alternative - Business - Investment - more coming soon... | |
/**
* @title: crect.cpp
* @date: 2004.09.05 - 10:21 am
* @author: vinnie
* @ main file for the app crect.exe
*/
// =============================================
#include <stdio.h>
#include <stdlib.h>
#include <afxwin.h>
//using namespace atl
void main( )
{
// =======================================================================
//
// *** assignment part a ***
//
CRect myRc[100];
int i;
srand( 82); // range 0:100 same seed gives same sequences
// 50, 82
//srand( time( NULL) ); // same seed gives same sequences
// init the array
for ( i = 0; i < 100; i++)
{
int t = -1, l = -1, r = -1, b = -1;
while ( t < 0 || t > 98 )
t = rand();
while ( l < 0 || l > 98 )
l = rand();
while ( b < 1 || b > 99 || b == t )
b = rand();
while ( r < 1 || r > 99 || r == l)
r = rand();
myRc[i].SetRect( t, l, b, r);
} // end of the for loop, array initialization
printf("\n Ok array's full !\n");
// find the rectangle with the smallest area including the point( 25, 25)
int minArea = 10000, minIndex, area;
CPoint tl, br;
CSize mySize;
for ( i = 0; i < 100; i++)
{
// normalize every rect of the array, just in case
// either its width or height is negative
myRc[i].NormalizeRect();
// check if the point( 25, 25) is inside the rect
tl = myRc[i].TopLeft();
br = myRc[i].BottomRight();
if ( tl.x < 25 && tl.y < 25 && br.x > 25 && br.y > 25 )
{
// get the area
mySize = myRc[i].Size();
area = mySize.cx * mySize.cy;
// save the index of the array
if ( minArea > area )
{
minArea = area;
minIndex = i;
} // fi
} // fi
} // rof
//
printf("\n The smallest rect with the point (25,25) is the #%d at ", minIndex+1);
printf("(%d, %d) - (%d, %d)\n", myRc[minIndex].TopLeft(), \
myRc[minIndex].BottomRight());
//
// =======================================================================
//
// *** assignment part b ***
//
minArea = 10000;
for ( i = 0; i < 100; i++)
{
tl = myRc[i].TopLeft();
br = myRc[i].BottomRight();
mySize = myRc[i].Size();
area = mySize.cx * mySize.cy;
// save the index of the smallest area
if ( minArea > area )
{
minArea = area;
minIndex = i;
} // fi
} // rof
unsigned char ordArr[100];
int arInd = 0;
memset( ordArr, 0, 100);
tl = myRc[minIndex].TopLeft();
br = myRc[minIndex].BottomRight();
int x, y, x1, y1, x2, y2;
x1 = tl.x;
y1 = tl.y;
x2 = br.x;
y2 = br.y;
CPoint cPu, cPl; // means CPointUpper and CPointLower
x = x1;
y = y1;
while ( x >= 0 )
{
--x;
while ( y >= 0 )
{
--y;
for ( i = 0; i < 100; i++)
{
cPu = myRc[i].TopLeft();
cPl = myRc[i].BottomRight();
if ( cPu.x <= x && cPu.y <= y && cPl.x >= x2 && cPl.y >= y2)
{
ordArr[arInd] = i;
++arInd;
x = cPu.x;
y = cPu.y;
x2 = cPl.x;
y2 = cPl.y;
break;
} // fi
} // rof
} // elihw
} // elihw
printf( "\n");
for ( i = 0; i < 100; i++) printf( "%d ", ordArr[i]);
printf( "\n");
// =======================================================================
} // niam
Vincenzo Maggio