본문 바로가기
프로그래머의 길/C & C++

라인 선택 함수

by 제이콥케이 2015. 5. 27.
반응형

// 라인 선택 모듈
int CTRDC2DMap::CheckLineSelect(CPoint mousePt)
{
 int nRes = -1;
 int lineWidth = 5;

 for (int i=0; i<ROUTE_MAX_COUNT; i++)
 {
  for (int k=0; k<(int)m_stRouteData[i].m_vRouteData.size()-1; k++)
  {
   CPoint startP, endP;
   m_pMapHandler->TransformGeoToScreen(&m_stRouteData[i].m_vRouteData[k], &startP);
   m_pMapHandler->TransformGeoToScreen(&m_stRouteData[i].m_vRouteData[k+1], &endP);
   
   int dx = endP.x - startP.x;
   int dy = endP.y - startP.y;
   int dx1 = mousePt.x - startP.x;
   int dy1 = mousePt.y - startP.y;
   int a = 0, b = 0, c = 0;
   if (abs(dx) > 0 && abs(dy) < abs(dx))
   {
    if (dx * dx1 >= 0 && abs(dx1) <= abs(dx))
    {
     a = (dy * dx1);
     b = (dx * dy1);
     c = abs(dx * lineWidth);
     if (abs(a - b) <= c)
     {      
      nRes = i;
      break;
     }
    }
   }
   else if (abs(dy) > 0)
   {
    if (dy * dy1 >= 0 && (abs(dy1) <= abs(dy)))
    {
     a = (dx * dy1);
     b = (dy * dx1);
     c = abs(dy * lineWidth);
     if (abs(a - b) <= c)
     {
      if (abs(a - b) <= c)
      {
       nRes = i;
       break;
      }
     }
    }
   }
  }

  if (nRes != -1)
  {
   break;
  }
 } 

 return nRes;
}

반응형