成熟丰满熟妇高潮XXXXX,人妻无码AV中文系列久久兔费 ,国产精品一国产精品,国精品午夜福利视频不卡麻豆

您好,歡迎來到九壹網。
搜索
您的當前位置:首頁判斷兩個矩形是否相交

判斷兩個矩形是否相交

來源:九壹網

??

說矩形相交算法之前,先看下如何判斷兩條線段是否相交

如果有兩條線段 [a, b],[c, d](a, b, c, d 為 X 軸坐標點),有下面兩種不相交的情況:
1)b < c 時,線段 [a, b] 在 [c, d] 的左邊不相交
2)d < a 時,線段 [a, b] 在 [c, d] 的右邊不相交
把上面兩種情況取反就是相交的情況。
相交的線段則可以表示為[min(a, c), min(b, d)](注意:min(b, d) 應大于 min(a, c),否則就是不相交了),即兩條線段中起點大的和終點小的。
寫成程序如果下所示(程序中用的結構體表示是起點和長度):

#define?max(x,?y)?((x)?>?(y)???(x)?:?(y))
#define?min(x,?y)?((x)?<?(y)???(x)?:?(y))
struct?section
{
????int?start;
????int?length;
};
/*?determine?if?the?two?input?section?intersect?with?each?other?*/
static?int?section_intersect(struct?section?sec1,?struct?section?sec2)
{
????/*return?!(sec1.start?>?sec2.start?+?sec2.length
????????????||?sec2.start?>?sec1.start?+?sec1.length);*/
????return?sec1.start?<?sec2.start?+?sec2.length
????????&&?sec2.start?<?sec1.start?+?sec1.length;
}
static?void?section_intersect_test()
{
????struct?section?sec1?=?{5,?5};
????struct?section?sec2?=?{7,?8};
????struct?section?sec3?=?{18,?3};
????printf("sec1?^?sec2?:?%d\n",?section_intersect(sec1,?sec2));
????printf("sec1?^?sec3?:?%d\n",?section_intersect(sec1,?sec3));
????printf("sec2?^?sec3?:?%d\n",?section_intersect(sec2,?sec3));
}
/*?return?the?intersection?of?the?two?input,?if?not?intersect,?return?a?emtpy?section?*/
static?struct?section?section_intersection(struct?section?sec1,?struct?section?sec2)
{
????struct?section?sec?=?{0,?0};
????int?start?=?max(sec1.start,?sec2.start);
????int?end?=?min(sec1.start?+?sec1.length,?sec2.start?+?sec2.length);
????if?(end?>=?start)
????{
????????sec.start?=?start;
????????sec.length?=?end?-?start;
????}
????return?sec;
}
static?void?print_section(const?char?*pcstr,?struct?section?sec)
{
????printf("%s?{%d,?%d}\n",?pcstr,?sec.start,?sec.length);
}
static?void?section_intersection_test()
{
????struct?section?sec1?=?{5,?5};
????struct?section?sec2?=?{7,?8};
????struct?section?sec3?=?{18,?3};
????print_section("sec1?^?sec2?:",?section_intersection(sec1,?sec2));
????print_section("sec1?^?sec3?:",?section_intersection(sec1,?sec3));
????print_section("sec2?^?sec3?:",?section_intersection(sec2,?sec3));
}
static?void?test_driver()
{
????section_intersect_test();
????section_intersection_test();
}

說明:
section_intersect 判斷兩條線段是否相交,如果不相交返回 0。它里面的注釋部分和沒有注釋的部分的意思等同。
section_intersect_test 是對 section_intersect 的簡單測試。
section_intersection 返回兩條線段相交的部分,如果不相交,返回一個空線段(起點為 0,長度為 0)。
section_intersection_test 是對 section_intersection 的簡單測試。

判斷矩形是否相交與之類似,因為矩形是由相互垂直的兩條線段確定的。
代碼如下:

struct?rectangle
{
????int?x;
????int?y;
????int?width;
????int?height;
};
static?int?rectangle_intersect(struct?rectangle?rec1,?struct?rectangle?rec2)
{
????return?rec1.x?<?rec2.x?+?rec2.width
????????&&?rec2.x?<?rec1.x?+?rec1.width
????????&&?rec1.y?<?rec2.y?+?rec2.height
????????&&?rec2.y?<?rec1.y?+?rec1.height;
}
static?struct?rectangle?rectangle_intersection(struct?rectangle?rec1,?struct?rectangle?rec2)
{
????struct?rectangle?rec?=?{0,?0,?0,?0};
????int?x1?=?max(rec1.x,?rec2.x);
????int?x2?=?min(rec1.x?+?rec1.width,?rec2.x?+?rec2.width);
????int?y1?=?max(rec1.y,?rec2.y);
????int?y2?=?min(rec1.y?+?rec1.height,?rec2.y?+?rec2.height);
????if?(x2?>=?x1?&&?y2?>=?y1)
????{
????????rec.x?=?x1;
????????rec.y?=?y1;
????????rec.width?=?x2?-?x1;
????????rec.height?=?y2?-?y1;
????}
????return?rec;
}

說明,以上的代碼都沒有經過嚴格的測試,只為了說明線段矩陣相交算法。

轉載于:https://my.oschina.net/xhan/blog/311305

因篇幅問題不能全部顯示,請點此查看更多更全內容

Copyright ? 2019- 91gzw.com 版權所有 湘ICP備2023023988號-2

違法及侵權請聯(lián)系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市萬商天勤律師事務所王興未律師提供法律服務