Results 1 to 5 of 5

Thread: Max and min from QVector<QPoint>

  1. #1
    Join Date
    Jan 2012
    Posts
    12
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Max and min from QVector<QPoint>

    Hello,

    I have an array of QVector <QPoint> and I'm trying to find the maximum and minimum of each x and y. (trying to get X max, X min, Y max, Y min). Is there a clean way of going about this? I was looking at QSort but I'm not sure how to seperate the QPoint container into its x and y.

    Thank you in advance for any advice you can offer.

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Max and min from QVector<QPoint>

    See the overloaded qSort function here. You will obviously have to sort twice so you need to define one function comparing the x values and another comparing the y values.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Max and min from QVector<QPoint>

    Here are two other options:
    Qt Code:
    1. #include <limits>
    2.  
    3. QVector<QPoint> array;
    4. array << QPoint(-1, 5) << QPoint(-2, 0) << QPoint(0, 3);
    5.  
    6. Q_ASSERT(array.size() > 0);
    7. int xMin = std::numeric_limits<int>::max(); // everything is <= this
    8. int xMax = std::numeric_limits<int>::min(); // everything is >= this
    9. int yMin = std::numeric_limits<int>::max();
    10. int yMax = std::numeric_limits<int>::min();
    11. foreach (QPoint p, array) {
    12. xMin = qMin(xMin, p.x());
    13. xMax = qMax(xMax, p.x());
    14. yMin = qMin(yMin, p.y());
    15. yMax = qMax(yMax, p.y());
    16. }
    17. qDebug() << xMin << xMax << yMin << yMax;
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. QPolygon poly(array);
    2. QRect brect = poly.boundingRect();
    3.  
    4. qDebug() << brect.left() << brect.right() << brect.top() << brect.bottom(); // note the smallest y is the top of the box not the bottom
    To copy to clipboard, switch view to plain text mode 

  4. The following 2 users say thank you to ChrisW67 for this useful post:

    awpitt13 (16th February 2012), norobro (14th February 2012)

  5. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Max and min from QVector<QPoint>

    @ChrisW67: The QPolygon::boundingRect() solution is a good one. Hadn't thought of that.

    @awpitt13: The qSort code:
    Qt Code:
    1. #include <QtCore>
    2.  
    3. bool xLessThan(const QPoint &p1, const QPoint &p2){ return p1.x()<p2.x();}
    4.  
    5. bool yLessThan(const QPoint &p1, const QPoint &p2){ return p1.y()<p2.y();}
    6.  
    7. int main(){
    8. srand(time(NULL));
    9. QVector<QPoint> vec;
    10. for(int i=0; i<5; i++)
    11. vec.append(QPoint(rand() % 100, rand() % 100));
    12. qDebug() << vec;
    13. qSort(vec.begin(),vec.end(),xLessThan);
    14. qDebug() << "Min X=" << vec.first().x() <<"\tMax X="<< vec.last().x();
    15. qSort(vec.begin(),vec.end(),yLessThan);
    16. qDebug() << "Min Y="<< vec.first().y()<<"\tMax Y=" << vec.last().y();
    17. return 0;
    18. }
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to norobro for this useful post:

    awpitt13 (16th February 2012)

  7. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Max and min from QVector<QPoint>

    I expect that a bounding box is what awpitt13 is ultimately trying to determine.

Similar Threads

  1. Replies: 5
    Last Post: 2nd September 2011, 23:11
  2. Getting widget from Qpoint
    By codeman in forum Qt Programming
    Replies: 1
    Last Post: 22nd June 2010, 15:39
  3. QtScript QPoint
    By bunjee in forum Qt Programming
    Replies: 2
    Last Post: 27th May 2009, 09:33
  4. QPoint Limitation
    By archanasubodh in forum Qt Programming
    Replies: 1
    Last Post: 5th August 2008, 10:22
  5. Confusion with QPoint
    By therealjag in forum Qt Programming
    Replies: 9
    Last Post: 14th February 2006, 17:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.