c++ - 是否有一种方法可以根据不同的变量重新计算和方程式?

标签 c++ qt algebra calculus qcustomplot

我目前是AP Calculus BC的大四学生,接受了在C++ Qt中复制主题的挑战。本主题涵盖作为曲线下方区域的积分以及这些区域的旋转以形成具有确定体积的实体模型。

enter image description here

我已经成功旋转了定义为的自定义方程式:

double y = abs(qSin(qPow(graphXValue,graphXValue))/qPow(2, (qPow(graphXValue,graphXValue)-M_PI/2)/M_PI))

要么

equation

我的问题是如何围绕Y轴而不是X轴旋转这样的方程式。是否有任何方法可以用y而不是x来近似求解该方程?当前是否有这种任务的实现?

请记住,我正在计算3D坐标系中变换的每个点:
for (float x = 0.0f; x < t_functionMaxX - t_projectionStep; x+=t_projectionStep)
{
    currentSet = new QSurfaceDataRow;
    nextSet = new QSurfaceDataRow;

    float x_pos_mapped = x;
    float y_pos_mapped = static_cast<float>(ui->customPlot->graph(0)->data()->findBegin(static_cast<double>(x), true)->value);

    float x_pos_mapped_ahead = x + t_projectionStep;
    float y_pos_mapped_ahead = static_cast<float>(graph1->data()->findBegin(static_cast<double>(x + t_projectionStep), true)->value);

    QList<QVector3D> temp_points;
    for (float currentRotation = static_cast<float>(-2*M_PI); currentRotation < static_cast<float>(2*M_PI); currentRotation += static_cast<float>((1) * M_PI / 180))
    {
        float y_pos_calculated = static_cast<float>(qCos(static_cast<qreal>(currentRotation))) * y_pos_mapped;
        float z_pos_calculated = static_cast<float>(qSin(static_cast<qreal>(currentRotation))) * y_pos_mapped;

        float y_pos_calculated_ahead = static_cast<float>(qCos(static_cast<qreal>(currentRotation))) * y_pos_mapped_ahead;
        float z_pos_calculated_ahead = static_cast<float>(qSin(static_cast<qreal>(currentRotation))) * y_pos_mapped_ahead;

        QVector3D point(x_pos_mapped, y_pos_calculated, z_pos_calculated);
        QVector3D point_ahead(x_pos_mapped_ahead, y_pos_calculated_ahead, z_pos_calculated_ahead);

        *currentSet << point;
        *nextSet << point_ahead;

        temp_points << point;
    }
    *data << currentSet << nextSet;


    points << temp_points;
}

最佳答案

本质上,您绕着Y轴旋转了 vector (x,f(x),0),因此Y值保持不变,但是X和Y部分随旋转而变化。

我还用static_cast<float>构造函数的显式调用替换了所有float部分,(我发现)它的读取效果更好。

// Render the upper part, grow from the inside
for (float x = 0.0f; x < t_functionMaxX - t_projectionStep; x+=t_projectionStep)
{
    currentSet = new QSurfaceDataRow;
    nextSet = new QSurfaceDataRow;

    float x_pos_mapped = x;
    float y_pos_mapped = float(ui->customPlot->graph(0)->data()->findBegin(double(x), true)->value);

    float x_pos_mapped_ahead = x + t_projectionStep;
    float y_pos_mapped_ahead = float(graph1->data()->findBegin(double(x + t_projectionStep), true)->value);

    QList<QVector3D> temp_points;
    for (float currentRotation = float(-2*M_PI); currentRotation < float(2*M_PI); currentRotation += float((1) * M_PI / 180))
    {
        float x_pos_calculated = float(qCos(qreal(currentRotation))) * x_pos_mapped;
        float z_pos_calculated = float(qSin(qreal(currentRotation))) * x_pos_mapped;

        float x_pos_calculated_ahead = float(qCos(qreal(currentRotation))) * x_pos_mapped_ahead;
        float z_pos_calculated_ahead = float(qSin(qreal(currentRotation))) * x_pos_mapped_ahead;

        QVector3D point(x_pos_calculated, y_pos_mapped, z_pos_calculated);
        QVector3D point_ahead(x_pos_calculated_ahead, y_pos_mapped_ahead, z_pos_calculated_ahead);

        *currentSet << point;
        *nextSet << point_ahead;

        temp_points << point;
    }
    *data << currentSet << nextSet;


    points << temp_points;
}

接下来,您需要添加底部的“板”。就像我们上面所做的那样,这只是一堆三角形,将(0,0,0)(1,0,0)绕Y轴旋转的两个相邻点相连。

最后,如果f(t_functionmaxX)不为零,则需要添加一个将(t_functionmaxX, f(t_functionmaxX), 0)连接到(t_functionmaxX, 0, 0)的边,并再次绕Y轴逐步旋转。

请注意,如果y <0,这将做一些奇怪的事情。如何解决取决于您自己。

关于c++ - 是否有一种方法可以根据不同的变量重新计算和方程式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61100193/

相关文章:

C++ UDP 在一个类中接收一个 vector

c++ - 如何使用 CImg.h 库在另一张图片上显示一张图片?

c++ - 如何将 QVector<double> 转换为 QBytearray

math - boolean 代数文章/书

algorithm - 获取颜色联合的可能性以生成另一种颜色

c++ - 是否可以在单个函数中使用默认参数和可变数量的参数?

c++ - 为一个类编写单元测试

c++ - 在 qtextedit 中高亮显示 x 和 y 作为位置的单词

c++ - Qt Scroll Area 不添加滚动条

php - 像美术课一样混合颜色(加减颜色)!