qwt直角坐标 画sing(x)/x

cmath的常见函数:qPow()求平方,log()对数10为底
角度转弧度:x=(angel/180)*M_PI
图示:qwt直角坐标 画sing(x)/x_第1张图片
头文件:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include
#include
#include 


QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

主函数:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 
#include 
#include 
#include 
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    /*清屏重绘*/
    ui->qwtPlot->detachItems();
    ui->qwtPlot->replot();
    /*setAxisScale四个参数的含义分别是:坐标轴->坐标轴最小值->坐标轴最大值->步进*/
    ui->qwtPlot->setAxisScale(QwtPlot::xBottom,-90,90,15);
    ui->qwtPlot->setAxisScale(QwtPlot::yLeft,0,60,1);
    ui->qwtPlot->setAxisTitle(QwtPlot::xBottom,"x(data)");
    ui->qwtPlot->setAxisTitle(QwtPlot::yLeft,"y(data)");
    QwtPlotCurve* curve = new QwtPlotCurve("Curve 1");   //设置曲线

    //以下是公式
#if 1
    QVectorpo;
    float y;
    float x;
    float u;
    for(float angel=-90;angel<=90;angel++)
    {
         x=(angel/180)*M_PI;
        if(angel!=0)
        {
            u=571*sin(x);//这里的u自变量会影响函数的周期
            y=sin(u)/u;
            y=y*900; //把y扩大900倍
            y=abs(y);
             po<setSamples(po);
    curve->setPen(QColor(160, 36,48),2,Qt::SolidLine);    //设置画笔(颜色,粗细,线条样式)
    curve->attach(ui->qwtPlot);   //把曲线附加到qwtPlot上
    curve->setCurveAttribute(QwtPlotCurve::Fitted, true);   //曲线光滑

    /*ui界面显示曲线*/
    ui->qwtPlot->replot();
    ui->qwtPlot->setAutoReplot(true);   //自动更新

}

MainWindow::~MainWindow()
{
    delete ui;
}

你可能感兴趣的:(qt)