Chinaunix首页 | 论坛 | 博客
  • 博客访问: 708173
  • 博文数量: 129
  • 博客积分: 2221
  • 博客等级: 大尉
  • 技术积分: 1620
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-09 11:27
个人简介

do{goodgoodstudy();daydayup();}while(!died)

文章分类

全部博文(129)

文章存档

2012年(26)

2011年(103)

分类:

2011-07-08 19:04:06

Andrew Huang

  在课堂实现的游戏碰撞算法需要使用矩形相交算法,因此按如下思路实现Pure C代码
 http://tonnypc.blog.163.com/blog/static/12258507920112222549172/

 
  1. /*
  2.   Author:Andrew Huang <bluedrum@163.com>
  3.     test rect interest .
  4.  */
  5. #include <stdio.h>

  6. struct rect{
  7.   int x;
  8.   int y;
  9.   int w;
  10.   int h;
  11. };

  12. #define ARY_SIZE(a) (sizeof(a)/sizeof(a[0]))

  13. void test_rect(struct rect * ra,struct rect * rb);
  14. void test_inter(struct rect * ra,struct rect * rb);

  15. int main()
  16. {
  17.   int i;
  18.   struct rect rects[] = {
  19.     {0,0,10,10},{20,20,10,10} , /* 不相交 */
  20.     {20,20,10,10},{0,0,10,10}, /* 不相交 */
  21.     {0,0,10,10},{0,0,10,10} , /* 重合 */
  22.     {30,30,10,10},{20,20,40,40} , /* 包含 */
  23.     {30,30,40,40},{60,40,50,50} , /* 交错 */
  24.     {30,30,40,40},{30,70,40,40} , /* 平行 */
  25.     {30,30,40,40},{30,71,40,40} , /* */
  26.   };

  27.    for(i=0 ; i<ARY_SIZE(rects)/2;i++)
  28.    {
  29.         test_rect(rects+(2*i),rects+(2*i+1));
  30.         test_inter(rects+(2*i),rects+(2*i+1));
  31.        printf("\n");
  32.    }

  33.   return 0;
  34. }

  35. int _abs(int val)
  36. {
  37.   return (val<0) ? -val : val;
  38. }
  39. /* 判断是否相交*/
  40. int test_rect_cross(struct rect * ra,struct rect * rb)
  41. {
  42.   
  43.   int a_cx,a_cy; /* 第一个中心点*/
  44.   int b_cx,b_cy; /* 第二个中心点*/
  45.   
  46.   a_cx = ra->x + (ra->w/2);
  47.   a_cy = ra->y + (ra->h/2);

  48.   b_cx = rb->x + (rb->w/2);
  49.   b_cy = rb->y + (rb->h/2);

  50.   return ( (_abs(a_cx - b_cx) <= (ra->w/2 + rb->w/2))
  51.      && (_abs(a_cy - b_cy) <= (ra->h/2 + rb->h/2)));
  52. }

  53. int _max(int a,int b)
  54. {
  55.   return a>b ? a : b;
  56. }

  57. int _min(int a,int b)
  58. {
  59.   return a<b ? a : b;
  60. }


  61. /* 求两个矩形相交矩形*/
  62. int get_rect_inter(struct rect * ra,struct rect * rb,
  63.  struct rect * result)
  64. {
  65.   int tl_x,tl_y;
  66.   int br_x,br_y;

  67.   tl_x = _max(ra->x,rb->x);
  68.   tl_y = _max(ra->y,rb->y);

  69.   br_x = _min(ra->x+ra->w,rb->x+rb->w);
  70.   br_y = _min(ra->y+ra->h,rb->y+rb->h);

  71.   if( (tl_x > br_x ) || (tl_y > br_y ) )
  72.      return 0 ; /* 没有交集*/
  73.   if(result)
  74.   {
  75.   result->x = tl_x ;
  76.   result->y = tl_y ;
  77.   result->w = br_x - tl_x ;
  78.   result->h = br_y - tl_y ;
  79.   }
  80.   return 1;
  81.   
  82. }


  83. void test_rect(struct rect * ra,struct rect * rb)
  84. {
  85.    printf("rect a= { %d,%d,%d,%d} \n",ra->x,ra->y,ra->w,ra->h);
  86.    printf("rect b= { %d,%d,%d,%d} \n",rb->x,rb->y,rb->w,rb->h);

  87.    printf("\ttest result %d\n",test_rect_cross(ra,rb));
  88. }

  89. void test_inter(struct rect * ra,struct rect * rb)
  90. {

  91.   struct rect result;
  92.   
  93.   if(get_rect_inter(ra,rb,&result))
  94.    {
  95.       printf("\t inter rect = {%d,%d,%d,%d}\n",
  96.          result.x,result.y,result.w,result.h);
  97.    }
  98.  else
  99.    {
  100.        printf("\t no inter rect\n");
  101.    }

  102. }

测试结果如下
 
  1. [blues@localhost test_rect]$ gcc main.c -o test_rect
  2. [blues@localhost test_rect]$ ./test_rect
  3. rect a= { 0,0,10,10}
  4. rect b= { 20,20,10,10}
  5. test result 0
  6. no inter rect

  7. rect a= { 20,20,10,10}
  8. rect b= { 0,0,10,10}
  9. test result 0
  10. no inter rect

  11. rect a= { 0,0,10,10}
  12. rect b= { 0,0,10,10}
  13. test result 1
  14. inter rect = {0,0,10,10}

  15. rect a= { 30,30,10,10}
  16. rect b= { 20,20,40,40}
  17. test result 1
  18. inter rect = {30,30,10,10}

  19. rect a= { 30,30,40,40}
  20. rect b= { 60,40,50,50}
  21. test result 1
  22. inter rect = {60,40,10,30}

  23. rect a= { 30,30,40,40}
  24. rect b= { 30,70,40,40}
  25. test result 1
  26. inter rect = {30,70,40,0}

  27. rect a= { 30,30,40,40}
  28. rect b= { 30,71,40,40}
  29. test result 0
  30. no inter rect

  31. [blues@localhost test_rect]$

 test_rect.zip  



阅读(4353) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~