<tfoot id='ge0Tl'></tfoot>
    <legend id='ge0Tl'><style id='ge0Tl'><dir id='ge0Tl'><q id='ge0Tl'></q></dir></style></legend>

    • <bdo id='ge0Tl'></bdo><ul id='ge0Tl'></ul>

      <small id='ge0Tl'></small><noframes id='ge0Tl'>

      <i id='ge0Tl'><tr id='ge0Tl'><dt id='ge0Tl'><q id='ge0Tl'><span id='ge0Tl'><b id='ge0Tl'><form id='ge0Tl'><ins id='ge0Tl'></ins><ul id='ge0Tl'></ul><sub id='ge0Tl'></sub></form><legend id='ge0Tl'></legend><bdo id='ge0Tl'><pre id='ge0Tl'><center id='ge0Tl'></center></pre></bdo></b><th id='ge0Tl'></th></span></q></dt></tr></i><div id='ge0Tl'><tfoot id='ge0Tl'></tfoot><dl id='ge0Tl'><fieldset id='ge0Tl'></fieldset></dl></div>

      快速矩形到矩形交集

      时间:2023-09-27
        <tfoot id='BBpzF'></tfoot>

            1. <small id='BBpzF'></small><noframes id='BBpzF'>

            2. <i id='BBpzF'><tr id='BBpzF'><dt id='BBpzF'><q id='BBpzF'><span id='BBpzF'><b id='BBpzF'><form id='BBpzF'><ins id='BBpzF'></ins><ul id='BBpzF'></ul><sub id='BBpzF'></sub></form><legend id='BBpzF'></legend><bdo id='BBpzF'><pre id='BBpzF'><center id='BBpzF'></center></pre></bdo></b><th id='BBpzF'></th></span></q></dt></tr></i><div id='BBpzF'><tfoot id='BBpzF'></tfoot><dl id='BBpzF'><fieldset id='BBpzF'></fieldset></dl></div>
            3. <legend id='BBpzF'><style id='BBpzF'><dir id='BBpzF'><q id='BBpzF'></q></dir></style></legend>

              • <bdo id='BBpzF'></bdo><ul id='BBpzF'></ul>

                  <tbody id='BBpzF'></tbody>
                本文介绍了快速矩形到矩形交集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                测试两个矩形是否相交的快速方法是什么?

                What's a fast way to test if 2 rectangles are intersecting?

                网上一搜就找到了这个单行代码(WOOT!),但我不明白如何用Javascript编写它,它似乎是用一种古老的C++形式编写的.

                A search on the internet came up with this one-liner (WOOT!), but I don't understand how to write it in Javascript, it seems to be written in an ancient form of C++.

                struct
                {
                    LONG    left;
                    LONG    top;
                    LONG    right;
                    LONG    bottom;
                } RECT; 
                
                bool IntersectRect(const RECT * r1, const RECT * r2)
                {
                    return ! ( r2->left > r1->right
                        || r2->right < r1->left
                        || r2->top > r1->bottom
                        || r2->bottom < r1->top
                        );
                }
                

                推荐答案

                这就是将代码转换为 JavaScript 的方式.请注意,正如评论所建议的那样,您的代码和文章中的代码有错别字.具体 r2->right left 应该是 r2->right <;r1->leftr2->bottom top 应该是 r2->bottom r1->top 使函数起作用.

                This is how that code can be translated to JavaScript. Note that there is a typo in your code, and in that of the article, as the comments have suggested. Specifically r2->right left should be r2->right < r1->left and r2->bottom top should be r2->bottom < r1->top for the function to work.

                function intersectRect(r1, r2) {
                  return !(r2.left > r1.right || 
                           r2.right < r1.left || 
                           r2.top > r1.bottom ||
                           r2.bottom < r1.top);
                }
                

                测试用例:

                var rectA = {
                  left:   10,
                  top:    10,
                  right:  30,
                  bottom: 30
                };
                
                var rectB = {
                  left:   20,
                  top:    20,
                  right:  50,
                  bottom: 50
                };
                
                var rectC = {
                  left:   70,
                  top:    70,
                  right:  90,
                  bottom: 90
                };
                
                intersectRect(rectA, rectB);  // returns true
                intersectRect(rectA, rectC);  // returns false
                

                这篇关于快速矩形到矩形交集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                <tfoot id='BXYvu'></tfoot>
                    <tbody id='BXYvu'></tbody>
                  • <bdo id='BXYvu'></bdo><ul id='BXYvu'></ul>
                    <legend id='BXYvu'><style id='BXYvu'><dir id='BXYvu'><q id='BXYvu'></q></dir></style></legend>
                      <i id='BXYvu'><tr id='BXYvu'><dt id='BXYvu'><q id='BXYvu'><span id='BXYvu'><b id='BXYvu'><form id='BXYvu'><ins id='BXYvu'></ins><ul id='BXYvu'></ul><sub id='BXYvu'></sub></form><legend id='BXYvu'></legend><bdo id='BXYvu'><pre id='BXYvu'><center id='BXYvu'></center></pre></bdo></b><th id='BXYvu'></th></span></q></dt></tr></i><div id='BXYvu'><tfoot id='BXYvu'></tfoot><dl id='BXYvu'><fieldset id='BXYvu'></fieldset></dl></div>

                      <small id='BXYvu'></small><noframes id='BXYvu'>