谁能看懂下面一个函数的代码啊? 大致说一下,这个函数功能是当画了一根线后,测试鼠标点击是否在相关范围的。
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.   

    abs函数返回传递给它的参数的绝对值,这个知道的吧?
    CStringArray对象就是字符串数组,getat(i)就像普通数组取值那样,如a[i]。只不过返回值是CString格式的然后照着公式看吧骚年
      

  2.   

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

  3.   


    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
    // 容忍误差,结果保留在rule里
    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;
    }
      

  4.   

    int rule = abs(m_points.GetAt(1).x - m_points.GetAt(0).x)
    +abs(m_points.GetAt(0).y - m_points.GetAt(1).y);这儿应该是计算一个容差,相当于点0与点1之间的距离,也相当于用均方根做距离的。
    假设一下measure为0的时候就是x1/y1 = x2/y2.
      

  5.   

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

  6.   

    int measure = x1*y2 - x2*y1我想知道,此时计算出来的measure的值为何意义啊??
      

  7.   

    看下msdn吧,是去绝对值的意思 Calculates the absolute value.The abs function returns the absolute value of its parameter. There is no error return.
      

  8.   

    一个点到这条线的两端的最大距离,再乘以放大倍数dis
      

  9.   

    如果楼主搞过数值计算的话就比较容易理解了
    那个measure变量就是计算偏差的,和我这里的delta m差不多,但是你要推导一下他们的关系式线性的
    不然有可能会导致measure越小而点与直线偏离越大
      

  10.   


    谢谢你的指点啊。只是我对mesure与rule比较的意义何在还是难以理解? rule *= dis有的说是计算面积的(姑且这么认为,但是似乎很牵强,到现在也没有看到看不出纰漏的解释)那么mesure也应该是计算面积的吧? 但是好像看不出来啊??大家感兴趣的,继续讨论。  还有我试过了好像程序运行是正常的,也达到了相应的效果,即当鼠标点击在直线附近时候测试正常。  但是其代码真的好晦涩啊
      

  11.   


    dcmilan 还在吗?  能继续不吝指点下?
      

  12.   

    在code中比较相等的数字,一般只能是 int、long等整型数据,斜率相等怎么判断出来呢?只能比较差别小于某个值就行了。