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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:14:31+00:00 2026-05-29T05:14:31+00:00

I have a vector of points stored in a std::vector instance. I want to

  • 0

I have a vector of points stored in a std::vector instance. I want to calculate the bounding box of these points. I’ve tried with this code:

bool _compare1(ofPoint const &p1, ofPoint const &p2) {
    return p1.x < p2.x && p1.y < p2.y;
}
bool _compare4(ofPoint const &p1, ofPoint const &p2) {
    return p1.x > p2.x && p1.y > p2.y;
}
vector<ofPoint> points;

// ...
if(points.size()>1) {
    ofPoint p_min = *std::min_element(points.begin(), points.end(), &_compare1);
    ofPoint p_max = *std::min_element(points.begin(), points.end(), &_compare4);
}

But this code produces weird results. In reality I’m interested only the first and last points of my bounding box:

1------2
|\     |
| \    |
|  \   |
|   \  |
|    \ |
|     \|
3------4

If my points represent the diagonal line I’m interested only in point 1 and 4.

Are there smart ways to get this with the standard libraries or Boost?


CURRENT SOLUTION:

bool _compare_min_x(ofPoint const &p1, ofPoint const &p2) { return p1.x < p2.x; }
bool _compare_min_y(ofPoint const &p1, ofPoint const &p2) { return p1.y < p2.y; }
 
// ....
 
    if(points.size()>1) {
        min_x = (*std::min_element(points.begin(), points.end(), &_compare_min_x)).x;
        min_y = (*std::min_element(points.begin(), points.end(), &_compare_min_y)).y;

        max_x = (*std::max_element(points.begin(), points.end(), &_compare_min_x)).x;
        max_y = (*std::max_element(points.begin(), points.end(), &_compare_min_y)).y;
    }
  • 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-05-29T05:14:31+00:00Added an answer on May 29, 2026 at 5:14 am

    I think the problem is that your comparison functions are making too strong an assumption about the shape of the bounding box. Consider these two points:

              1
             /
            /
           /
          2
    

    The correct bounding box is

          +---1
          |  /|
          | / |
          |/  |
          2---+
    

    Notice that the bounding box corners aren’t actually points in your vector. Instead, they’re points formed by combining coordinates from different points in the vector. Moreover, if you look at your two comparison functions, you’ll find that given these two points, neither point compares less than or greater than the other point, since each one has one coordinate that is higher than the other and one that is lower than the other.

    To get your bounding box, you should do the following:

    1. Find the point with the min x value.
    2. Find the point with the max x value.
    3. Find the point with the min y value.
    4. Find the point with the max y value.
    5. Combine the x and y from the points with the min x and y value into one corner point.
    6. Combine the x and y from the points with the max x and y value into one corner point.

    You can do this using the new C++11 std::minmax_element algorithm, along with lambdas:

    auto xExtremes = std::minmax_element(v.begin(), v.end(),
                                         [](const ofPoint& lhs, const ofPoint& rhs) {
                                            return lhs.x < rhs.x;
                                         });
    
    auto yExtremes = std::minmax_element(v.begin(), v.end(),
                                         [](const ofPoint& lhs, const ofPoint& rhs) {
                                            return lhs.y < rhs.y;
                                         });
    
    ofPoint upperLeft(xExtremes.first->x, yExtremes.first->y);
    ofPoint lowerRight(xExtremes.second->x, yExtremes.second->y);
    

    Hope this helps!

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

Sidebar

Related Questions

I have a vector that I want to insert into a set . This
So I have this function used to calculate statistics (min/max/std/mean). Now the thing is
I have a list of x,y points stored as a vector of vectors, and
This is related to this question I have a vector of points that will,
I currently have a vector of points vector<Point> corners; where I have previously stored
I have a class class ChartLine{ protected: vector<Point> line; // points connecting the line
I have a vector of integers and I want to convert it to a
I have a vector of beans that holds information I want to display in
I have a std::vector<DOUBLEPOINT> I'm making an application with bezier curves and the results
Here is my issue. I have a std::vector<POINTFLOAT> Which stores verticies. The issue is

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.