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

cmath的常见函数:qPow()求平方,log()对数10为底
角度转弧度:x=(angel/180)*M_PI
图示:在这里插入图片描述
头文件:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <qwt_plot.h>
#include <qwt_polar_curve.h>
#include <qwt_polar_grid.h>
#include <qwt_polar_marker.h>
#include <qwt_polar_canvas.h>
#include <qwt_polar_renderer.h>

#include <QVector>
#include <QMap>
#include<cmath>
#include<QtMath>
#include <QDebug>


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 <qwt_plot_curve.h>
#include <qwt_plot_grid.h>
#include <qwt_legend.h>
#include <qwt_legend_label.h>
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
    QVector<QPointF>po;
    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<<QPointF(angel,y);
        }
    }
#endif
    curve->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;
}

相关推荐

  1. qt 多边形,可以拖拽

    2024-01-27 15:06:02       5 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-27 15:06:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-27 15:06:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-27 15:06:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-27 15:06:02       18 阅读

热门阅读

  1. 嵌入式学习第二篇——C语言基础10

    2024-01-27 15:06:02       28 阅读
  2. c#扩展方法

    2024-01-27 15:06:02       34 阅读
  3. Spring Boot + EasyExcel实现Excel文件导入导出

    2024-01-27 15:06:02       36 阅读
  4. ReactHooks 官网文档翻译

    2024-01-27 15:06:02       25 阅读
  5. react-jss书写样式

    2024-01-27 15:06:02       37 阅读
  6. docker基础

    2024-01-27 15:06:02       34 阅读