使用Bresenham的线算法绘制线 [英] Drawing lines with Bresenham's Line Algorithm

查看:89
本文介绍了使用Bresenham的线算法绘制线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的计算机图形作业是仅使用绘制点的能力来实现OpenGL算法.

My computer graphics homework is to implement OpenGL algorithms using only the ability to draw points.

因此,很明显,我需要先获得drawLine()才能工作,然后才能绘制其他任何东西. drawLine()必须仅使用整数来完成.没有浮点数.

So obviously I need to get drawLine() to work before I can draw anything else. drawLine() has to be done using integers only. No floating point.

这就是我所教的.基本上,线可以分为4个不同的类别,正陡,正浅,负陡和负浅.这是我应该绘制的图片:

This is what I was taught. Basically, lines can be broken up into 4 different categories, positive steep, positive shallow, negative steep and negative shallow. This is the picture I am supposed to draw:

这是我的程序绘制的图片:

and this is the picture my program is drawing:

颜色为我们完成.给定了顶点,我们需要使用Bresenham的Line算法根据起点和终点绘制线.

The colors are done for us. We are given vertices and we need to use Bresenham's Line algorithm to draw the lines based on the start and end points.

这是我到目前为止所拥有的:

This is what I have so far:

int dx = end.x - start.x;
int dy = end.y - start.y;

//initialize varibales
int d;
int dL;
int dU;

if (dy > 0){
        if (dy > dx){
                //+steep
                d = dy - 2*dx;
                dL = -2*dx;
                dU = 2*dy - 2*dx;

                for (int x = start.x, y = start.y; y <= end.y; y++){
                        Vertex v(x,y);
                        drawPoint(v);

                        if (d >= 1){
                                d += dL;
                        }else{
                                x++;
                                d += dU;
                        }
                }              
        } else {
                //+shallow
                d = 2*dy - dx;
                dL = 2*dy;
                dU = 2*dy - 2*dx;

                for (int x = start.x, y = start.y; x <= end.x; x++) {
                        Vertex v(x,y);
                        drawPoint(v);

                        // if choosing L, next y will stay the same, we only need
                        // to update d by dL
                        if (d <= 0) {
                                d += dL;
                        // otherwise choose U, y moves up 1
                        } else {
                                y++;
                                d += dU;
                        }
                }
        }
} else {
        if (-dy > dx){
                cout << "-steep\n";
                //-steep
                d = dy - 2*dx;
                //south
                dL = 2*dx;
                //southeast
                dU = 2*dy - 2*dx;

                for (int x = start.x, y = start.y; y >= end.y; --y){
                        Vertex v(x,y);
                        drawPoint(v);

                        //if choosing L, next x will stay the same, we only need
                        //to update d
                        if (d >= 1){
                                d -= dL;
                        } else {
                                x++;
                                d -= dU;
                        }
                }

        } else {
                cout << "-shallow\n";
                //-shallow
                d = 2*dy - dx;
                dL = 2*dy;
                dU = 2*dy - 2*dx;

                for (int x = start.x, y = start.y; x <= end.x; x++){
                        Vertex v(x,y);
                        drawPoint(v);

                        if (d >= 0){
                                d += dL;
                        } else {
                                --y;
                                d -= dU;
                        }
                }
        }
}

我知道我的错误将是愚蠢的,但是老实说我无法弄清楚我在做什么错.为什么有些线绘制不正确,如上所示?

I know my error is going to be something silly, but I honestly cannot figure out what I am doing wrong. Why are some of the lines drawn incorrectly as shown above?

推荐答案

万一有人想知道问题出在哪里,我仍然不知道这是什么.我最终要做的是将代码重构,以便-shallow和-steep分别使用与+ shallow和+ steep相同的算法.调整了x,y坐标(使x或y坐标取负)后,当我绘制它们时,我取反了原来的取反,以便将其绘制在正确的位置.

In case anyone was wondering what the problem was, I still don't know what it was. What I ended up doing was re-factored my code so that the -shallow and -steep used the same algorithm as +shallow and +steep, respectively. After adjusting the x,y coordinates (negating the x or y coordinate), when I went to plot them I negated my original negation so that it plotted in the right spot.

这篇关于使用Bresenham的线算法绘制线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆