1

所以我有一个要排序的顶点向量,而且我不熟悉使用谓词。这就是我目前对顶点进行排序的方法。

void Triangulate::OrderVertices()
{
     std::sort(mVertices.begin(), mVertices.end(), Triangulate::ClockWise());
}

这是我的排序谓词

bool Triangulate::ClockWise::operator()(const CVertex2& pointA, const CVertex2& pointB)
{
    if(pointA.mPoint.x >= 0 && pointB.mPoint.x < 0)
    {
        return true;
    }

    if(pointA.mPoint.x == 0 &&  pointB.mPoint.x == 0)
    {
        return true;
    }

        // Gets the center of my current viewport
    CVertex2 centerCoordinate(CClipper::Instance()->GetMidX(), CClipper::Instance()->GetMidY());


    // compute the cross product of vectors
    int det = (int)((pointA.mPoint.x - centerCoordinate.mPoint.x) * 
                    (pointB.mPoint.y - centerCoordinate.mPoint.y) - 
                    (pointB.mPoint.x - centerCoordinate.mPoint.x) *
                    (pointA.mPoint.y - centerCoordinate.mPoint.y));

    if(det < 0)
    {
        return true;
    }

    if(det > 0)
    {
        return false;
    }

    int det1 = (int)((pointA.mPoint.x - centerCoordinate.mPoint.x) * 
                        (pointA.mPoint.x - centerCoordinate.mPoint.x) + 
                        (pointA.mPoint.y - centerCoordinate.mPoint.y) * 
                        (pointA.mPoint.y - centerCoordinate.mPoint.y));

    int det2 = (int)((pointB.mPoint.x - centerCoordinate.mPoint.x) *
                        (pointB.mPoint.x - centerCoordinate.mPoint.x) +
                        (pointB.mPoint.y - centerCoordinate.mPoint.y) *
                        (pointB.mPoint.y - centerCoordinate.mPoint.y));

    return det1 > det2;
}

我目前在我的三角类中设置了我的谓词,就像这样。

class Triangulate
{
    friend class ClockWise;
public:

    Triangulate(std::vector<CVertex2>& vertices)
        :mVertices(vertices) {}
    ~Triangulate(void);

    class ClockWise
    {
    public:
        bool operator()(const CVertex2& pointA, const CVertex2& pointB);
    };

private:
};

当我收到错误并且我很确定这是因为我没有正确设置它时。有人可以帮忙吗?

4

0 回答 0