线段是否与矩形相交

// 判断点在有向直线的左侧还是右侧.
// 返回值:-1: 点在线段左侧; 0: 点在线段上; 1: 点在线段右侧
int PointAtLineLeftRight(CPoint ptStart, CPoint ptEnd, CPoint ptTest)
{
 ptStart.x -= ptTest.x;
 ptStart.y -= ptTest.y;
 ptEnd.x -= ptTest.x;
 ptEnd.y -= ptTest.y;

 int nRet = ptStart.x * ptEnd.y - ptStart.y * ptEnd.x;
 if (nRet == 0)
  return 0;
 else if (nRet > 0)
  return 1;
 else if (nRet < 0)
  return -1;

 return 0;
}

// 判断两条线段是否相交
BOOL IsTwoLineIntersect(CPoint ptLine1Start, CPoint ptLine1End, CPoint ptLine2Start, CPoint ptLine2End)
{
 int nLine1Start = PointAtLineLeftRight(ptLine2Start, ptLine2End, ptLine1Start);
 int nLine1End = PointAtLineLeftRight(ptLine2Start, ptLine2End, ptLine1End);
 if (nLine1Start * nLine1End > 0)
  return FALSE;

 int nLine2Start = PointAtLineLeftRight(ptLine1Start, ptLine1End, ptLine2Start);
 int nLine2End = PointAtLineLeftRight(ptLine1Start, ptLine1End, ptLine2End);

 if (nLine2Start * nLine2End > 0)
  return FALSE;

 return TRUE;
}

// 判断线段是否与矩形相交
BOOL IsLineIntersectRect(CPoint ptStart, CPoint ptEnd, CRect rect)
{
 // Two point both are in rect
 if (rect.PtInRect(ptStart) && rect.PtInRect(ptEnd))
  return TRUE;

 // One point is in rect, another not.
 if (rect.PtInRect(ptStart) && !rect.PtInRect(ptEnd))
  return TRUE;
 if (!rect.PtInRect(ptStart) && rect.PtInRect(ptEnd))
  return TRUE;

 // Two point both aren't in rect
 if (IsTwoLineIntersect(ptStart, ptEnd, CPoint(rect.left, rect.top), CPoint(rect.left, rect.bottom)))
  return TRUE;
 if (IsTwoLineIntersect(ptStart, ptEnd, CPoint(rect.left, rect.bottom), CPoint(rect.right, rect.bottom)))
  return TRUE;
 if (IsTwoLineIntersect(ptStart, ptEnd, CPoint(rect.right, rect.bottom), CPoint(rect.right, rect.top)))
  return TRUE;
 if (IsTwoLineIntersect(ptStart, ptEnd, CPoint(rect.left, rect.top), CPoint(rect.right, rect.top)))
  return TRUE;

 return FALSE;
}

// 测试代码:
const int RECTSIZE = 20;
void CMainFrame::OnPaint()
{
 CPaintDC dc(this); // device context for painting
 
 // TODO: Add your message handler code here
 
 CRect rcClient;
 GetClientRect(rcClient);
 CDC memDC;
 memDC.CreateCompatibleDC(&dc);
 CBitmap memBmp;
 memBmp.CreateCompatibleBitmap(&dc, rcClient.Width(), rcClient.Height());
 CBitmap *pOldBmp = memDC.SelectObject(&memBmp);
 
 memDC.FillSolidRect(rcClient, RGB(255, 255, 255));
 
 // Draw rect
 CPoint pt;
 GetCursorPos(&pt);
 ScreenToClient(&pt);

 CRect rect;
 rect.left = pt.x - RECTSIZE / 2;
 rect.top = pt.y - RECTSIZE / 2;
 rect.right = rect.left + RECTSIZE;
 rect.bottom = rect.top + RECTSIZE;

 memDC.Draw3dRect(rect, RGB(0, 0, 0), RGB(0, 0, 0));

 // Draw line
 CPoint pt1(300, 200);
 CPoint pt2(500, 350);
 if (IsLineIntersectRect(pt1, pt2, rect))
  DrawArrowLine(memDC.m_hDC, &pt1, &pt2, 30, xAngle, RGB(128, 0, 255));
 else
  DrawArrowLine(memDC.m_hDC, &pt1, &pt2, 30, xAngle, RGB(0, 0, 0));

 dc.BitBlt(0, 0, rcClient.Width(), rcClient.Height(), &memDC, 0, 0, SRCCOPY);
 memDC.SelectObject(pOldBmp);


 // Do not call CWnd::OnPaint() for painting messages
}

void CMainFrame::OnMouseMove(UINT nFlags, CPoint point)
{
 // TODO: Add your message handler code here and/or call default
 Invalidate();

 CWnd::OnMouseMove(nFlags, point);
}

BOOL CMainFrame::OnEraseBkgnd(CDC* pDC)
{
 // TODO: Add your message handler code here and/or call default

 return TRUE;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值