How to resolve the algorithm Xiaolin Wu's line algorithm step by step in the C++ programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Xiaolin Wu's line algorithm step by step in the C++ programming language

Table of Contents

Problem Statement

Implement the   Xiaolin Wu's line algorithm   described in Wikipedia.

This algorithm draws anti-aliased lines.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Xiaolin Wu's line algorithm step by step in the C++ programming language

The provided C++ code implements the Wu line algorithm, which is used to draw lines between two points on a raster display. Here's a detailed explanation of the code:

  1. Function Signature:

    void WuDrawLine(float x0, float y0, float x1, float y1,
                  const std::function<void(int x, int y, float brightess)>& plot)
    • x0, y0: Coordinates of the starting point of the line.
    • x1, y1: Coordinates of the ending point of the line.
    • plot: A callback function that takes the coordinates (x, y) and a brightness value and is responsible for plotting a point on the display.
  2. Helper Functions:

    • ipart(float x): Returns the integer part of x.
    • round(float x): Rounds x to the nearest integer.
    • fpart(float x): Returns the fractional part of x.
    • rfpart(float x): Returns 1 - fpart(x).
  3. Variables:

    • steep: A boolean variable to determine if the line is steep (i.e., has a greater slope than 1).
    • If the line is steep, the x and y coordinates are swapped.
    • If the starting point is to the right of the ending point, the coordinates are swapped.
  4. Calculating Line Parameters:

    • dx: The difference between x1 and x0.
    • dy: The difference between y1 and y0.
    • gradient: The slope of the line (ratio of dy to dx).
  5. Initial Point Calculation:

    • xpx11: The integer part of the x-coordinate of the initial point.
    • intery: The initial y-coordinate of the line.
    • The values for xpx11, ypx11, and intery are calculated based on the xend, yend, and xgap values.
    • xgap represents the fractional part of the x-coordinate of the initial point.
  6. Endpoint Calculation:

    • xpx12: The integer part of the x-coordinate of the endpoint.
    • ypx12: The integer part of the y-coordinate of the endpoint.
    • Similar to the initial point calculation, these values are computed based on the xend, yend, and xgap values.
  7. Drawing the Line:

    • The code iterates over x values between xpx11 and xpx12.
    • For each x, the intery value is calculated.
    • The plot callback is called four times for each x value to set brightness values at four pixel locations around the line.
    • Depending on whether the line is steep or not, the plot calls are adjusted.
  8. Algorithm Overview:

    • The algorithm first determines if the line is steep and swaps the coordinates if necessary.
    • It then calculates the line's gradient and initial and endpoint coordinates.
    • The line is then drawn by iterating over x values and calculating the corresponding y values.
    • For each x, it determines the brightness values at four pixel locations around the line and uses the plot callback to set these values on the display.

Source code in the cpp programming language

#include <functional>
#include <algorithm>
#include <utility>

void WuDrawLine(float x0, float y0, float x1, float y1,
                const std::function<void(int x, int y, float brightess)>& plot) {
    auto ipart = [](float x) -> int {return int(std::floor(x));};
    auto round = [](float x) -> float {return std::round(x);};
    auto fpart = [](float x) -> float {return x - std::floor(x);};
    auto rfpart = [=](float x) -> float {return 1 - fpart(x);};
        
    const bool steep = abs(y1 - y0) > abs(x1 - x0);
    if (steep) {
        std::swap(x0,y0);
        std::swap(x1,y1);
    }
    if (x0 > x1) {
        std::swap(x0,x1);
        std::swap(y0,y1);
    }
        
    const float dx = x1 - x0;
    const float dy = y1 - y0;
    const float gradient = (dx == 0) ? 1 : dy/dx;
        
    int xpx11;
    float intery;
    {
        const float xend = round(x0);
        const float yend = y0 + gradient * (xend - x0);
        const float xgap = rfpart(x0 + 0.5);
        xpx11 = int(xend);
        const int ypx11 = ipart(yend);
        if (steep) {
            plot(ypx11,     xpx11, rfpart(yend) * xgap);
            plot(ypx11 + 1, xpx11,  fpart(yend) * xgap);
        } else {
            plot(xpx11, ypx11,    rfpart(yend) * xgap);
            plot(xpx11, ypx11 + 1, fpart(yend) * xgap);
        }
        intery = yend + gradient;
    }
    
    int xpx12;
    {
        const float xend = round(x1);
        const float yend = y1 + gradient * (xend - x1);
        const float xgap = rfpart(x1 + 0.5);
        xpx12 = int(xend);
        const int ypx12 = ipart(yend);
        if (steep) {
            plot(ypx12,     xpx12, rfpart(yend) * xgap);
            plot(ypx12 + 1, xpx12,  fpart(yend) * xgap);
        } else {
            plot(xpx12, ypx12,    rfpart(yend) * xgap);
            plot(xpx12, ypx12 + 1, fpart(yend) * xgap);
        }
    }
        
    if (steep) {
        for (int x = xpx11 + 1; x < xpx12; x++) {
            plot(ipart(intery),     x, rfpart(intery));
            plot(ipart(intery) + 1, x,  fpart(intery));
            intery += gradient;
        }
    } else {
        for (int x = xpx11 + 1; x < xpx12; x++) {
            plot(x, ipart(intery),     rfpart(intery));
            plot(x, ipart(intery) + 1,  fpart(intery));
            intery += gradient;
        }
    }
}


  

You may also check:How to resolve the algorithm Record sound step by step in the Go programming language
You may also check:How to resolve the algorithm Loops/Increment loop index within loop body step by step in the Go programming language
You may also check:How to resolve the algorithm Word frequency step by step in the MATLAB / Octave programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the Coco programming language
You may also check:How to resolve the algorithm Remove duplicate elements step by step in the Wortel programming language