【问题标题】:Control the number of intermediate points between two vertices - Extension of Bresenham's algorithm控制两个顶点之间的中间点的数量 - Bresenham 算法的扩展
【发布时间】:2017-12-06 17:48:19
【问题描述】:

我有以下可用的 Bresenham 算法,它找到两个顶点之间的中间点。但这是针对大小为 1 的像素。

void Bresenham(int x1,
    int y1,
    int const x2,
    int const y2)
{
    int delta_x(x2 - x1);
    // if x1 == x2, then it does not matter what we set here
    signed char const ix((delta_x > 0) - (delta_x < 0));
    delta_x = std::abs(delta_x) << 1;

    int delta_y(y2 - y1);
    // if y1 == y2, then it does not matter what we set here
    signed char const iy((delta_y > 0) - (delta_y < 0));
    delta_y = std::abs(delta_y) << 1;

    cout << "(" << x1 << "," << y1 << ")\n";
    //plot(x1, y1);

    if (delta_x >= delta_y)
    {
        // error may go below zero
        int error(delta_y - (delta_x >> 1));

        while (x1 != x2)
        {
            // reduce error, while taking into account the corner case of error == 0
            if ((error > 0) || (!error && (ix > 0)))
            {
                error -= delta_x;
                y1 += iy;
            }
            // else do nothing

            error += delta_y;
            x1 += ix;

            cout << "(" << x1 << "," << y1 << ")\n";
            //plot(x1, y1);
        }
    }
    else
    {
        // error may go below zero
        int error(delta_x - (delta_y >> 1));

        while (y1 != y2)
        {
            // reduce error, while taking into account the corner case of error == 0
            if ((error > 0) || (!error && (iy > 0)))
            {
                error -= delta_y;
                x1 += ix;
            }
            // else do nothing

            error += delta_x;
            y1 += iy;

            cout << "(" << x1 << "," << y1 << ")\n";
            //plot(x1, y1);
        }
    }
}

我想控制两个给定顶点之间形成的中间点的数量。在上面的代码中,我目前无法控制它。

例如:如果我有顶点 (0,0) 和 (3,3);如果我想要介于两者之间的 2 个点,那就是 (1,1) 和 (2,2)。

您能否建议在我的代码中进行更改,以便我可以控制给定两个顶点之间的中间点的数量,并且请告诉我确定每个像素长度的方法(目前,像素大小固定为1)

我希望我的整体函数看起来像:void Bresenham(int x1, 整数 y1, int 常量 x2, int const y2, int TotalIntermediatePoints)

提前致谢!

【问题讨论】:

    标签: c++ geometry bresenham


    【解决方案1】:

    看来您根本不需要 Bresenham 算法。
    并且中间点可能有非整数坐标。

    只使用线性插值

     x[i] = x0 + (x1-x0) * i / (N + 1)
    

    【讨论】:

    • 你认为,我可以实现 2D 线性插值。如果你不介意,请你分享算法,以便我可以尝试并找到它。另外,请解释我如何确定以中间点为中心的像素大小。
    • 我已经写过公式(Y 类似)。块大小是长度(段)除以N
    • 代码中的“N”是中间点数(或)分割数??所以,如果顶点是(0,0)和(5,0);中间点为 5,像素大小为 5/5 = 1?对吗?
    • 但是如果我有一个三角形的三个边,那么如果顶点1和顶点2之间的距离(连接边1); vertex2 和 vertex 3(连接 edge2)是不同的,那么即使沿边缘 1 和边缘 2 之间绘制的 Pieces(或像正方形的像素)的长度也不同,对吧?如果是这样,我将如何应用填充算法?请解释一下。
    • 您的真正问题是什么?
    猜你喜欢
    • 1970-01-01
    • 2012-10-09
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 2019-04-20
    • 2015-10-19
    • 2010-11-18
    • 2010-10-03
    相关资源
    最近更新 更多