两个物体覆盖
我有一个 bCoord 数组,其中包含图像的 x、y 位置、宽度和高度。我想将其他对象插入到不互相覆盖的数组中。如果数组对象的大小大于或等于我想要插入其中的对象,则下面的源代码工作得很好,否则不行。我有一个解决方案,但这不是很好。如果有人对这个问题有好的解决方案,请分享给我。
this.isCover = function(pixel, width, height)
{
for (var i=0; i<bCoords.length; i++)
if (isThereBuilding(bCoords[i],pixel.x, pixel.y) || isThereBuilding(bCoords[i],pixel.x+width, pixel.y) ||
isThereBuilding(bCoords[i],pixel.x, pixel.y+height) ||isThereBuilding(bCoords[i],pixel.x+width, pixel.y+height) )
return bCoords[i];
return null;
}
function isThereBuilding(obj,x, y)
{
return (obj.x <= x && (obj.w+obj.x)>= x) && (obj.y <= y && (obj.h+obj.y) >= y);
}
I'm have an bCoord array which is contains the image x, y position, width and height. I want to insert other object to the array which is not cover each others. The source bellow working very well if the array objects size is bigger or equal with object that I want to insert there, otherwise not. I have a solution for that, but that is not very nice. If anybody has a nice solution regarding this problem, please share me.
this.isCover = function(pixel, width, height)
{
for (var i=0; i<bCoords.length; i++)
if (isThereBuilding(bCoords[i],pixel.x, pixel.y) || isThereBuilding(bCoords[i],pixel.x+width, pixel.y) ||
isThereBuilding(bCoords[i],pixel.x, pixel.y+height) ||isThereBuilding(bCoords[i],pixel.x+width, pixel.y+height) )
return bCoords[i];
return null;
}
function isThereBuilding(obj,x, y)
{
return (obj.x <= x && (obj.w+obj.x)>= x) && (obj.y <= y && (obj.h+obj.y) >= y);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用此函数来检查一个对象是否与另一个对象重叠:
可在此处找到。
You can use this function to check if an objects overlaps another:
Found here.