Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8114535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:10:35+00:00 2026-06-06T03:10:35+00:00

I’m trying to implement the Graham Scan in C++ but it doesn’t work and

  • 0

I’m trying to implement the Graham Scan in C++ but it doesn’t work and I can’t find why. Any lead would be appreciated. After some tries it seems that I always have m_M = 2 and the 2 points are the highest-y points, if that help.

Cross product to know if it’s a right turn or a left turn.

qreal Interpolation::ccw(QPointF pt1, QPointF pt2, QPointF pt3)
{
    return (pt2.x()-pt1.x())*(pt3.y()-pt1.y()) - (pt2.y()-pt1.y())*(pt3.x()-pt1.x());
}

Dot product divided by the norm to have the cos because sorting the angle is the same as sorting the cos in [0, Pi].

qreal Interpolation::dp(QPointF pt1, QPointF pt2)
{
    return (pt2.x()-pt1.x())/qSqrt((pt2.x()-pt1.x())*(pt2.x()-pt1.x()) + (pt2.y()-pt1.y())*(pt2.y()-pt1.y()));
}

The main function:

void Interpolation::ConvexHull()
{
    QPointF points[m_N+1]; // My number of points
    QPointF pt_temp(m_pt[0]);
    qreal angle_temp(0);
    int k_temp(0);

Fill the array points with points[1] being the lower-y point:

    for (int i(1); i < m_N; ++i)
    {
        if (m_pt[i].y() < pt_temp.y())
        {
            points[i+1] = pt_temp;
            pt_temp = m_pt[i];
        }
        else
        {
            points[i+1] = m_pt[i];
        }
    }
    points[1] = pt_temp;

Sorting the points array by angle and doing points[m_N] = points[0]

    for (int i(2); i <= m_N; ++i)
    {
        pt_temp = points[i];
        angle_temp = dp(points[1], pt_temp);
        k_temp = i;
        for (int j(1); j <= m_N-i; ++j)
        {
            if (dp(points[1], points[i+j]) < angle_temp)
            {
                pt_temp = points[i+j];
                angle_temp = dp(points[1], points[i+j]);
                k_temp = i+j;
            }
        }
        points[k_temp] = points[i];
        points[i] = pt_temp;
    }
    points[0] = points[m_N];

Executing the Graham scan

    m_M = 1; // Number of points on the convex hull.

    for (int i(2); i <= m_N; ++i)
    {
        while (ccw(points[m_M-1], points[m_M], points[i]) <= 0)
        {
            if (m_M > 1)
            {
                m_M -= 1;
            }
            else if (i == m_N)
            {
                break;
            }
            else
            {
                i += 1;
            }
        }
        m_M += 1;
        pt_temp = points[m_M];
        points[m_M] = points[i];
        points[i] = points[m_M];
    }

Saving the points to m_convexHull which should be the list of the points on the hull with ConvexHull[m_M]=[ConvexHull[0]

    for (int i(0); i < m_M; ++i)
    {
        m_convexHull.push_back(points[i+1]);
    }
    m_convexHull.push_back(points[1]);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:10:37+00:00Added an answer on June 6, 2026 at 3:10 am

    I found the problem. It lies with the sentence:

    Dot product divided by the norm to have the cos because sorting the angle is the same as sorting the cos in [0, Pi].

    The lower the angle, the higher the cos, so I just had to change this line of code:

                if (dp(points[1], points[i+j]) < angle_temp)
    

    to:

                if (dp(points[1], points[i+j]) > angle_temp)
    

    and now it works perfectly!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.