Hy vọng giúp đc ai đó ^_^!
Code:
	bool IsValidPoint(int x,int y)	// Specified whether point is in drawing area
	{
		return (x >= 0 && y >= 0 && x < m_Width && y < m_Height);
	}
	bool IsValidPoint(CRect SrcRect,int x,int y)
	{
		return (x >= SrcRect.left && y >= SrcRect.top && x <= SrcRect.right && y <= SrcRect.bottom);
	}
	BOOL GetValidLine(int &x1,int &y1,int &x2,int &y2)	// Get visible line in drawing area
	{
		if(IsValidPoint(x1,y1) && IsValidPoint(x2,y2)) return 1;	// Ok
		if((y1 < 0 && y2 < 0) || (y1 > m_Height && y2 > m_Height) || (x1 < 0 && x2 < 0) || (x1 > m_Width && x2 > m_Width)) return -1;	// Two points in same side
		int Size = 0;
		// For x
		if(x1 < 0 || x1 >= m_Width)
		{
			Size = (x1 < 0) ? 0 : m_Width - 1;
			y1 = y1 - ((x1 - Size)*(y1 - y2)/(x1 - x2));
			x1 = Size;
		}
		if(x2 < 0 || x2 >= m_Width)
		{
			Size = (x2 < 0) ? 0 : m_Width - 1;
			y2 = y2 - ((x2 - Size)*(y2 - y1)/(x2 - x1));
			x2 = Size;
		}
		// For y
		if(!(y1 - y2)) return -2;
		if(y1 < 0 || y1 >= m_Height)
		{
			Size = (y1 < 0) ? 0 : m_Height - 1;
			x1 = x1 - ((y1 - Size)*(x1 - x2)/(y1 - y2));
			y1 = Size;
		}
		if(y2 < 0 || y2 >= m_Height)
		{
			Size = (y2 < 0) ? 0 : m_Height - 1;
			x2 = x2 - ((y2 - Size)*(x2 - x1)/(y2 - y1));
			y2 = Size;
		}
		return IsValidPoint(x1,y1) && IsValidPoint(x2,y2);
	}