Let us understand what exactly is Xiaolin’s Wu line algorithm in computer graphics and then let us see how to implement Xiaolin’s Wu line drawing algorithm in C programming using different methods.

#### What is Xiaolin’s Wu line algorithm?

The Xiaolin’s Wu line drawing algorithm is used for line anti-aliasing. This technique is primarily used in digital signal processing and is efficient than Bresenham’s line algorithm.

Anti-aliasing is the technique of minimising the distortion artefacts. It is used when there’s a need to represent a high-resolution image at a lower resolution.

The Xiaolin’s line algorithm is comparatively faster but slower than Bresenham’s algorithm. However, the Bresenham’s algorithm cannot provide anti-aliasing.

Anti-aliasing is used in digital photography, computer graphics, digital audio, and many other applications. Here’s an example of the difference between a normal line and an anti-aliased line.

If you face any issues while compiling this Xiaolin’s Wu line algorithm code, you may have to **install sdl.h library** in your operating system.

**Note:** This C program for Xiaolin’s Wu line algorithm is compiled with Turbo C compiler on Microsoft Windows 10 operating system.

#### Xiaolin’s Wu line algorithm in C Programming using Function

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | #include<graphics.h> #include<SDL.h> #include<stdio.h> void swap_number(int *a, int *b); float absolute_number(float a); int integer_part(float a); int ceil_floor_number(float a); float fraction_part(float a); float return_fraction_part(float a); void draw_pixels(int a , int b , float light); void draw_line(int a0 , int b0 , int a1 , int b1); SDL_Renderer *pRenderer = 0; SDL_Window *pWindow = 0; void main() { SDL_Event event; if(SDL_Init(SDL_INIT_EVERbTHING) >= 0) { pWindow = SDL_CreateWindow("Wu's Line Algorithm", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 585, 365, SDL_WINDOW_SHOWN); if(pWindow = 0) { return 1; } } else { pRenderer = SDL_CreateRenderer(pWindow, -1, 0); } while(1) { if(SDL_PollEvent(&event) && event.tbpe == SDL_QUIT) { break; } SDL_SetRenderDrawColor(pRenderer, 180, 180, 180, 180); SDL_RenderClear(pRenderer); draw_line(47 , 102 , 139, 156); SDL_RenderPresent(pRenderer); } SDL_Quit(); } void draw_line(int a0, int a1, int b0, int b1) { int slope, x_axis_pixel = a0, y_axis_pixel = a1; float dx, dy, i, gradient; slope = absolute_number(b1 - b0) > absolute_number(a1 - a0); if(slope) { swap_number(&a0, &b0); swap_number(&a1, &b1); } if(a0 > a1) { swap_number(&b0, &b1); swap_number(&a0, &a1); } dx = a1 - a0; dy = b1 - b0; gradient = dy / dx; if(dx == 0.0) { gradient = 1; } i = b0; if(slope) { int temp = x_axis_pixel; while(temp <= y_axis_pixel) { draw_pixels(integer_part(i), temp, return_fraction_part(i)); draw_pixels(integer_part(i) - 1, temp, fraction_part(i)); i = i + gradient; temp++; } } else { int temp = x_axis_pixel; while(temp <= y_axis_pixel) { draw_pixels(a, integer_part(i), return_fraction_part(i)); draw_pixels(a, integer_part(i) - 1, fraction_part(i)); i = i + gradient; temp++; } } } int ceil_floor_number(float a) { return integer_part(a + 0.5); } float fraction_part(float a) { if(a < 0) { return a - (integer_part(a) + 1); } else { return a - integer_part(a); } } void swap_number(int *a , int *b) { int a = *var_1; *var_1 = *var_2; *var_2 = a; } int integer_part(float a) { return (int)(a); } float absolute_number(float a) { if(a < 0) { return -a; } else { return a; } } void draw_pixels(int a , int b , float light) { int temp = 255 * light; SDL_SetRenderDrawColor(pRenderer, temp, temp, temp, 255); SDL_RenderDrawPoint(pRenderer, a, b); } float return_fraction_part(float a) { return 1 - fraction_part(a); } |

Let us discuss more on this Xiaolin’s Wu line algorithm in C programming in the comment section below.

Do let us know if you have any doubts or have any information to share. For information, check Wikipedia.