home Sandy, UT
Wednesday, 2017.11.22
04:19 MDT [GMT-7]
Brionews - Company Logo Condition: Partly Cloudy
Temperature: 4°C (39.2°F)
Barometer: 1029 mb and rising
IT
MIX
News
Low Cost Affordable C C++ PHP mySQL Perl Programs Development
Assignment for a Windows C++ class.
Assignment was in consolle mode but including Microsoft Foundation Classes (MFC).
It was due to generate an array of 100 random CRect (MFC) in a given viewport (or window).
Then to find the smallest of them containing the point (25,25).
I am not sure but I think to remember there was a bug in this code, so don't trust it 100 %.
If you don't compile with Borland, change the srand() and rand() functions.
Download the executable: crect.exe
September 5, 2004

CRect

/**
  *  @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

2004.09.05

Vincenzo Maggio