QCustomPlot Discussion and Comments

Plotting a Polygon Graph Based on Input CoordinatesReturn to overview

I want to plot a polygon graph based on the coordinates I input. However, after using setData(x, y) to add the data, the final graph is connected according to the x-axis coordinates, rather than the order in which I input the coordinates.

#include <QApplication>
#include <QMainWindow>
#include "qcustomplot.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QMainWindow window;
    QCustomPlot *customPlot = new QCustomPlot();

    QVector<double> x(17), y(17);
    x[0] = 0.86; y[0] = 0.15;
    x[1] = 0.73; y[1] = 0.56;
    x[2] = 0.43; y[2] = 0.86;
    x[3] = 0.11; y[3] = 1.01;
    x[4] = -0.25; y[4] = 1.00;
    x[5] = -0.56; y[5] = 0.87;
    x[6] = -0.79; y[6] = 0.58;
    x[7] = -0.92; y[7] = 0.21;
    x[8] = -0.89; y[8] = -0.21;
    x[9] = -0.72; y[9] = -0.62;
    x[10] = -0.44; y[10] = -0.92;
    x[11] = -0.16; y[11] = -1.04;
    x[12] = 0.16; y[12] = -1.08;
    x[13] = 0.52; y[13] = -0.91;
    x[14] = 0.74; y[14] = -0.75;
    x[15] = 0.89; y[15] = -0.30;

    customPlot->addGraph();
    customPlot->graph(0)->setData(x, y);
    customPlot->graph(0)->setLineStyle(QCPGraph::lsLine);

    customPlot->xAxis->setRange(-1.5, 1.5);
    customPlot->yAxis->setRange(-1.5, 1.5);

    customPlot->plotLayout()->insertRow(0);
    QCPTextElement *title = new QCPTextElement(customPlot, "XY Polygon", QFont("sans", 12, QFont::Bold));
    customPlot->plotLayout()->addElement(0, 0, title);

    window.setCentralWidget(customPlot);
    window.resize(800, 600);
    window.show();

    return a.exec();
}

I used QCPCurve to solve this problem, but I also want to make the connections between points smoother. What should I do?

#include <QApplication>
#include <QMainWindow>
#include "qcustomplot.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QMainWindow window;
    QCustomPlot *customPlot = new QCustomPlot();
    
    // 创建 QCPCurve 对象
    QCPCurve *curve = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
    
    // XY 坐标数据
    QVector<double> t(16), x(16), y(16);
    x[0] = 0.86; y[0] = 0.15;
    x[1] = 0.73; y[1] = 0.56;
    x[2] = 0.43; y[2] = 0.86;
    x[3] = 0.11; y[3] = 1.01;
    x[4] = -0.25; y[4] = 1.00;
    x[5] = -0.56; y[5] = 0.87;
    x[6] = -0.79; y[6] = 0.58;
    x[7] = -0.92; y[7] = 0.21;
    x[8] = -0.89; y[8] = -0.21;
    x[9] = -0.72; y[9] = -0.62;
    x[10] = -0.44; y[10] = -0.92;
    x[11] = -0.16; y[11] = -1.04;
    x[12] = 0.16; y[12] = -1.08;
    x[13] = 0.52; y[13] = -0.91;
    x[14] = 0.74; y[14] = -0.75;
    x[15] = 0.89; y[15] = -0.30;
    
    for (int i = 0; i < 16; ++i)
    {
        t[i] = i; // 设置参数 t 为索引值,以确保按输入顺序连接点
    }
    
    // 设置数据
    curve->setData(t, x, y);
    
    // 设置轴的范围
    customPlot->xAxis->setRange(-1.5, 1.5);
    customPlot->yAxis->setRange(-1.5, 1.5);
    
    // 添加标题
    customPlot->plotLayout()->insertRow(0);
    QCPTextElement *title = new QCPTextElement(customPlot, "Polygon with QCPCurve", QFont("sans", 12, QFont::Bold));
    customPlot->plotLayout()->addElement(0, 0, title);
    
    // 将 customPlot 添加到主窗口
    window.setCentralWidget(customPlot);
    window.resize(800, 600);
    window.show();
    
    return a.exec();
}

You may need to compute spline interpolation…
Functionnality not included with qcp