0

I've got a convex hull program going but the only issue left is that it only ever captures up to 4 points on the diagram. In a sense, if I were to make a 5th point, it would only replace one of the original 4 to keep the same 4 side limit. Wondering where I messed up, as I used the wikibooks example as my base.

https://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain

NOTE: The sorting does work as I've debugged through that part and it does capture all points made, the issue lies in probably the upper/lower hull part of the code, but I am unsure what is wrong/missing. Would simply like another set of eyes to help.

double cross(const QPointF &O, const QPointF &A, const QPointF &B)
    {
        return (long)(A.x() - O.x()) * (B.y() - O.y())
                - (long)(A.y() - O.y()) * (B.x() - O.x());
    }

    void culling(QPainter * painter)
    {
        int n = vecPoints.size();
        int k = 0;
        std::vector<QPointF> H(2*n);

        // Sort points lexicographically
        std::sort(vecPoints.begin(),vecPoints.end(), lexicalSort());

        // Build lower hull
        for (int i = 0; i < n; ++i)
        {
            while (k >= 2 && cross(H[k-2], H[k-1], vecPoints[i]) <= 0)
                k--;
            H[k++] = vecPoints[i];
        }

        // Build upper hull
        for (int i = n-2, t = k+1; i >= 0; i--)
        {
            while (k >= t && cross(H[k-2], H[k-1], vecPoints[i]) <= 0) k--;
            H[k++] = vecPoints[i];
        }

        H.resize(k);

        //draw the rubber band around outter points
        QPointF tmp;
        for (std::vector<QPointF>::const_iterator it = H.begin(); it != H.end(); it++)
        {
            if (it == H.begin())
                tmp = *it;
            else
            {
                painter->drawLine(tmp, *it);
                tmp = *it;
            }
        }
    }

0 Answers0