g大好崇拜您啊,厉害
哥我只是个菜鸟,又不是你的女神;P 本帖最后由 橡皮 于 2023-5-13 15:02 编辑
程序很顶,提供帮助极大,我在试用期间有以下疑问或者说问题(以添加圆为例,进行说明):
1. 创建新节点的时候(代码964行),此处按理应该是先添加叶子节点 GQtree之后把数据添加上,以下代码会以待添加的圆圆心当做叶子节点的左下角,效果如下:
n->child = new nodeType(x, y, r, data);
n->node_size -= 1;
2. 判断两圆相交(259行处函数),这个计算原理是啥,想了半天没明白,我换了一种方法也贴在下边
// 两圆是否相交
bool Intersect(const nodeType* n, COOR_TYPE x, COOR_TYPE y, COOR_TYPE r) const
{
COOR_TYPE x0 = (n->x + n->x1) / 2;
COOR_TYPE y0 = (n->y + n->y1) / 2;
COOR_TYPE vx = x > x0 ? x - x0 : x0 - x;
COOR_TYPE vy = y > y0 ? y - y0 : y0 - y;
COOR_TYPE hx = n->x > x0 ? n->x - x0 : x0 - n->x;
COOR_TYPE hy = n->y > y0 ? n->y - y0 : y0 - n->y;
COOR_TYPE ux = vx > hx ? vx - hx : 0;
COOR_TYPE uy = vy > hy ? vy - hy : 0;
return ux * ux + uy * uy <= r * r;
}
bool Intersect(const nodeType* n, const COOR_TYPE& x, const COOR_TYPE& y, const COOR_TYPE& r) const
{
COOR_TYPE delta = n->r + r;
COOR_TYPE left = n->x - delta;
COOR_TYPE right = n->x1 + delta;
COOR_TYPE top = n->y1 + delta;
COOR_TYPE bottom = n->y - delta;
if (Inbound(left, bottom, right, top, x, y)) return true;
else return false;
//// 详细筛选规则
//if (Between(left, right, x) && Between(n->y, n->y1, y)) return true;
//if (Between(bottom, top, y) && Between(n->x, n->x1, x)) return true;
//if (InReach(n->x, n->y, x, y, delta) || InReach(n->x1, n->y, x, y, delta) || InReach(n->x1, n->y1, x, y, delta) || InReach(n->x, n->y1, x, y, delta)) return true;
//else return false;
}
牛B,厉害,能不能继续优化,添加功能 why1025 发表于 2023-7-10 17:06
牛B,厉害,能不能继续优化,添加功能
俺能力不够,等你优化后共享下。 师傅领进门,修行在个人。
感谢gzxl大师的分享! // 搜索指定宽、高内的节点(点的x y值 宽w 高h)
void Search(const nodeType* n, COOR_TYPE x, COOR_TYPE y, COOR_TYPE w, COOR_TYPE h, std::vector<const nodeType*>& result) const
{
if (n != NULL)
{
if (n->leaf)
{
acutPrintf(_T("n->leaf true"));
// 判断两个矩形是否相交
if (Intersect(n->x, n->y, n->w, n->h, x, y, w, h))
{
acutPrintf(_T(" push_back"));
result.push_back(n);
}
}
else if (Intersect(n, x, y, w, h) && n->node_size > 0)
{
acutPrintf(_T("n->leaf false"));
for (int i = 0; i < 4; i++)
{
if (n->child)
{
Search(n->child, x, y, w, h, result);
acutPrintf(_T("n->node Search"));
}
}
}
}
}
这段矩形碰撞的原理是什么???为什么判断两个矩形碰撞,先要判断一个矩形和另一个的nodeType,也就是另一个矩形所在的节点是否碰撞???我测试的例子大部分都是检查出来了,就有一个怎么都不对,这个没检查出来的就是我说的这种,一个矩形和节点没碰撞,但是和这个节点里的一个child是碰撞的,不知道我这么理解对不对,这导致循序直接跳过这个nodeType,他的四个child也被跳过了,想完善完善还是能力不足,问题先放着,先谢谢g大,确实牛b
页:
1
[2]