Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用Qt绘制抛物线或任何其他多项式_C++_Qt_User Interface - Fatal编程技术网

C++ 使用Qt绘制抛物线或任何其他多项式

C++ 使用Qt绘制抛物线或任何其他多项式,c++,qt,user-interface,C++,Qt,User Interface,有没有一种方法可以使用Qt库绘制抛物线或任何其他多项式? 我尝试使用QPainter,但没有这样的选项。 谢谢你试过了吗?它的函数和成员函数可能会有所帮助。从多项式函数计算离散点,并用于绘制图形 例如,y=x^2: float xmin = 0; float xmax = 2; float step = 0.1; // experiment with values QVector<QPointF> points; float x = xmin;

有没有一种方法可以使用Qt库绘制抛物线或任何其他多项式? 我尝试使用QPainter,但没有这样的选项。
谢谢

你试过了吗?它的函数和成员函数可能会有所帮助。

从多项式函数计算离散点,并用于绘制图形

例如,
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);
float xmin=0;
浮点xmax=2;
浮动步长=0.1;//试验价值观
矢量点;
浮动x=xmin;
而(x

计算y后,需要对x和y进行几何平移。

如果要使用QPaint,则应使用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
\ifndef主窗口
#定义主窗口
#包括
#包括
#包括
名称空间用户界面{
类主窗口;
}
类主窗口:公共QMainWindow
{
Q_对象
公众:
显式主窗口(QWidget*parent=0);
~main窗口();
私人:
void drawMyFunction(qreal xMin,qreal xMax);
qreal-myFunction(qreal-x);
QImage*pic;
Ui::MainWindow*Ui;
};
#endif//main窗口
这将是您的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
}
#包括“mainwindow.h”
#包括“ui_main window.h”
主窗口::主窗口(QWidget*父窗口):
QMainWindow(父级),
用户界面(新用户界面::主窗口)
{
用户界面->设置用户界面(此);
drawMyFunction(-3,3);
}
MainWindow::~MainWindow()
{
删除用户界面;
}
void主窗口::drawMyFunction(qreal xMin,qreal xMax)
{
qreal步长=(xMax-xMin)/1000;
QPainterPath painterPath;
QSize皮克斯(300300);
对于(qreal x=xMin;x yScaling)
刻度=Y刻度;
其他的
刻度=X刻度;
对于(int i=0;i填充(Qt::白色);
油漆工(pic);
QPen myPen;
设置颜色(Qt::黑色);
myPen.setWidth(10);
picPainter.drawPath(painterPath);
ui->label->setPixmap(QPixmap::fromImage(*pic));//label是添加到ui的标签
//您也可以这样做,而不只是显示图片
//pic->save(“myImage.bmp”);
}
qreal主窗口::myFunction(qreal x)
{
return x*x;//在此处编写所需的任何函数
}

也许首先你应该找到一些符合方程式的点,然后你应该像这里这样用抗锯齿画将它们连接起来?谢谢。“这正是我现在正在做的,但我想要更高效的东西。”卡库什。慢吗?因为我知道步长的大值会产生一条可怕的抛物线,而小值会增加循环和绘图的时间,以获得更好的输出。谢谢,但我不知道如何使用这些函数来绘制一条简单的抛物线parabola@kakush抛物线可以是。