一个极坐标类

#include <iostream>
#include <complex>

using namespace std;

template<class T>
struct BasicPolar
{
public:
    typedef BasicPolar self;
    BasicPolar() : m() {}
    BasicPolar(const self& x) : m(x.m) {}
    BasicPolar(const T& rho, const T& theta) : m(polar(rho, theta)) {}

    self operator-() { return Polar(-m); }
    self& operator+=(const self& x) { m += x.m; return *this; }
    self& operator-=(const self& x) { m -= x.m; return *this; }
    self& operator*=(const self& x) { m *= x.m; return *this; }
    self& operator/=(const self& x) { m /= x.m; return *this; }
    operator complex<T>() const { return m; }

    T rho() const { return abs(m); }
    T theta() const { return arg(m); }

    friend self operator+(self x, const self& y) { return x += y; }
    friend self operator-(self x, const self& y) { return x -= y; }
    friend self operator*(self x, const self& y) { return x *= y; }
    friend self operator/(self x, const self& y) { return x /= y; }

    friend bool operator==(const self& x, const self& y) { return x.m == y.m; }
    friend bool operator!=(const self& x, const self& y) { return x.m != y.m; }
private:
    complex<T> m;
};

typedef BasicPolar<double> Polar;

int main(int argc, char** argv)
{
    double rho = 3.0;
    double theta = 3.141592 / 2;
    Polar coord(rho, theta);
    cout << "rho = " << coord.rho() << ", theta = " << coord.theta() << endl;
    coord += Polar(4.0, 0.0);
    cout << "rho = " << coord.rho() << ", theta = " << coord.theta() << endl;
    system("pause");
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QCustomPlot是一个功能强大的Qt绘图库,可以用于绘制各种图表,包括极坐标图。 要绘制极坐标图,首先需要设置图表的坐标系极坐标。可以通过以下代码实现: ```cpp QCustomPlot customPlot; // 创建一个 QCustomPlot 对象 // 设置坐标轴型为极坐标 customPlot->setPolar(true); // 设置极坐标网格线的样式 customPlot->xAxis->grid()->setLineStyle(QCPAxisGrid::lsCircular); // 设置径向网格线样式 customPlot->yAxis->grid()->setSubGridVisible(true); // 设置角向网格线可见 customPlot->yAxis->grid()->setSubGridLineStyle(QCPAxisGrid::lsLine); // 设置角向子网格线样式 ``` 然后,可以使用QCPGraph极坐标上绘制数据。绘制极坐标图的步骤与绘制普通二维图表似,需要先创建一个图表对象,然后设置数据和样式。以下是一个示例代码,实现在极坐标上绘制一个正弦函数曲线: ```cpp // 创建一个 QCPGraph 对象 QCPGraph *graph = new QCPGraph(customPlot->xAxis, customPlot->yAxis); // 设置曲线的线条颜色和宽度 graph->setPen(QPen(Qt::red)); graph->setLineStyle(QCPGraph::lsLine); graph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 4)); // 设置曲线的数据 QVector<double> xData, yData; int pointCount = 100; // 数据个数 double angleStep = 2 * M_PI / (pointCount - 1); // 角度步长 for (int i = 0; i < pointCount; ++i) { double angle = i * angleStep; double value = std::sin(angle); xData.append(angle); yData.append(value); } graph->setData(xData, yData); // 更新图表 customPlot->replot(); ``` 以上代码会在极坐标上绘制一个红色的正弦函数曲线,并设置曲线的数据为100个。 通过以上步骤,就可以使用QCustomPlot绘制极坐标图表了。根据不同的需求,可以添加更多的图表元素,如图例、标题等,以实现更加丰富的可视化效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值