使用我自己的排序顺时针排序2D顶点

时间:2013-08-09 22:54:21

标签: c++

所以我有一个顶点向量,我正在尝试排序,我是新手使用谓词。这就是我目前对顶点进行排序的方法。

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:
};

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

0 个答案:

没有答案