Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2621359
  • 博文数量: 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-19 10:07:03



点击(此处)折叠或打开

  1. typedef struct _GFPoint
  2. {
  3.     float x;
  4.     float y;
  5. }GFPoint;

  6. typedef struct _GFPolyline
  7. {
  8.     uint32_t count;
  9.     GFPoint * pPoints;
  10. } GFPolyline;


点击(此处)折叠或打开

  1. /**
  2.  * @brief 指定点沿着平行于指定直线的方向移动指定距离后获得的新点
  3.  * @param[in] pStart - 起始方向点
  4.  * @param[in] pEnd - 终止方向点
  5.  * @param[in] pPoint - 指定点 【可以是直线上的点或者不是】
  6.  * @param[in] dDis - 移动的距离
  7.  * @return 返回移动后的点
  8.  */

  9. GFPoint GetPointAlongLine(GFPoint pStart, GFPoint pEnd, GFPoint pPoint, double dDis);

  10. /**
  11.  * @brief 直线起始点各向外扩展指定距离【平行于该直线】
  12.  * @param[in] pGFPolyline - 直线起始距离
  13.  * @param[in] dDis - 移动的距离
  14.  * @return void
  15.  */
  16. void GFPolylineExtendPoints(GFPolyline * pGFPolyline, double dDis);

  17. /**
  18.  * @brief 指定点沿着垂直于指定直线的方向移动指定距离后获得的新点
  19.  * @param[in] pStart - 起始方向点
  20.  * @param[in] pEnd - 终止方向点
  21.  * @param[in] pPoint - 指定点 【可以是直线上的点或者不是】
  22.  * @param[in] dLen - 线宽
  23.  * @param[in] dDis - 移动的距离
  24.  * @return 返回移动后的点
  25.  * @note 调用约定:
  26.  * 1. dLen - 【如果多次调用该接口,而线宽又不改变的情况下,可以外部算一次,作为参数;否则传-1.
  27.  * 2. dDis - 距离的正负代表了扩充点在线的左边还是右边.- 代表在以线的起始点为方向的右边,反之;
  28.  */

  29. GFPoint GetPointVerticalToLine(GFPoint pStart, GFPoint pEnd, GFPoint pPoint, double dLen, double dDis);

  30. /**
  31.  * @brief 直线起始点沿着垂直于该直线的方向外扩展指定距离
  32.  * @param[in] pPolyline - 直线起始距离
  33.  * @param[in] width - 线宽
  34.  * @param[out] pGFPointStartl, pGFPointStartr - 线起点按照指定宽度扩充后的线左右两个点
  35.  * @param[out] pGFPointEndl, pGFPointEndr - 线终点按照指定宽度扩充后的线左右两个点
  36.  * @return void
  37.  */
  38. void GFPolylineExternsPoints(const GFPolyline * pGFPolyline, float width,
  39.                              GFPoint * pGFPointStartr, GFPoint * pGFPointStartl,
  40.                              GFPoint * pGFPointEndr, GFPoint * pGFPointEndl);


  41. GFPoint GetPointAlongLine(GFPoint pStart, GFPoint pEnd, GFPoint pPoint, double dDis)
  42. {
  43.     GFPoint pP;
  44.     double dLen = 0;

  45.     dLen = hypot(pEnd.x - pStart.x, pEnd.y - pStart.y);
  46.     assert(0 != dLen);
  47.     pP.x = pPoint.x + (pEnd.x - pStart.x) * (dDis/dLen);
  48.     pP.y = pPoint.y + (pEnd.y - pStart.y) * (dDis/dLen);

  49.     return pP;
  50. }

  51. void GFPolylineExtendPoints(GFPolyline * pGFPolyline, double dDis)
  52. {
  53.     GFPoint rGFPointEx, lFGPointEx; ///< 延伸后的点

  54.     if (NULL == pGFPolyline || NULL == pGFPolyline->pPoints)
  55.     {
  56.         return;
  57.     }

  58.     lFGPointEx = GetPointAlongLine(pGFPolyline->pPoints[1], pGFPolyline->pPoints[0], pGFPolyline->pPoints[0], dDis);

  59.     pGFPolyline->pPoints[0].x = lFGPointEx.x;
  60.     pGFPolyline->pPoints[0].y = lFGPointEx.y;

  61.     if (2 == pGFPolyline->count)
  62.     {
  63.         rGFPointEx = GetPointAlongLine(pGFPolyline->pPoints[0], pGFPolyline->pPoints[1], pGFPolyline->pPoints[1], dDis);
  64.     }
  65.     else
  66.     {
  67.         rGFPointEx = GetPointAlongLine(pGFPolyline->pPoints[pGFPolyline->count - 2], pGFPolyline->pPoints[pGFPolyline->count - 1], pGFPolyline->pPoints[pGFPolyline->count - 1], dDis);
  68.     }
  69.     pGFPolyline->pPoints[1].x = rGFPointEx.x;
  70.     pGFPolyline->pPoints[1].y = rGFPointEx.y;
  71. }

  72. GFPoint GetPointVerticalToLine(GFPoint pStart, GFPoint pEnd, GFPoint pPoint, double dLen, double dDis)
  73. {
  74.     GFPoint pP;

  75.     if (-1 == dLen)
  76.     {
  77.         dLen = hypot(pEnd.x - pStart.x, pEnd.y - pStart.y);
  78.         assert(0 != dLen);
  79.     }


  80.     pP.x = pPoint.x - (pEnd.y-pStart.y) * (dDis/dLen);
  81.     pP.y = pPoint.y + (pEnd.x-pStart.x) * (dDis/dLen);

  82.     return pP;
  83. }

  84. void GFPolylineExternsPoints(const GFPolyline * pGFPolyline, float width,
  85.                              GFPoint * pGFPointStartr, GFPoint * pGFPointStartl,
  86.                              GFPoint * pGFPointEndr, GFPoint * pGFPointEndl)
  87. {
  88.     double dLen = 0;
  89.     if (NULL == pGFPolyline || pGFPolyline->count < 2)
  90.     {
  91.         return;
  92.     }

  93.     width /= 2;

  94.     dLen = hypot((pGFPolyline->pPoints[1].x - pGFPolyline->pPoints[0].x), (pGFPolyline->pPoints[1].y - pGFPolyline->pPoints[0].y));
  95.     *pGFPointStartr = GetPointVerticalToLine(pGFPolyline->pPoints[0], pGFPolyline->pPoints[1], pGFPolyline->pPoints[0], dLen, width);
  96.     *pGFPointStartl = GetPointVerticalToLine(pGFPolyline->pPoints[0], pGFPolyline->pPoints[1], pGFPolyline->pPoints[0], dLen, -width);

  97.     ///< 考虑到线是折线,而我们只需要起始点进行扩展
  98.     if (2 == pGFPolyline->count)
  99.     {
  100.         *pGFPointEndr = GetPointVerticalToLine(pGFPolyline->pPoints[0], pGFPolyline->pPoints[1], pGFPolyline->pPoints[1], dLen, width);
  101.         *pGFPointEndl = GetPointVerticalToLine(pGFPolyline->pPoints[0], pGFPolyline->pPoints[1], pGFPolyline->pPoints[1], dLen, -width);
  102.     }
  103.     else
  104.     {
  105.         dLen = hypot((pGFPolyline->pPoints[pGFPolyline->count - 1].x - pGFPolyline->pPoints[pGFPolyline->count - 2].x), (pGFPolyline->pPoints[pGFPolyline->count - 1].y - pGFPolyline->pPoints[pGFPolyline->count - 2].y));
  106.         *pGFPointEndr = GetPointVerticalToLine(pGFPolyline->pPoints[pGFPolyline->count - 2], pGFPolyline->pPoints[pGFPolyline->count - 1], pGFPolyline->pPoints[pGFPolyline->count - 1], dLen, width);
  107.         *pGFPointEndl = GetPointVerticalToLine(pGFPolyline->pPoints[pGFPolyline->count - 2], pGFPolyline->pPoints[pGFPolyline->count - 1], pGFPolyline->pPoints[pGFPolyline->count - 1], dLen, -width);
  108.     }
  109. }

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