Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VisualC++

R-tree:Space Search Algorithm

3.22/5 (3 votes)
3 Dec 2014CPOL 23.1K   497  
This tip will show you an example for using R-tree.

Introduction

We want to find out what controls are in Rectangle1 and Rectangle2 .

Here are some buttons and two rectangles. "Rectangle1" contains "Button1","Button2","Button5". "Rectangle2" contains "Button3","Button4","Button5".

Image 1

If we want find out what controls are contained by Rectangle1 , then

Click "Test Rectangle1" can show or hide the controls in "Rectangle1".

If we want find out what controls are contained by Rectangle2 , then

Click "Test Rectangle2" can show or hide the controls in "Rectangle2".

Image 2

Image 3

Using the Code

The class description is as follows:

C++
// R-tree class define
template <class DATATYPE,class KEYTYPE,int NUMDIMS,
class KEYTYPEREAL=ELEMTYPE,int TMAXNODES=8,int TMINNODES=TMAXNODES/2> class RTreeEx;

// template argument description
//     DATATYPE - data type
//     KEYTYPE - key type
//     NUMDIMS - Dimension
//     KEYTYPEREAL - The type who can contain KEYTYPE<sup>3
//     TMAXNODES - 3~20
//     TMINNODES - TMAXNODES/2

The usage is as follows:

C++
// Create R-tree object
RT::RTreeEx<CWnd *, LONG, 2> m_rt;

// Insert data
LONG lMin[2], lMax[2];
CRect rc;
pCtrl->GetWindowRect(&rc);
ScreenToClient(&rc);
lMin[0] = rc.left;
lMin[1] = rc.top;
lMax[0] = rc.right;
lMax[1] = rc.bottom;
m_rt.Insert(lMin, lMax, pCtrl);

// Search 
LONG lMin[2], lMax[2];
CRect rc;
lMin[0] = ...;
lMin[1] = ...;
lMax[0] = ...;
lMax[1] = ...;//Particular see source
std::set<CWnd *> setCtrl;
m_rt.Search(lMin, lMax, setCtrl);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)