English 中文(简体)
如何确定2D中2英寸之间的区域边界点?
原标题:How to identify the boundary points of area overlapped between 2 rectangles in 2D?

I m looking to return the coordinates of the points bounding the area of overlap between 2 arbitrary rectangles in 2D. Whats the best way to approach this that would take care of all the special cases eg:

  • Rectangles intersecting only on a single vertex ? (the program would have to return the lone vertex)
  • Rectangles which share whole or part of a side ? (the program would have to return the endpoints of the common line segment)

为了增加复杂性,它必须按锁定点/锁定点。 因此,我可以使用综合计算法,在报告之前命令它们,但如果有一个算法可以直接列出各约束点,那么,这种计算是最好的!

Any ideas of what avenues I should be looking at ? I m not looking for code projects etc, only a general idea of a generic algorithm for which I don t have to keep a lot of if "special case" then "do this" kind of code.

EDIT:斜体是任意的,即可能/不能与X/Y轴平行。

问题回答

仅使用一般的聚合物交叉方法。 查阅<条码> 环形环形环形环形器轮值<>。

Alright, we ll try this again...

考虑建立两个联盟,由每个垂直点(黑色)到EFGH(红红)划定的区组成:

enter image description here

The hard part here is coming up with all of the shapes defined by these lines (both the gray lines and the original lines coming from the ABCD and EFGH rectangles.)

一旦发现这些形状,就在交叉点内形成一个相关联的清单,并假设其中每一种形状。

<>Step 1. Translate & 2007,e all so that ABCD has one vertex on 0,0 and its Line are similar/perpendicular to the x and y axes.

<>Step 1A>。 在ABCD中找到了最低的、价值最低的垂直,然后从现场所有其他的外vert中分离。 为了证明这一疏忽是C的,请作假设。 通过从现场的每个垂直点击C,我们将有效地将原产地(0,0)移至C,使C周围的轮换变得容易。

for all shapes in linked list {
    for all vertices in shape {
        vertex -= C;
    }
}

<>Step 1B> 1. 以与C->、B 病媒和x-轴之间的gle角相等的视角,将一切有关原产地,从而使B 土地在轴上:

// see http://en.wikipedia.org/wiki/Atan2
float rotRadians = atan2(B.x, B.y);  // assuming ABCD vertices are labelled clockwise

for all shapes in linked list {
    for all vertices in shape {
        rot(thisVert, rotRadians);
    }
}


// ...

// function declaration
void rot(theVertex, theta) {
    tempX = theVertex.x;
    tempY = theVertex.y;

    theVertex.x = cos(theta) * tempX + sin(theta) * tempY;
    theVertex.y = cos(theta) * tempY - sin(theta) * tempX;

}

如果ABCD的vert被贴上锁定的标签,ABCD的vert就应当照此办理:

A = ABCDx , ABCDy
B = ABCDx ,   0
C = 0     ,   0
D = 0     , ABCDy

(如果它们没有被贴上锁定标签,那么第2步中的“底板”检查就赢得了笔工作,从而确保了<代码>atan2(......)中的阴道,即从最低垂直点起,即为垂直反锁。)

<>Step 2. 现在,我们可以很容易地分析一种形状是否存在于ABCD的试金星体内,例如if(该代码Vert.x >=0 &这一Vert.y >=0 &&这一Vert.x <= ABCDx &&这一Vert.y <= ABCDy)。 跟踪相关的形状清单,并进行检查,以确保每一形状的垂直。 如果一种形状的垂直点不在于ABCD,那么这种形状不属于ABCD/EFGH交汇点。 标记不作为交汇点的一部分,ski到下层。

<>Step 3. 打破从第1B步的轮换,从第1A步中删除译文。

Repeat steps 1-3 with EFGH than ABCD, and You will have contributed cross set.


如果两套之间唯一的交叉点是线,那么上述两套将不再作为交叉点。 如果交叉点=NUL,则检查交叉线路。

如果交叉点仍为NUL,则检查交叉点。

这可能确实是粗略的,但:

object rectangle { 

     pos  { x, y }              // top-left position
     size { height, width }     // rectangle-size
}


collision::check (rectangle rect) {

     // collision-detection logic 

     collision->order_coords(coords);   // order-coords clockwise;
     return collision_result_object;    // return collided vertices, ordered clockwise, or 0 if rect hit nothing
}

collision::order_rects (rectangle *rect, opt angle)  {

     return clockwise_rects; // returns rectangles ordered clockwise
}

collision::order_coords (coordinate *coord, opt angle) {

     return ordered_coords; // recieves coordinates and returns ordered clockwise
}

rectangle rects; // bunch of rectangles

ordered_rects = collision->order_rects (rects); // order rects starting at 12PM

loop {    

       foreach ordered_rects as i {

           if (collision->check(i)) {

           array_of_collision_result_objects[i] = collision->check(i);      // start checking rects starting at 12PM, if collision found, return ordered vertexes
           }
       }

}

找出各阶层的交叉点。 其结果包括其中一些和一些初步的vert。 找到他们只是检查每一点,这在两点都存在。 删除不必要的要点(如果一行有三个或三个以上)。 其结果很复杂,没有严格意义上的,因此(如果至少有3个点)从某些内点到点点点点,并享受到结果。

我提出了一个合理的方法,涵盖所有可能的案件:

我们需要的是:

步骤1:

for each side Si of R1
    for each side Sj of R2
           Check if Si and Sj intersect. If they do, push the point in results array
           (This also has to take care of the case in case Si and Sj overlap, which is 
           basically checking if they  have the same equation or not - if so, push in 
           the points of overlap. This also takes care of the case where a vertex of 
           R2 lies on Si).
    next
next

步骤2:

for each vertex Vi of R1
   Check if Vi lies inside R2, If so, push it in the results array.
next

步骤3

for each vertex Vi of R2
   Check if Vi lies inside R1, If so, push it in the results array.
next

现在,确定成果阵列和回返

For step 2 & 3 (how to find if a point lies inside a rectangle) - I d use this excellent article (the last algorithm stated there).





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

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->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签