Let us understand what exactly is Gupta Sproull algorithm in computer graphics and then let us see how to implement Gupta Sproull line drawing algorithm in C programming for anti aliased lines.

#### What is Gupta Sproull Algorithm?

The Gupta Sproull’s algorithm is a scan-conversion method and offers an efficient way of drawing antialiased lines.

This algorithm is primarily used to draw pixel-based lines and the output is usually printers and pixel-based display screens.

This algorithm is derived from **DDA line drawing algorithm**. wherein three pixel wide lines are drawn on the output screen. It requires a calculation of the perpendicular distance from pixel’s centre to the line’s centre.

#### What is Line antialiasing?

Anti-aliasing is also known as **Oversampling**. Anti-aliasing is a technique to transform a stair-like line into a smooth line as shown in the image above.

The Bresenham algorithm is extremely simple to implement. Along with this, it also provides speed and efficiency. This makes it applicable in many domains such as:

This technique for line antialiasing is used majorly in computer graphics and in high-end computer games. There could be different reasons for an antialiased line to occur on your screen or the output device.

One of them could be because of the **screen resolution incompatibility** with the programmed graphical format. The pixels get deformed when you convert bitmap images.

Here, anti-aliasing technique is of help which can be achieved through many computer graphics algorithms such as DDA algorithm, Gupta Sproull’s line algorithm and much more. The effect of these hazy or blurred lines can be reduced by these anti-aliasing line algorithms.

#### Flowchart for Gupta Sproull Algorithm

If you face any issues while compiling this Gupta Sproull Anti aliasing Algorithm code, you may **Install graphics.h** in your operating system.

**Note:** This C program for Anti-aliased line algorithm is compiled with Turbo C compiler on Microsoft Windows 10 operating system.

#### C Program To Implement Gupta Sproull Algorithm For Anti Aliased Line

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 | #include<stdio.h> #include<graphics.h> #include<math.h> #include<stdlib.h> void gupta_sproull_algorithm(int x0, int x1, int y0, int y1); void pixel_brightness(int x, int y, double length); int main() { int graphics_driver = DETECT, graphics_mode; int x0, x1, y0, y1; initgraph(&graphics_driver, &graphics_mode, "c:\\turboc3\\bgi"); printf("Enter X-axis coordinate of the initial point:\t"); scanf("%d", &x0); printf("Enter X-axis coordinate of the final point:\t"); scanf("%d", &x1); printf("Enter Y-axis coordinate of the initial point:\t"); scanf("%d", &y0); printf("Enter Y-axis coordinate of the final point:\t"); scanf("%d", &y1); gupta_sproull_algorithm(x0, x1, y0, y1); return 0; } void gupta_sproull_algorithm(int x0, int x1, int y0, int y1) { int dx = x1 - x0; int dy = y1 - y0; int d = 2 * dy - dx; int increment_E = 2 * dy; int increment_NE = 2 * (dy - dx); int 2dx = 0; int x = x0; int y = y0; double inv_denominator = 1.0 / (2.0 * sqrt(dx * dx + dy * dy)); double inv_denominator_2dx = 2.0 * dx * inv_denominator; pixel_brightness(x, y, 0); pixel_brightness(x, y + 1, inv_denominator_2dx); pixel_brightness(x, y - 1, inv_denominator_2dx); for(;x < x1;) { if(d < 0) { 2dx = d + dx; d = d + increment_E; } else { 2dx = d - dx; d = d + increment_NE; y = y + 1; } x = x + 1; pixel_brightness(x, y, 2dx * inv_denominator); pixel_brightness(x, y + 1, inv_denominator_2dx - 2dx * inv_denominator); pixel_brightness(x, y - 1, inv_denominator_2dx - 2dx * inv_denominator); } } void pixel_brightness(int x, int y, double length) { unsigned long brightness; brightness = filter ((floor(fabs (length)) + 0.5), color); *( *(grid + y) + x) = brightness; } |

Let us discuss more on this Gupta Sproull Algorithm in C programming in the comment section below. Do let us know if you have any doubts or any information to share.