在这个平面上给出一个笛卡尔平面和矩形,左下角有坐标(x1,y1),右上角有坐标(x2,y2)。
现在我需要找到那些矩形的公共区域的计数,矩形的左下角坐标(x1,y1)和右上角坐标(x2,y2)。
如何以有效的方式完成这项工作?
它们可以是x1 y1 x2 y2形式的许多这样的查询,对于给定的矩形,我需要找到重叠矩形的数量。即使两个矩形只共享一个公共点,它们仍然被视为共享公共区域。平面上可以有几个相同的矩形,它们应该被视为几个不同的矩形。
主要观点是可以在任何特定时刻添加和删除矩形。
约束:
它们总共可以有10 ^ 5个查询。每个坐标可以从1到10 ^ 9。
我的方法:我们知道假设我们有两个矩形R1和R2。设(x1,y1)为R1的左下角的位置,(x2,y2)为其右上角的位置。类似地,let(x3,y3)和(x4,y4)是R2的相应角位置。 R1和R2的交点将是一个矩形R3,其左下角位于(max(x1,x3),max(y1,y3)),右上角位于(min(x2,x4),min(y2) ,y4))。
如果max(x1,x3)> min(x2,x4)或max(y1,y3)> min(y2,y4)则R3不存在,即R1和R2不相交。
现在我面临的主要问题是我们有插入查询说(I X1 Y1 X2 Y2)和删除类型(D索引)的查询,它将删除在插入索引查询时插入的矩形。如何处理他们有效率
答案 0 :(得分:0)
R树应该可以工作。有关详情,请参阅http://en.wikipedia.org/wiki/R-tree。
答案 1 :(得分:0)
我在C#中为类似问题编写了一个解决方案
https://github.com/ERufian/Overlap/blob/master/RectangleOverlap/Heap.cs
通过微小的修改,它可以满足您的需求:
namespace RectangleOperations
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
public static class Heaped
{
public static int GetOverlap(double x1, double y1, double x2, double y2, Rect[] rects, int rectCount)
{
Rect startingArea = new Rect(new Point(x1, y1), new Point(x2, y2));
return Overlap(startingArea, 0, 0, rects, rectCount)
}
private static int Overlap(Rect currentArea, int currentIndex, int currentCount, Rect[] rects, int rectCount)
{
List<int> counts = new List<int>();
for (int i = currentIndex; rectCount > i; i++)
{
Rect newArea = currentArea;
newArea.Intersect(rects[i]);
// include point and edge (size 0) overlaps
if (0 <= newArea.Size.Height && 0 <= newArea.Size.Width)
{
counts.Add(Overlap(newArea, i + 1, currentCount + 1));
}
}
return (0 == counts.Count) ? currentCount : counts.Max();
}
}
}
如果不使用C#
,可以帮助您翻译此代码Rect.Intersect计算当前矩形与另一个矩形的交集,并替换当前矩形。
列表与LT;&GT;是一个动态数组(随着你添加项目而增长),你可以使用常规数组,但是你需要计算大小,跟踪实际添加的项目数量,并提供计算最大值的方法。