English 中文(简体)
3. 试金星-真切交叉区
原标题:Area of rectangle-rectangle intersection
  • 时间:2011-11-04 15:00:18
  •  标签:
  • c++
  • math

Below are 2 rectangles. Given the coordinates of the rectangle vertices - (x1, y1)...(x8, y8), how can the area of the overlapping region (white in the figure below) be caclulated?

Note that:

  1. Coordinates of points might be any
  2. Rectangles may or may not overlap
  3. Assume area is 0 when rectangles don t overlap, or they overlap at point or line.
  4. If one rectangle is inside the other, then calculate area of smaller rectangle.

“entergraph

最佳回答

由于你说,试金石可能无法调和,可能的答复可能只是什么,一个点,一个线段,或一个有3-8方的多角。

The usual way to do this 2d boolean operation is to choose a counterclockwise ordering of the edges, and then evaluate edge segments between critical points (intersections or corners). At each intersection you switch between an edge segment of the first rectangle to an edge of the second, or visa-versa. You always pick the segment to the left of the previous segment.

enter image description here

详细情况有联络处,但基本算法是找到所有交叉点,并命令它们有适当的数据结构。 选择一个交叉点(如果有的话),选择一个线段,脱离这一交叉点。 将其他试金字部分排在选定的起始部分的左边。 总体情况是,我们选择将“十字路口”绿色部分作为参考部分。 另一条直径部分右边是a>>。 将这一部分作为下一个参考部分,并选择一个绿色部分来取代它左边。 这一部分从bc>。 Find部分cd。 下一个部分从d到玉米,因此玉米也出现在交叉点的垂直清单中。 从玉米到a>。

To choose the left side each time, you use the determinate of the coordinates of the direction vectors for the edges that meet. If the determinant for the ordered pair of directed edges is positive, you re going the right way.

既然您有交错,你可以使用<>> surveyor s program

我留给你的一些细节是:

问题回答

另一种做法是:

  1. determine the minimum rectangle( whose sides are parallel to coordinate axes ) that contains both of the given rectangles, lets call that new one a bounding box.
  2. pick a random dot that is in the bounding box and check whether it is in both rectangles or not
  3. repeat step 2 for as long as you want( it depends on the precision you want for your result ), and have two counters, one to keep track of the number of dots inside both of the rectangles, and the other which is the number of repetitions of step 2
  4. the final solution is the area of the bounding box multiplied by the number of dots inside both rectangles and then divided by number of repetitions of step 2, or in a form of a formula:

    intersection_area = bounding_box_area * num_of_dots_inside_both / num_of_repetitions

当然,如果重复次数增加,结果将更加准确。 这样,这种方法被称为蒙特卡洛方法。

You can calculate the intersection points by solving intersection equations for all pairs of sides of the figures: /frac{x - a}{b - a} = /frac{x - c}{d - c}

你以这种方式获得的观点可以落在 para子的一边,但决不能。 您必须检查,你通过解决等同而获得的交叉点是否在数字的两侧。 如果是的话,你可以计算出该数字的侧面长度,并计算交叉点的面积。

I guess my method sounds a bit over-complicated, but this is the first thought that came to my mind.

If you happen to use Qt then the intersection can be computed as QPolygonF intersection. Roughly as so:

QPolygonF p1,p2, intersected;
p1 << QPointF(r1x1,r1y1) << ... << QPointF(r1x4, r1y4);
p2 << QPointF(r2x1,r2y2) << ... << QPointF(r2x4, r2y4);
intersected = p1.intersected(p2);

float area = polyArea(intersected); // see code block below

(r1 = rectangle 1, r2 = rectangle 2, with 4 respective x and y coordinates).

Now compute the area (using the already mentioned Shoelace formula):

inline float polyArea(const QPolygonF& p)
{
    //https://en.wikipedia.org/wiki/Polygon#Area_and_centroid
    const int n = p.size();
    float area = 0.0;
    for (int i=0; i<n; i++)
    {
        area += p[i].x()*p[(i+1)%n].y() - p[(i+1)%n].x()*p[i].y();
    }
    if (area < 0)
        return -0.5*area;
    else
        return 0.5*area;
}

我在这里的法典:公共领域

或许需要公开使用。 利用填充功能产生一种 rec。 确保补休是白人(255,255,255)。 然后使用复印件“To”(To)()功能,你将进入重叠区。 然后,检查每台钢材的价值,如果是白色的,则加1。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?