点が三角形の辺の上にあるかどうか

point-on-triangle-edge.png
bool CTriangle::PtOnEdge(CVertex* _pt ,int* _onedge){
    //initial edge test
    double tx=0;
    double ty=0;
    tx=(_pt->x-this->initialedge->begin->x)/(this->initialedge->end->x-this->initialedge->begin->x);
    ty=(_pt->y-this->initialedge->begin->y)/(this->initialedge->end->y-this->initialedge->begin->y);
    if(/*tx==ty &&*/ tx>0 && tx<1 && ty>0 && ty<1){*_onedge=this->initialedge->myId;return true;}
    //next edge test
    tx=(_pt->x-this->initialedge->next->begin->x)/(this->initialedge->next->end->x-this->initialedge->next->begin->x);
    ty=(_pt->y-this->initialedge->next->begin->y)/(this->initialedge->next->end->y-this->initialedge->next->begin->y);
    if(/*tx==ty &&*/ tx>0 && tx<1 && ty>0 && ty<1){*_onedge=this->initialedge->next->myId;return true;}
    //prev edge test
    tx=(_pt->x-this->initialedge->prev->begin->x)/(this->initialedge->prev->end->x-this->initialedge->prev->begin->x);
    ty=(_pt->y-this->initialedge->prev->begin->y)/(this->initialedge->prev->end->y-this->initialedge->prev->begin->y);
    if(/*tx==ty &&*/ tx>0 && tx<1 && ty>0 && ty<1){*_onedge=this->initialedge->prev->myId;return true;}
    return false;
}

重心座標による方法

重心座標で点が辺の上にあるかどうか判定するのは難しい。
三角形が重心座標どのぐらいの粒度で1ピクセルつぶれるか判定しずらいから

bool CTriangle::PtOnEdge2(CVertex* _pt,int* _onedge){
    //Barycentric Technique
    // Compute vectors        
    CVertex v0 = *(this->initialedge->prev->begin) - *(this->initialedge->begin);
    CVertex v1 = *(this->initialedge->end) - *(this->initialedge->begin);
    CVertex v2 = *_pt - *(this->initialedge->begin);
 
    // Compute dot products
    double dot00 = v0.CalcInnerProduct(&v0);
    double dot01 = v0.CalcInnerProduct(&v1);
    double dot02 = v0.CalcInnerProduct(&v2);
    double dot11 = v1.CalcInnerProduct(&v1);
    double dot12 = v1.CalcInnerProduct(&v2);
 
    // Compute barycentric coordinates
    double invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
    double u = (dot11 * dot02 - dot01 * dot12) * invDenom;
    double v = (dot00 * dot12 - dot01 * dot02) * invDenom;
 
    // Check if point is in triangle
    if(u==0){*_onedge=this->initialedge->prev->myId;return true;}
    if(v == 0){*_onedge=this->initialedge->myId;return true;}
    if(u + v == 1){*_onedge=this->initialedge->next->myId;return true;}
    return false;
}

barycentric-coordinates delaunaytriangulation triangle

サポートサイト Wikidot.com barycentric-coordinatesdelaunaytriangulationtriangle