zoukankan      html  css  js  c++  java
  • 判断点在凸多边形内

    // cocos2dx

    int cross = 0;

    CCPoint pos = getPosition();
    CCPoint point_origin = CCPointMake(pos.x + m_rectTouch.origin.x, pos.y + m_rectTouch.origin.y);

    int valid_pointnum = m_curShapePoint->GetValidPointNum();

    for (int i = 0; i < valid_pointnum; ++i)
    {
      CCPoint p1 = ccpAdd(ccp(m_curShapePoint->m_fShapePosX[i], m_curShapePoint->m_fShapePosY[i]), point_origin);
      CCPoint p2 = ccpAdd(ccp(m_curShapePoint->m_fShapePosX[(i + 1) % valid_pointnum], m_curShapePoint->m_fShapePosY[(i + 1) % valid_pointnum]), point_origin);

      if (p1.y == p2.y)
        continue;

      if (point.y < MIN(p1.y, p2.y))
        continue;

      if (point.y >= MAX(p1.y, p2.y))
        continue;

      float x = (float)(point.y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y) + p1.x;

      if (x > point.x)
        cross++;
    }

    return (cross % 2 == 1);

  • 相关阅读:
    指针、字符串、数组操作
    字符串转换为数字(str2int)
    新的,开始。
    Hello, World.
    Go语言趣学指南lesson1
    hdoj2058
    poj2378
    hdoj1233
    poj2398
    hdoj1392
  • 原文地址:https://www.cnblogs.com/shadow21/p/3262375.html
Copyright © 2011-2022 走看看