## 四叉树 原

梦想游戏人

``````
class Rect
{
public:
int x = 0, y = 0, w = 100, h = 100;
Rect(int x, int y, int w, int h) :x(x), y(y), w(w), h(h){}
Rect(){};
};

class Point
{
public:
int x = 90, y = 10;
Point(){};
Point(int x, int y) :x(x), y(y){};

};

{
public:
Rect *rect;
int depth = 0;
vector<Point*> obj;

void insert(Point* p)
{
obj.push_back(p);
}
{
if (child == 0)return;

this->child.push_back(child);
}

{

if (p->x > rect->x + rect->w &&  p->y > rect->y + rect->h)return 0;

if (child.size() ==0) return this;

int cx = (rect->x + rect->w) / 2;
int cy = (rect->y + rect->h) / 2;

int index = 0;

if (p->x > cx && p->y > cy)
{
index = 0;

}

else if (p->x<=cx && p->y>=cy)
{
index = 1;
}

else if (p->x <= cx && p->y <= cy)
{
index = 2;
}
else if (p->x>cx&&p->y<cy)
{
index = 3;

}

return child[index]->getByPoint(p);

return 0;

for (int i = 0; i < child.size(); i++)
{
QuadTree * b = child[i]->getByPoint(new Point(p->x / 2, p->y / 2));

if (b == 0)
{

}
else
{
return    b->getByPoint(new Point(p->x/2,p->y/2));
}
}

//	if (child.size() != 0)return 0;

if (p->x >= rect->x && p->x <= rect->x + rect->w)
{
if (p->y >= rect->y && p->y <= rect->y + rect->h)
{
cout << " find " << endl;

return this;
}

}
return 0;

cout << "root   " << rect->x << "      " << rect->y << endl;

}

{

for (auto & node : child)
{
if (node->getObjs(rec) == 0)
{
n = node;
}
}

if (n)
{
return n->getObjs(rec);

}
return 0;
}*/

Rect** split()
{
Rect ** ret = new Rect *[4];
ret[0] = new Rect;
ret[1] = new Rect;
ret[2] = new Rect;
ret[3] = new Rect;

int x = rect->x;
int y = rect->y;
int w = rect->w;
int h = rect->h;

for (int i = 0; i < 4; i++)
{
ret[i]->h = h / 2;
ret[i]->w = w / 2;
}

ret[0]->x = (x + w) / 2;
ret[0]->y = (y + h) / 2;

ret[1]->x = x;
ret[1]->y =( y+h) / 2;

ret[2]->x = x;
ret[2]->y = y;

ret[3]->x = (x+w) / 2;
ret[3]->y = y;

return ret;

}

};

{
if (depth == 0) return 0;

Parent->depth = depth;
Parent->rect = rect;
Rect ** path4 = Parent->split();

Parent->insert(creater(depth - 1, path4[0]));
Parent->insert(creater(depth - 1, path4[1]));
Parent->insert(creater(depth - 1, path4[2]));
Parent->insert(creater(depth - 1, path4[3]));

return Parent;

}

int main(...)
{
QuadTree *root = creater(4, new Rect(0, 0, 100, 100));

system("pause");
return 0;
}

``````

### 梦想游戏人

2013/12/25
1K
0

zhanxinhang
2011/08/21
0
0

02/26
0
0

indulge_in
2017/11/21
0
0
【javascript实现】几道题目带你学习二叉搜索树

2018/11/27
0
0

Spring Boot 2.x基础教程：快速入门

1
0
SpringSecurity认证流程源码级详解

SpringSecurity认证流程源码级详解 认证流程说明 认证结果如何在多个请求之间共享 获取认证用户信息

chendom

1
0
C语言中的volatile——让我保持原样

volatile译为：易变的。这不是和题目的让我保持原样矛盾了吗？其实不然，在变量前加上该关键字修饰，确实是告诉编译器，这个变量是一个容易改变的变量，不要对它进行优化，每次都要到变量的地...

1
0

XuePeng77

1
0
JavaEE开发的颠覆者SpringBoot实战摘要笔记

7
0