RPG game Bresenham algorithm derivation and application

Bresenham algorithm is used to start graphics drawing straight lines. No matter how big the screen resolution, which is always one of square pixels composed. When drawing a straight line on an angled screen, the pixel does not fall on a straight line. For the points on the line, we need an algorithm to calculate the closest point on the straight line or the most appropriate point. BresenHam algorithm is one of an algorithm. This algorithm will only use relatively fast integer addition, subtraction and bit shift, it is very efficient.
Bresenham algorithm generally used rpg games or other needs wayfinding game. Such maps of the game will be divided into small lattice (e.g.: 32 pixels by 32 pixels), using a tool, the barrier area map may be marked, and then export the map profile (e.g., xml JSON or the like).
When rpg game entities need to find its way when the first step is to stop according to the map information, the entity to calculate whether the target point by a straight line, this time you need to use bresenham algorithm.
This paper only considers the slope greater than 0 and smaller than 1.


First look at a dda algorithm:
function dda (X0: Number, yO: Number, X1: Number, Y1: Number) {
the let Output = [];
the let K = (Y1 - yO) / (X1 - X0);
the let x = x0 + 1; // x initial value of
the while (X <= X1) {
the let Math.floor Y = (K * (X +. 1) + 0.5);
output.push ({X: X, Y: Y} );
X = X +. 1;
}
return Output;
}
little code, but used in a floating-point arithmetic multiplications and additions, not a very efficient approach.


Next look at bresenham derivation algorithm.
There are two Bresenham algorithm derivation method, as shown below:
RPG game Bresenham algorithm derivation and application


For simplicity, it is assumed, the linear equation y = kx, it is the starting point for the origin of coordinates.

  1. Set a variable, xStep = 1; totalXstep = 0; totalYstep = 0; lineY = 0; gridY = 0; preGridY = 0; deltaX = x1-x0; deltaY = y1-y0. LineY wherein the ordinate is a true straight line, girdY coordinates of the selected cell, preGridY as a grid coordinate.
  2. = Kx may be known by y, when each additional x 1, y increment to be k.
  3. Derived from the start x0.
    A. When x = x0 + 1, totalXstep = totalXstep + 1, lineY = y0 + k, discriminant of k-0.5.
    When k - while 0.5> = 0, gridY = y0 + 1, totalYstep = totalYstep + 1;
    if k - when 0.5 <0, gridy = yO;
    preGridY = gridy;
    B. When X = X0 + 2, totalXStep = totalXStep +. 1, lineY = yO + totalXstep K, discriminant is totalXstep K - (totalYstep + 0.5);
    if totalXstep K - time (totalYstep + 0.5)> = 0 , gridY = preGridY + 1, totalYstep = totalYstep + 1;
    if totalXstep
    K - (totalYstep + 0.5) <time 0, gridy = preGridY,
    preGridY = gridy;
  4. Can be obtained from the above formula is determined totalXstep k - (totalYstep + 0.5)> = 0, k = deltaY / deltaX; k is substituted into the left-type, and multiplying both sides deltaX, multiplied by 2 to give:
    2
    totalXstep deltaY -2 deltaX totalYstep - deltaX> = 0
    may be written as:
    2
    totalXstep deltaY> 2 deltaX totalYstep + * deltaX
    thus, this algorithm is only an integer multiplication and addition.

Here we change another derivation method.
1. Set variable d1, d2, Xi, Xi + 1, Yi, Yi + 1, Y, X, Hi, Hi + 1

  1. = Yi = Y-D1 (K (Xi + 1'd) + B) -Yi
    D2 + 1'd = Yi - Yi = Yi + 1'd - (K (Xi + 1'd) + B)
  2. 2K = D2-D1 (Xi + 1'd) - Yi + 2B-2. 1
    4. The k = deltaY / deltaX, substituting into the above equation
    deltaX (D1-D2) = 2deltaY Xi -2deltaX Yi + C
    so Hi = deltaX (d1 -d2) = 2deltaY Xi -2deltaX Yi + C
    is +. 1 = 2deltaY the Hi Xi +. 1 -2deltaX Yi + C +. 1
    the Hi +. 1 - 2deltaY the Hi = (. 1 + Xi - Xi) - 2deltaX (. 1 + Yi - Yi)
    order = Xi + 1'd. 1 + Xi,
    the Hi. 1 = + + 2deltaY the Hi - 2deltaX (Yi +. 1 - Yi)
    is:
    A. Yi = 1, then Hi + 1 = Hi + 2deltaY - - 2deltaX Yi + 1: if the upper right pixel, choose
    B. If the selected pixel to the right, i.e., Yi + 1 = Yi, then: Hi + 1 = Hi + 2deltaY
    the starting pixel (x0, y0) for the first parameter h0:
    h0 = 2deltaY - deltaX;
    algorithm with only to a more rapid integer addition, subtraction and bit shifting, efficient algorithms.
    function bresenham (x0: number, y0 : number, x1: number, y1: number) {
    the let Output = [];
    the let deltaX = X1 - X0;
    the let deltaY = Y1 - yO;
    the let ERR = 2
    deltaY - deltaX;
    the let X = X0;
    the let Y = yO;
    the while (X <= X1) {
    X ++;
    // assume take some right, + 2 described ERR deltaY less than 0;
    // point of fact, may also be assumed to take the top right, the condition is determined to be different
    ERR + 2 = ERR
    deltaY;
    IF (ERR> = 0) {// Description point taken is wrong right, the top right of the point to be taken
    Y ++;
    ERR ERR = - 2 * deltaX;
    }
    output.push ({X: X, Y: Y});
    }
    }

Guess you like

Origin blog.51cto.com/zhangzhao/2422258