Pregunta

I have a QCustomPlot Graph with the center (0,0) i want to add an ellipse item around all the data on the graph but there i am only able to set the top left and bottom right positions of the ellipse, this shows almost in the center of the graph but its a bit off because im not able to set the center off the ellipse as center isnt a position its an anchor

does anyone know how i would be able to set the center of my ellipse item to (0,0)?

    QCPItemEllipse *ellipse = new QCPItemEllipse(ui->customPlot);
    ellipse->setAntialiased(true);
    ellipse->topLeft->setCoords(lowestX, highestY);
    ellipse->bottomRight->setCoords(highestX, lowestY);
¿Fue útil?

Solución

Man, the position of ellipse's center is determined by it's top-left and bottom-right points.

E.g. center.x == (topLeft.x + bottomRight.x)/2 and center.y == (topLeft.y + bottomRight.y)/2. That's how ellipses work in math :).

In your code, center will be in (0,0) if (lowestX + hightX) == 0 & (lowestY + hightY) == 0.

Or maybe I'm not getting what are you talking about?

If you want to move center of ellipse in easy way, you can create class derived from QCPItemEllipse with field QCPItemTracer *mCenterTracer and with following stuff in constructor:

mCenterTracer->setStyle(QCPItemTracer::tsNone);

topLeft->setParentAnchor(mCenterTracer->position);
bottomRight->setParentAnchor(mCenterTracer->position);

topLeft->setCoords(-xRadius/2.0, -yRadius/2.0);
bottomRight->setCoords(xRadius/2.0, yRadius/2.0);

So method for moving center (without changing radiuses) will look like:

void FreakingEllipse::moveCenter(double x, double y) {
    mCenterTracer->position->setCoords(x, y);
}

By the way, you can add following if you want your ellipse to have constant radius in pixels, while it's center is still stick to plot coordinates:

topLeft->setType(QCPItemPosition::ptAbsolute);
bottomRight->setType(QCPItemPosition::ptAbsolute);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top