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 9147215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:56:02+00:00 2026-06-17T10:56:02+00:00

I am attempting to sort three points for a triangle in order to add

  • 0

I am attempting to sort three points for a triangle in order to add it successfully into a physics engine. All of the triangles whose position is smaller than 0.0 on the x axis show up correctly. However, after that point the first element (v[0]) is always set to {0.0, 0.0} and the rest seem okay. Here is my main function that does the sorting and adding.

void Core::addPoly(float x1, float y1, 
                   float x2, float y2, 
                   float x3, float y3) {

    std::vector<PVector> v(3);
    v.push_back(PVector(x1, y1));
    v.push_back(PVector(x2, y2));
    v.push_back(PVector(x3, y3));

    PVector center((x1+x2+x3)/3, (y1+y2+y3)/3);

    std::sort(v.begin(), v.end(), [center](PVector b, PVector a) { 
        if (a.x >= 0 && b.x < 0)
            return true;
        if (a.x == 0 && b.x == 0)
            return a.y > b.y;

        // compute the cross product of vectors (center -> a) x (center -> b)
        float det = (a.x-center.x) * (b.y-center.y) - (b.x - center.x) * (a.y - center.y);
        if (det < 0)
            return true;
        if (det > 0)
            return false;

        // points a and b are on the same line from the center
        // check which point is closer to the center
        float d1 = (a.x-center.x) * (a.x-center.x) + (a.y-center.y) * (a.y-center.y);
        float d2 = (b.x-center.x) * (b.x-center.x) + (b.y-center.y) * (b.y-center.y);
        return d1 > d2;
    });

    emap->polys.push_back(Polygon(v[0], v[1], v[2]));
}

I am using the sorting function provided here. It originally had the first element of all triangles pointed to center (however I don’t believe this is correct behaviour)- I switched a and b in the lambda declaration and now it only shows up after 0.0 on the x axis.

If we have 3 triangles passed in (Imagine | to be the 0.0 and ___ to be 0.0 on a plane)

^     |
    ^ |   ^
______|_____________

The 0’th vertice of the third triangle will actually make it like this:

^     |
    ^ |  ___/|
______|_/______________

(This is supposed to be a triangle)

However, 0.0 was never passed to addPoly.

  • 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-17T10:56:03+00:00Added an answer on June 17, 2026 at 10:56 am

    This line:

    std::vector<PVector> v(3);
    

    Causes your vector to get initialized with three <0.0,0.0> values. Changing it to

    std::vector<PVector> v;
    

    should fix your issue.

    The code seems to work like this:

    struct PVector 
    { 
        float x; 
        float y; 
        PVector(float x, float y) : x(x),y(y){}
        PVector() : x(0.0f), y(0.0f) {}
    };
    
    
    static void addPoly(float x1, float y1, 
                       float x2, float y2, 
                       float x3, float y3) {
    
        std::vector<PVector> v;
        v.push_back(PVector(x1, y1));
        v.push_back(PVector(x2, y2));
        v.push_back(PVector(x3, y3));
    
        PVector center((x1+x2+x3)/3, (y1+y2+y3)/3);
    
        std::sort(v.begin(), v.end(), [center](PVector b, PVector a) -> bool { 
            if (a.x >= 0 && b.x < 0)
                return true;
            if (a.x == 0 && b.x == 0)
                return a.y > b.y;
    
            // compute the cross product of vectors (center -> a) x (center -> b)
            float det = (a.x-center.x) * (b.y-center.y) - (b.x - center.x) * (a.y - center.y);
            if (det < 0)
                return true;
            if (det > 0)
                return false;
    
            // points a and b are on the same line from the center
            // check which point is closer to the center
            float d1 = (a.x-center.x) * (a.x-center.x) + (a.y-center.y) * (a.y-center.y);
            float d2 = (b.x-center.x) * (b.x-center.x) + (b.y-center.y) * (b.y-center.y);
            return d1 > d2;
        });
    
        //emap->polys.push_back(Polygon(v[0], v[1], v[2]));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have sort of a tricky problem I'm attempting to solve. First of all,
I currently have a deployed app (fortworth.herokuapp.com) that I am attempting to sort movies
I'm attempting to do a simple bubble sort code to get familiar with list/string
Attempting to use XStream's JavaBeanConverter and running into an issue. Most likely I'm missng
Possible Duplicate: C++ template, linking error I am attempting to implement a selection sort,
So I am pulling a list of links and I am attempting to sort
I'm attempting to implement a jQuery Lightbox plugin into a portfolio site build on
I'm attempting to sort a ListView using C#, but whenever I click the sort
I'm currently attempting to use the built-in quicksort provided by C in order to
In this bit of code, i am attempting to split a string into Characters

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.