Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2618437
  • 博文数量: 315
  • 博客积分: 3901
  • 博客等级: 少校
  • 技术积分: 3640
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-08 15:32
个人简介

知乎:https://www.zhihu.com/people/monkey.d.luffy Android高级开发交流群2: 752871516

文章分类

全部博文(315)

文章存档

2019年(2)

2018年(1)

2016年(7)

2015年(32)

2014年(39)

2013年(109)

2012年(81)

2011年(44)

分类: C/C++

2013-11-17 19:24:15


点击(此处)折叠或打开

  1. /**
  2.  * @brief 判断曲线【n条直线】是否在QMesh范围内
  3.  * @param[in] polyline - 待检测的线【包含n条直线】
  4.  * @param[in] offset - 每个点的偏移量,对于已经转换好的点offset.x和offset.y都设置为0;
  5.  * @param[in] recSrc - 矩形框范围
  6.  * @return 在矩形框范围内,返回ture,否则返回false;
  7.  */
  8. bool testInRange(const Polyline polyline, const Point offset, const Rectangle recSrc);

  9. /**
  10.  * @brief 判断直线是否在QMesh范围内
  11.  * @param[in] vector - 某条直线
  12.  * @param[in] recSrc - 矩形框范围
  13.  * @return 直线属于直线,返回true,否则返回false;
  14.  */
  15. bool IsLineInRectangle(RtVector * vector, const Rectangle rect);

  16. /**
  17.  * @brief 起始点绕中心点旋转指定度数后的新点
  18.  * @param[in] rotateCenter - 旋转中心点
  19.  * @param[in] pt - 旋转点
  20.  * @param[in] angel - 旋转角度
  21.  * @return 返回旋转后的点
  22.  * @note
  23.  * 1. angle需要传入角度对应的弧度值,正为顺时针,负为逆时针.
  24.  */
  25. Point PointRotate(const Point rotateCenter, const Point pt, const double angleHude);

  26. /**
  27.  * @brief 矩形绕自身中心点按照指定度数进行旋转
  28.  * @param[in] pRectangle - 旋转矩形
  29.  * @param[in] angel - 旋转角度
  30.  * @return 返回旋转后的点
  31.  * @note
  32.  * 1. angle需要传入角度,正为顺时针,负为逆时针.
  33.  */
  34. void RetancgleRotate(Rectangle * pRectangle, const double angle);


  35. bool IsLineInRectangle(RtVector * vector, const Rectangle rect)
  36. {
  37.     int i = 0;
  38.     bool flag = false;
  39.     int16_t u1 = 0, u2 =1;
  40.     int16_t p[4], q[4];
  41.     int16_t r;

  42.     p[0] = vector->sp.x - vector->ep.x;
  43.     p[1] = vector->ep.x - vector->sp.x;
  44.     p[2] = vector->sp.y - vector->ep.y;
  45.     p[3] = -vector->sp.y + vector->ep.y;
  46.     q[0] = vector->sp.x - rect.x;
  47.     q[1] = rect.x + rect.width - vector->sp.x;
  48.     q[2] = vector->sp.y - rect.y;
  49.     q[3] = rect.y + rect.height - vector->sp.y;

  50.     if (0 == p[0] && ((vector->sp.x < rect.x) || (vector->sp.x > (rect.x + rect.width))))
  51.     {
  52.         return false;
  53.     }
  54.     if (0 == p[2] && ((vector->sp.y < rect.y) || (vector->sp.y > (rect.y + rect.height))))
  55.     {
  56.         return false;
  57.     }

  58.     for (i = 0; i < 4; ++i)
  59.     {
  60.         r = (float)q[i] / (float)p[i];
  61.         if (p[i] < 0)
  62.         {
  63.             u1 = max(u1, r);
  64.             if(u1 > u2)
  65.             {
  66.                 flag = true;
  67.             }
  68.         }
  69.         if (p[i] > 0)
  70.         {
  71.             u2 = min(u2, r);
  72.             if (u1 > u2)
  73.             {
  74.                 flag = true;
  75.             }
  76.         }
  77.         if (0 == p[i] && p[i] < 0)
  78.         {
  79.             flag = true;
  80.         }
  81.     }

  82.     if (flag)
  83.     {
  84.         return false;
  85.     }

  86.     return true;
  87. }

  88. bool testInRange(const Polyline polyline,const Point offset, const Rectangle recSrc)
  89. {
  90.     uint32_t i = 0;
  91.     RtVector rtVect;

  92.     if (polyline.count < 2)
  93.     {
  94.         return false;
  95.     }

  96.     for (i = 0; i < (polyline.count - 1); ++i)
  97.     {
  98.         rtVect.sp.x = polyline.pPoints[i].x + offset.x;
  99.         rtVect.sp.y = polyline.pPoints[i].y + offset.y;

  100.         rtVect.ep.x = polyline.pPoints[i + 1].x + offset.x;
  101.         rtVect.ep.y = polyline.pPoints[i + 1].y + offset.y;

  102.         if (IsLineInRectangle(&rtVect, recSrc))
  103.         {
  104.             return true;
  105.         }
  106.     }

  107.     return false;
  108. }

  109. Point PointRotate(const Point rotateCenter, const Point pt, const double angleHude)
  110. {
  111.     Point newPt;
  112.     double xNew, yNew;

  113.     xNew = (pt.x - rotateCenter.x) * cos(angleHude) +
  114.             (pt.y - rotateCenter.y) * sin(angleHude) + rotateCenter .x;
  115.     yNew = -(pt.x - rotateCenter.x) * sin(angleHude) +
  116.             (pt.y - rotateCenter.y) * cos(angleHude) + rotateCenter.y;
  117.     newPt.x = (int16_t)xNew;
  118.     newPt.y = (int16_t)yNew;

  119.     return newPt;
  120. }

  121. void RetancgleRotate(Rectangle * pRectangle, const double angle)
  122. {
  123.     Point topLeft, downLeft, topRight, downRight;
  124.     Point pt;
  125.     int16_t Xmin0, Ymax0, Xmax0, Ymin0;
  126.     int16_t Xmin1, Ymax1, Xmax1, Ymin1;
  127.     double angleHude;

  128.     topLeft.x = pRectangle->x;
  129.     topLeft.y = pRectangle->y;

  130.     downLeft.x = topLeft.x;
  131.     downLeft.y = topLeft.y - pRectangle->height;

  132.     topRight.x = topLeft.x + pRectangle->width;
  133.     topRight.y = topLeft.y;

  134.     downRight.x = topRight.x;
  135.     downRight.y = downLeft.y;

  136.     pt.x = topLeft.x + pRectangle->width/2;
  137.     pt.y = topLeft.y - pRectangle->height/2;

  138.     angleHude = angle * PI / 180;

  139.     topLeft = PointRotate(pt, topLeft, angleHude);
  140.     downLeft = PointRotate(pt, downLeft, angleHude);
  141.     topRight = PointRotate(pt, topRight, angleHude);
  142.     downRight = PointRotate(pt, downRight, angleHude);

  143.     {
  144.         ///< 对角点进行比较
  145.         if (topLeft.x > downRight.x)
  146.         {
  147.             Xmin0 = downRight.x;
  148.             Xmax0 = topLeft.x;
  149.         }
  150.         else
  151.         {
  152.             Xmin0 = topLeft.x;
  153.             Xmax0 = downRight.x;
  154.         }
  155.         if (topLeft.y > downRight.y)
  156.         {
  157.             Ymin0 = downRight.y;
  158.             Ymax0 = topLeft.y;
  159.         }
  160.         else
  161.         {
  162.             Ymin0 = topLeft.y;
  163.             Ymax0 = downRight.y;
  164.         }

  165.         ///< 对角点进行比较
  166.         if (topRight.x > downLeft.x)
  167.         {
  168.             Xmin1 = downLeft.x;
  169.             Xmax1 = topRight.x;
  170.         }
  171.         else
  172.         {
  173.             Xmin1 = topRight.x;
  174.             Xmax1 = downLeft.x;
  175.         }
  176.         if (topRight.y > downLeft.y)
  177.         {
  178.             Ymin1 = downLeft.y;
  179.             Ymax1 = topRight.y;
  180.         }
  181.         else
  182.         {
  183.             Ymin1 = topRight.y;
  184.             Ymax1 = downLeft.y;
  185.         }

  186.         ///< 找出x、y最小最大值
  187.         Xmin0 = Xmin0 > Xmin1 ? Xmin1 : Xmin0;
  188.         Xmax0 = Xmax0 > Xmax1 ? Xmax0 : Xmax1;
  189.         Ymin0 = Ymin0 > Ymin1 ? Ymin1 : Ymin0;
  190.         Ymax0 = Ymax0 > Ymax1 ? Ymax0 : Ymax1;
  191.     }

  192.     pRectangle->x = Xmin0;
  193.     pRectangle->y = -Ymax0; ///< 屏幕坐标系和计算旋转点的坐标系的y轴是相反的,因此取反
  194.     pRectangle->width = Xmax0 - Xmin0;
  195.     pRectangle->height = Ymax0 - Ymin0;

  196.     return;
  197. }

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