You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C+++SFML中余弦插值被渲染为线性插值的问题排查

余弦插值与线性插值渲染效果一致的问题排查

问题概述

我正在实现点之间的多种插值方法,线性插值(Linear interpolation)已正常工作,但余弦插值(Cosine interpolation)的渲染效果却与线性插值完全一致,存在明显异常。开发环境为C++ + SFML渲染。

核心代码实现

线性插值函数

sf::Vector2f game::linearInterpolate(sf::Vector2f a, sf::Vector2f b, float randN) {
        return sf::Vector2f(a.x * (1 - randN) + b.x * randN,
            a.y * (1 - randN) + b.y * randN);
}

注:a为起始插值点,b为目标插值点,randN是0~1的随机数。sf::Vector2f是SFML的二维浮点向量,代表像素坐标。

余弦插值函数

sf::Vector2f game::cosineInterpolate(sf::Vector2f a, sf::Vector2f b, float randN) {
    float ft = randN * 3.1415927f;
    float f = (1 - cos(ft)) * 0.5f;

    return sf::Vector2f(a.x * (1-f) + b.x * f,
        a.y * (1 - f) + b.y * f);
    
}

该函数的数学逻辑参考了相关教程,但实际返回结果与线性插值完全一致,存在问题。

像素绘制逻辑

// 遍历每一组待插值的点对
for (sf::Vector2f &l : this->noiseSpots) {
    
    // 绘制两点间的插值像素
    for (unsigned long xScreen = 0; xScreen < 1000; xScreen++) {
        // 生成0~1的随机数
        float r = this->rand01();
        
        // 计算插值坐标
        sf::Vector2f lInterpolatedVec = linearInterpolate(l, this->noiseSpots.at(a + 1), r);
        sf::Vector2f coInterpolatedVec = cosineInterpolate(l, this->noiseSpots.at(a + 1), r);

        // 绘制线性插值点(白色)
        this->graph.setPixel(lInterpolatedVec.x, lInterpolatedVec.y, sf::Color(255, 255, 255));
        // 绘制余弦插值点(红色)
        this->graph.setPixel(coInterpolatedVec.x, coInterpolatedVec.y, sf::Color(255, 0, 0));
    }
    // 切换到下一组点对
    if (a < fidelity-1) { a++; }
}

注:noiseSpots是存储所有待插值点的向量,fidelity为点的总数。因std::next无法正常访问向量下一个元素,采用a从0递增的方式获取下一个点(noiseSpots.at(a + 1))。

补充排查信息

  • 当将余弦插值函数中的f替换为randN时,能得到预期的曲线效果,说明问题确实出在余弦插值函数的计算逻辑中,但暂未明确具体原因。
  • 渲染效果对比:
    • 线性插值示例:线性插值示例图
    • 余弦插值示例:余弦插值示例图

内容的提问来源于stack exchange,提问作者DGYLLEANN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 16:57:30