谁能看懂下面一个函数的代码啊? 大致说一下,这个函数功能是当画了一根线后,测试鼠标点击是否在相关范围的。
CArray<CPoint, CPoint> m_points;
#define TKPT_SIZE 4BOOL CDrawLine::HitTest(const CPoint &point){
int x1 = point.x - m_points.GetAt(0).x;
int x2 = point.x - m_points.GetAt(1).x;
int y1 = point.y - m_points.GetAt(0).y;
int y2 = point.y - m_points.GetAt(1).y;
if(x1==0)
x1=1;
if(y1==0)
y1=1;
if(x2==0)
x2=1;
if(y2==0)
y2=1;
//y1/x1=y2/x2 => x1*y2=x2*y1
int measure = x1*y2 - x2*y1;//tolerable distance
int rule = abs(m_points.GetAt(1).x - m_points.GetAt(0).x)
+abs(m_points.GetAt(0).y - m_points.GetAt(1).y);
int dis=TKPT_SIZE*2;
if(dis<m_penWidth)
dis=m_penWidth;
rule *= dis;if(measure < rule && measure > -rule){
//between the two points
if(x1 * x2 < 0)
return TRUE;
else
return FALSE;
}
else
return FALSE;
}
 //y1/x1=y2/x2 => x1*y2=x2*y1
这个是提示,我估计。
//y1/x1=y2/x2  表示:如果该等式成立,则表示在同一直线上。即斜率相等
 int measure = x1*y2 - x2*y1;
//tolerable distance
int rule = abs(m_points.GetAt(1).x - m_points.GetAt(0).x)
+abs(m_points.GetAt(0).y - m_points.GetAt(1).y);这两句实在不知道是什么意思??

解决方案 »

  1.   

    简单说下思路吧:
    直线斜率算法:
    2点确定一条直线,既然要判断是否在直线上,那你直线的2点肯定是存在了(X1,Y1),(X2,Y2)
    根据直线公式:
    y = kx + b;k和b就确定了之后你的point输入,point和(X1,Y1)形成直线求出k1和b1判断k和k1,b和b1是否相等,相等就是一条直线了
      

  2.   

    y = kx + b;
    斜率k相等,两线就平行了
    b相等,两线重合