我有一个以 (0,0) 为中心的 QCustomPlot 图表,我想在图表上的所有数据周围添加一个椭圆项目,但我只能设置椭圆的左上角和右下角位置,这几乎显示在图表的中心,但它有点偏离,因为我无法将中心设置为椭圆的中心,因为中心不是 anchor 的位置
有谁知道如何将椭圆项目的中心设置为 (0,0)?
QCPItemEllipse *ellipse = new QCPItemEllipse(ui->customPlot);
ellipse->setAntialiased(true);
ellipse->topLeft->setCoords(lowestX, highestY);
ellipse->bottomRight->setCoords(highestX, lowestY);
最佳答案
伙计,椭圆中心的位置是由它的左上点和右下点决定的。
例如center.x == (topLeft.x + bottomRight.x)/2
和 center.y == (topLeft.y + bottomRight.y)/2
。这就是省略号在数学中的作用:)。
在您的代码中,如果 (lowestX + hightX) == 0
& (lowestY + hightY) == 0
,中心将位于 (0,0)。
或者我没听懂你在说什么?
如果您想以简单的方式移动椭圆的中心,您可以创建派生自 QCPItemEllipse
的类,其中包含字段 QCPItemTracer *mCenterTracer
并在构造函数中包含以下内容:
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);
因此移动中心(不改变半径)的方法如下所示:
void FreakingEllipse::moveCenter(double x, double y) {
mCenterTracer->position->setCoords(x, y);
}
顺便说一句,如果您希望椭圆具有以像素为单位的恒定半径,同时它的中心仍然坚持绘制坐标,您可以添加以下内容:
topLeft->setType(QCPItemPosition::ptAbsolute);
bottomRight->setType(QCPItemPosition::ptAbsolute);
关于c++ - QCustomPlot QCPItemEllipse 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19632468/