Andrew Huang
在课堂实现的游戏碰撞算法需要使用矩形相交算法,因此按如下思路实现Pure C代码
http://tonnypc.blog.163.com/blog/static/12258507920112222549172/
- /*
-
Author:Andrew Huang <bluedrum@163.com>
-
test rect interest .
-
*/
-
#include <stdio.h>
-
-
struct rect{
-
int x;
-
int y;
-
int w;
-
int h;
-
};
-
-
#define ARY_SIZE(a) (sizeof(a)/sizeof(a[0]))
-
-
void test_rect(struct rect * ra,struct rect * rb);
-
void test_inter(struct rect * ra,struct rect * rb);
-
-
int main()
-
{
-
int i;
-
struct rect rects[] = {
-
{0,0,10,10},{20,20,10,10} , /* 不相交 */
-
{20,20,10,10},{0,0,10,10}, /* 不相交 */
-
{0,0,10,10},{0,0,10,10} , /* 重合 */
-
{30,30,10,10},{20,20,40,40} , /* 包含 */
-
{30,30,40,40},{60,40,50,50} , /* 交错 */
-
{30,30,40,40},{30,70,40,40} , /* 平行 */
-
{30,30,40,40},{30,71,40,40} , /* */
-
};
-
-
for(i=0 ; i<ARY_SIZE(rects)/2;i++)
-
{
-
test_rect(rects+(2*i),rects+(2*i+1));
-
test_inter(rects+(2*i),rects+(2*i+1));
-
printf("\n");
-
}
-
-
return 0;
-
}
-
-
int _abs(int val)
-
{
-
return (val<0) ? -val : val;
-
}
-
/* 判断是否相交*/
-
int test_rect_cross(struct rect * ra,struct rect * rb)
-
{
-
-
int a_cx,a_cy; /* 第一个中心点*/
-
int b_cx,b_cy; /* 第二个中心点*/
-
-
a_cx = ra->x + (ra->w/2);
-
a_cy = ra->y + (ra->h/2);
-
-
b_cx = rb->x + (rb->w/2);
-
b_cy = rb->y + (rb->h/2);
-
-
return ( (_abs(a_cx - b_cx) <= (ra->w/2 + rb->w/2))
-
&& (_abs(a_cy - b_cy) <= (ra->h/2 + rb->h/2)));
-
}
-
-
int _max(int a,int b)
-
{
-
return a>b ? a : b;
-
}
-
-
int _min(int a,int b)
-
{
-
return a<b ? a : b;
-
}
-
-
-
/* 求两个矩形相交矩形*/
-
int get_rect_inter(struct rect * ra,struct rect * rb,
-
struct rect * result)
-
{
-
int tl_x,tl_y;
-
int br_x,br_y;
-
-
tl_x = _max(ra->x,rb->x);
-
tl_y = _max(ra->y,rb->y);
-
-
br_x = _min(ra->x+ra->w,rb->x+rb->w);
-
br_y = _min(ra->y+ra->h,rb->y+rb->h);
-
-
if( (tl_x > br_x ) || (tl_y > br_y ) )
-
return 0 ; /* 没有交集*/
-
if(result)
- {
-
result->x = tl_x ;
-
result->y = tl_y ;
-
result->w = br_x - tl_x ;
-
result->h = br_y - tl_y ;
- }
-
return 1;
-
-
}
-
-
-
void test_rect(struct rect * ra,struct rect * rb)
-
{
-
printf("rect a= { %d,%d,%d,%d} \n",ra->x,ra->y,ra->w,ra->h);
-
printf("rect b= { %d,%d,%d,%d} \n",rb->x,rb->y,rb->w,rb->h);
-
-
printf("\ttest result %d\n",test_rect_cross(ra,rb));
-
}
-
-
void test_inter(struct rect * ra,struct rect * rb)
-
{
-
-
struct rect result;
-
-
if(get_rect_inter(ra,rb,&result))
-
{
-
printf("\t inter rect = {%d,%d,%d,%d}\n",
-
result.x,result.y,result.w,result.h);
-
}
-
else
-
{
-
printf("\t no inter rect\n");
-
}
-
-
}
测试结果如下
- [blues@localhost test_rect]$ gcc main.c -o test_rect
-
[blues@localhost test_rect]$ ./test_rect
-
rect a= { 0,0,10,10}
-
rect b= { 20,20,10,10}
-
test result 0
-
no inter rect
-
-
rect a= { 20,20,10,10}
-
rect b= { 0,0,10,10}
-
test result 0
-
no inter rect
-
-
rect a= { 0,0,10,10}
-
rect b= { 0,0,10,10}
-
test result 1
-
inter rect = {0,0,10,10}
-
-
rect a= { 30,30,10,10}
-
rect b= { 20,20,40,40}
-
test result 1
-
inter rect = {30,30,10,10}
-
-
rect a= { 30,30,40,40}
-
rect b= { 60,40,50,50}
-
test result 1
-
inter rect = {60,40,10,30}
-
-
rect a= { 30,30,40,40}
-
rect b= { 30,70,40,40}
-
test result 1
-
inter rect = {30,70,40,0}
-
-
rect a= { 30,30,40,40}
-
rect b= { 30,71,40,40}
-
test result 0
-
no inter rect
-
-
[blues@localhost test_rect]$
test_rect.zip
阅读(4353) | 评论(0) | 转发(0) |