dda算法_计算机图形学中的DDA(数字差分分析仪)算法

dda算法

DDA(数字差分分析仪)算法 (DDA (Digital Differential Analyzer) Algorithm)

In computer graphics, the DDA algorithm is the simplest algorithm among all other line generation algorithms. Here, the DDA is an abbreviation that stands for "Digital Differential Analyzer". It is an incremental method, i.e. it works by incrementing the source coordinate points according to the values of the slope generated.

在计算机图形学中, DDA算法是所有其他线生成算法中最简单的算法。 在此, DDA“数字差分分析仪”的缩写。 这是一种增量方法,即,它根据生成的坡度值通过增加源坐标点来工作。

Hence, we can define DDA as follows,

因此,我们可以如下定义DDA,

"DDA stands for Digital Differential Analyzer. This algorithm is incremental and is used for the rasterization of lines, triangles, and polygons."

“ DDA代表数字差分分析仪。此算法是增量算法,用于线,三角形和多边形的栅格化。”

DDA算法的工作 (Working of the DDA Algorithm)

Suppose we have to draw a line PQ with coordinates P (x1, y1) and Q (x2, y2).

假设我们必须绘制一条坐标为P(x1,y1)和Q(x2,y2)的直线PQ

  1. First, Calculate dx = (x2 - x1) and dy = (y2 - y1)

    首先,计算dx =(x2-x1)和dy =(y2-y1)

  2. Now calculate the slope m = (dy / dx)

    现在计算斜率m =(dy / dx)

  3. Calculate the number of points to be plotted (i.e. n) by finding the maximum of dx and dy, i.e. n = abs (max (dx , dy))

    通过找到dxdy的最大值来计算要绘制的点数(即n ),即n = abs(最大值(dx,dy))

    To draw an accurate line, more number of points are required. Therefore, the maximum of the two values is used here.

    要绘制一条精确的线,需要更多的点。 因此,此处使用两个值中的最大值。

  4. Now as the n is calculated, to know by how much each source point should be incremented, calculate xinc and yinc as follows: xinc = (dx / n) and yinc = (dy / n)

    现在,当计算n时,要知道每个源点应增加多少,请按以下方式计算x incy incx inc =(dx / n)和y inc =(dy / n)

  5. Now we draw the points from P to Q. The successive points are calculated as follows: (xnext, ynext) = (x + xinc, y + yinc)

    现在我们将点从P画到Q。 连续点的计算如下: (x next ,y next )=(x + x inc ,y + y inc )

    Start plotting the points from

    从开始绘制点

    P and stop when Q is reached. In case the incremented values are decimal, use the round off values.

    P并在达到Q时停止。 如果增量值为十进制,请使用四舍五入值。

Now, let us have a look at the algorithm that is followed in DDA.

现在,让我们看一下DDA中遵循的算法。

DDA算法 (DDA Algorithm)

  • Step 1: Start.

    步骤1:开始。

  • Step 2: Declare x1, y1, x2, y2.

    步骤2:声明x1,y1,x2,y2。

  • Step 3: Calculate the following,

    步骤3:计算以下内容,

        dx = x2 - x1
        dy = y2 - y1
    
    
  • Step 4: Calculate slope as follows,

    步骤4:按以下方式计算斜率,

        m = dy / dx
    
    
  • Step 5: Calculate the no. of points (n) between x1 and x2 as follows,

    步骤5:计算编号。 x1和x2之间的点(n)如下,

        n = abs ( max ( dx , dy ) )
    
    
  • Step 6: Calculate xincand yinc as follows,

    步骤6:按以下方式计算x inc和y inc

        xinc = (dx / n) and yinc = (dy / n)
    
    
  • Step 7: Now, Initialize x = x1 and y = y1.

    步骤7:现在,初始化x = x1和y = y1。

  • Step 8:

    步骤8:

        while ( x <= x2 )
            x = x + xinc
            y = y + yinc
    
    
  • Step 9: Now, Plot (x,y) on the graph, and hence we get our required line between the given points.

    步骤9:现在,在图形上绘制(x,y),因此我们得到了给定点之间的所需线。

  • Step 10: End.

    步骤10:结束。

Formula:

式:

In the entire DDA algorithm, there are three conditions and according to these conditions, the formula for calculating the coordinates is changed. These formulas are as follows,

在整个DDA算法中 ,存在三个条件,并根据这些条件更改了计算坐标的公式。 这些公式如下:

    If m < 1 :      If m > 1 :          If m = 1 :
        xinc = 1        xinc = (1 / m)      xinc = 1
        yinc = m        yinc = 1            yinc = 1

Example:

例:

Now let us take an example to understand the whole working of the DDA algorithm,

现在让我们举一个例子来了解DDA算法的整个工作原理

Question: Draw a line from A(2 , 2) to B(5 , 5) using the DDA algorithm.

问题:使用DDA算法A(2,2)B(5,5 )画一条线。

Solution:

解:

    Given:
    x1 = 2 , y1 = 2
    x2 = 5 , y2 = 6 

    Calculating:    
    dx  = (x2 - x1) = (5 - 2) = 3
    dy  = (y2 - y1) = (6 - 2) = 4
    n   = abs (max (dx , dy ) ) = abs (max (3 , 4) ) = 4

    xinc = dx / n = 3/4 = 0.75
    yinc = dy / n = 4 / 4 = 1

XYx = round(x + xinc)y = y + yinc
222 + 0.75 = 2.75 = 32 + 1 = 3
333 + 0.75 = 3.75 = 43 + 1 = 4
444 + 0.75 = 4.75 = 54 + 1 = 5
555 + 0.75 = 5.75 = 65 + 1 = 6
X ÿ x =圆(x + x inc ) y = y + y inc
2 2 2 + 0.75 = 2.75 = 3 2 +1 = 3
3 3 3 + 0.75 = 3.75 = 4 3 +1 = 4
4 4 4 + 0.75 = 4.75 = 5 4 +1 = 5
5 5 5 + 0.75 = 5.75 = 6 5 +1 = 6

Stop here as we have reached point B.

当我们到达B点时,在此停止。

Now, Plot the points ( (2 , 2) , (3 , 3) , (4 , 4) , (5 , 5) ) on the graph and thus we get our required line from point A to point B.

现在,在图形上绘制点((2,2),(3、3),(4、4),(5、5)) ,这样我们就得到了从点A到点B所需的线。

DDA算法的优点 (Advantages of the DDA algorithm)

Now, we will be looking at the advantages that the DDA algorithm offers over other line drawing algorithms.

现在,我们将探讨DDA算法相对于其他线条绘制算法的优势。

  1. It is the simplest line generation algorithm.

    它是最简单的线生成算法。

  2. Implementation of the DDA algorithm is very easy as compared to other line generation algorithms.

    与其他线路生成算法相比,DDA算法的实现非常容易。

  3. It does not use multiplication which reduces the time complexity of implementation.

    它不使用乘法来减少实现的时间复杂度。

  4. It is a faster and a better method than using the direct method of the line equation: i.e. y = mx + c

    与使用线性方程式的直接方法相比,这是一种更快,更好的方法:即y = mx + c

DDA算法的缺点 (Disadvantages of the DDA algorithm)

  • DDA algorithm use floating-point arithmetic as it involves the use of division in the calculation of xinc and yinc. This floating-point arithmetic makes the algorithm time-consuming.

    DDA算法使用浮点算法,因为它涉及在x inc和y inc的计算中使用除法。 这种浮点算法使算法耗时。

  • The use of floating-point arithmetic decreases the accuracy of the generated points. Hence the points that we get are not accurate, i.e. they may not lie accurately on the line.

    使用浮点算法会降低生成点的准确性。 因此,我们得到的点是不准确的,即它们可能不准确地位于线上。

  • As the points that we get from the DDA algorithm are not accurate, the lines generated by this algorithm are not smooth, i.e. some discontinuation and zigzag nature can be commonly seen in the lines drawn through this algorithm.

    由于我们从DDA算法获得的点不准确,因此该算法生成的线条不平滑,即在通过该算法绘制的线条中通常可以看到一些不连续和之字形性质。

翻译自: https://www.includehelp.com/computer-graphics/dda-digital-differential-analyzer-algorithm.aspx

dda算法

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值