使用Qt绘制抛物线或任何其他多项式
using Qt to paint a parabola or any other polynomial
有没有办法使用Qt库来绘制抛物线或任何其他多项式?我试过使用QPainter,但没有这样的选择。感谢
如果你想使用QPainter,那么你应该使用QImage或QPixmap来显示或保存你的输出。
这是一个简单的代码,展示了如何在Qt小工具应用程序中完成它。
这将是MainWindow.h文件。
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QImage>
#include <QPainter>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
void drawMyFunction(qreal xMin,qreal xMax);
qreal myFunction(qreal x);
QImage * pic;
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
这将是您的MainWindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
drawMyFunction(-3,3);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::drawMyFunction(qreal xMin, qreal xMax)
{
qreal step = (xMax - xMin) / 1000;
QPainterPath painterPath;
QSize picSize(300,300);
for(qreal x = xMin ; x <= xMax ; x = x + step)
{
if(x == xMin)
{
painterPath.moveTo(x,myFunction(x));
}
else
{
painterPath.lineTo(x,myFunction(x));
}
}
// Scaling and centering in the center of image
qreal xScaling = picSize.width() / painterPath.boundingRect().width();
qreal yScaling = picSize.height() / painterPath.boundingRect().height();
qreal scale;
if(xScaling > yScaling)
scale = yScaling;
else
scale = xScaling;
for(int i = 0 ; i < painterPath.elementCount() ; i++ )
{
painterPath.setElementPositionAt(i,painterPath.elementAt(i).x*scale + picSize.width()/2,-painterPath.elementAt(i).y*scale + picSize.height()/2);
}
// Drawing to the image
pic = new QImage(picSize,QImage::Format_RGB32);
pic->fill(Qt::white);
QPainter picPainter(pic);
QPen myPen;
myPen.setColor(Qt::black);
myPen.setWidth(10);
picPainter.drawPath(painterPath);
ui->label->setPixmap(QPixmap::fromImage(*pic)); // label is an added label to the ui
// you can also do this instead of just showing the picture
// pic->save("myImage.bmp");
}
qreal MainWindow::myFunction(qreal x)
{
return x*x; // write any function you want here
}
您尝试过QPainterPath吗?它具有QPainterPath::quadTo和QPainterPath::cubicTo成员函数,这些函数可能会有所帮助。
从多项式函数中计算离散点,并使用QPainter::drawLines绘制图形。
例如,y = x^2
:
float xmin = 0;
float xmax = 2;
float step = 0.1; // experiment with values
QVector<QPointF> points;
float x = xmin;
while(x < xmax)
{
float y = x^x; //f(x)
lines.push_back(QPointF(x,y));
x+= step;
}
painter.drawLines(points);
因为qt坐标中的上角是(0,0)。计算y后,您需要对x和y进行几何平移。
相关文章:
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 为什么 c++ 中的 main() 函数不采用除 int 和 void 之外的任何其他返回类型
- 如何在不进行任何其他修改的情况下"pretty print" C++的XML字符串?
- 如何对 int 变量应用验证,使其仅接受整数数据,并且在任何其他数据的情况下不会出错?
- 如何计算该程序的复杂性?是否有任何其他复杂性较低的解决方案
- 如何使用"std::multimap"或任何其他容器对多个值进行排序?
- 如何在任何其他全局变量或静态变量之前创建/构造类实例
- SSO在STD :: String以外的任何其他标准库容器中使用
- OpenCV将一种颜色转换为任何其他颜色
- 如何使用QT或Linux上的任何其他CPP框架播放WMA音频文件
- 在构造函数以外的任何其他位置访问相机时,我的相机指针返回 null
- "size_t"是否始终是"vector<int>::size_type"或任何其他容器类型的别名?
- 来自没有默认构造函数的超级类的子类,而无需调用任何其他构造函数
- 我们还能使用任何其他方法来访问Android中的C 代码,而不是使用JNI访问C 代码
- 如何在 ubuntu 中单击任何其他歌曲后停止当前播放的歌曲
- 如何检查数组是否等于任何其他数组C++
- 我如何使用OCI或任何其他LIB登录Oracle作为SYS帐户
- 默认的Qt控制台应用程序不会"Hello World"...或做任何其他事情
- -fno elide构造函数是否包含在-O0或任何其他优化级别中