Qt Custom Scatterplot I have  created a custom scatterplot in Qt and am wondering if there is a more efficient way to do it. Here is a sketch of the problem:



The layout of the plotting area consists of a rectangular structure starting at [0,0] in the top left corner and expands by 250 pixels to the bottom right corner [250,250]. So the y-coordinate grows downwards and the x-coordinate to the right. 

The task consists of mapping two vectors (A[i],B[i]) holding numerical data onto this rectangular structure such that the lowest x,y coordinates are in the bottom left corner and the highest x,y coordinates are in the top right corner.

To solve this problem I choose the following approach (pseudo code):


calculate the min and max for (A and B);
calculate the spread (max - min) for (A and B);
transform A and B by:

 foreach (i in A) {
     (i - min(A) / spread(a))* 250
 } 
 foreach(j in B) {
     (250 - ((j - min(B) / spread(B))*250
 } 



and the implementation:

.h file

#include &lt;QDialog&gt;
#include &lt;QtGui&gt;
#include &lt;QtCore&gt;
#include &lt;QGraphicsScene&gt;
#include &lt;cstdlib&gt;
#include &lt;cmath&gt;
#include &lt;QVector&gt;
#include &lt;QRectF&gt;

namespace Ui {
class paintEr;
}

class paintEr : public QDialog
{
    Q_OBJECT

public:
    explicit paintEr(QWidget *parent = 0);
    ~paintEr();
private slots:
    void addPoint();

private:
    Ui::paintEr *ui;
    QGraphicsScene *scatter;

protected:
    void paintEvent(QPaintEvent *event);


};


.cpp

#include "painter.h"
#include "ui_painter.h"

paintEr::paintEr(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::paintEr)
{
    ui-&gt;setupUi(this);
}

void paintEr::paintEvent(QPaintEvent *event){

    scatter = new QGraphicsScene(this);
    ui-&gt;graphicsView-&gt;setSceneRect(0,0,250,250);
    ui-&gt;graphicsView-&gt;setScene(scatter);

}
void paintEr::addPoint(){
    QPen greenPen(Qt::green);   
    QBrush greenBrush(Qt::green);

    QVector&lt;double&gt; a,b,c,d;
    a &lt;&lt; 2 &lt;&lt; 5 &lt;&lt; 7;
    b &lt;&lt; 22 &lt;&lt; 34 &lt;&lt; 75;
    double min_a = *std::min_element(a.begin(), a.end());
    double max_a = *std::max_element(a.begin(), a.end());
    double min_b = *std::min_element(b.begin(), b.end());
    double max_b = *std::max_element(b.begin(), b.end());
    double spread_a = max_a - min_a;
    double spread_b = max_b - min_b;

    for(int i = 0; i &lt; a.length(); ++i){
        c.push_back(((a[i]-min_a)/spread_a)*250);
        d.push_back(250-((b[i]-min_b)/spread_b)*250);        
        scatter-&gt;addEllipse(c[i], d[i], 5, 5, greenPen, greenBrush);
    }
}
paintEr::~paintEr()
{
    delete ui;
}


main.cpp

#include "painter.h"
#include &lt;QApplication&gt;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    paintEr w;
    w.show();

    return a.exec();
}
You should separate the actual data from the logic. Make your function capable of working on any QVectorq of QPairs (or pair of QVectors but that's not as normalized).

Variable names a,b,c,d don't make sense. Especially since a and b really ought to be input parameters.

You should be able to get both the min and max with one pass over the list. You'll probably have to write a custom mapreduce-style function.

For the circles in the QGraphicsScene: Are they supposed to have their center at the point in question, or the topleft of their bounding rect?