Pergunta

I really got stuck with drawing "roads" on Pixmap in Qt. I have all coordinates in fractional value which are very close to each other (I've got them from converting longitude/latitude to X/Y coordinates using Mercator's formulas). Qt drawLine function has only integer parameters to draw on a pixmap (cause nobody will draw 2.5 pixels, for example). Moreover, the coordinate starts with top left corner so I need to change it, like this:

Xold = x
Ynew = Ymax - Y

Now I have ordinary X/Y coordinate system, with Y-axis going to top and X-axis going to left.

Here's my code, how I trying to draw lines:

    double minlat = 637800*log(tan(3.14/4+3.14*bounds[1]/360.0))/log(2.71),maxlat=637800*log(tan(3.14/4+3.14*bounds[2]/360.0))/log(2.71);
    std::vector<double> x;
    std::vector<double> y;
    QSize size = ui->label_2->size();
    size=ui->label_2->size();
    QImage pic(size.width(),size.height(),QImage::Format_ARGB32_Premultiplied);
    pic.fill(Qt::transparent);
    QPainter painter(&pic);
    for (unsigned int i=0; i < wayVector.size(); i++){
        for (unsigned int j=0; j<wayVector[i].refs.size(); j++){
            x.push_back(637800*3.14*nodeHash[wayVector[i].refs[j]].lon/180.0);
            y.push_back(637800*log(tan(3.14/4+3.14*nodeHash[wayVector[i].refs[j]].lat/360.0))/log(2.71));

        }
        for (unsigned int j=0; j<wayVector[i].refs.size()-1;j++){
            painter.setPen(Qt::green);
            double x1 = x[j]/(size.width()/(maxlon-minlon));
            double y1 = maxlat*size.height()/(maxlat-minlat)-y[j]*size.height()/(maxlat-minlat);
            double x2 = x[j+1]/(size.width()/(maxlon-minlon));
            double y2 = maxlat*size.height()/(maxlat-minlat)-y[j+1]*size.height()/(maxlat-minlat);
            painter.drawLine(x1,y1,x2,y2);
        }
        x.clear();
        y.clear();
    }

But as soon as I put x1,y1,x2,y2 to drawLine function they converts to integer and everything goes wrong, because all X/Y-coordinates become the same (because of they are very close).

I really don't know how I could draw this lines on a pixmap. Any ideas?

Foi útil?

Solução

There are 5 different drawLine() functions. Use void QPainter::drawLine(const QPointF& p1, const QPointF& p2) or void QPainter::drawLine(const QLineF& line) instead. Those types ending with F use doubles.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top