C language uses Bresenham algorithm to generate straight line (EASYx Graphics Library)

Time:2021-4-22

Bresenham algorithm is the most widely used linear scan conversion method in the field of computer graphics.

The principle is: construct a group of virtual grid lines through the center of each row and column of pixels, calculate the intersection of each vertical grid line according to the order of the line from the start point to the end point, and then determine the nearest pixel in the column of pixels.

Bresenham algorithm is also a common algorithm for drawing straight lines in computer graphics. Its essential idea is also the idea of stepping. However, because it avoids floating-point operation, it is equivalent to an improved algorithm of DDA algorithm.

Source code display:

#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
#Define x0 400 // define global variables x0, Y0: axis center (x0, Y0)
#define y0 300           
void Bresenham(int x1, int y1, int x2, int y2) 
{
 int x, y, dx, dy, p1, i;
 dx = x2 - x1;            
 dy = y2 - y1;
 If (ABS (DX) > ABS (Dy)) // the absolute value of slope is (0,1), and the step direction is x-axis
 {       
 If (x1 < x2) // draw from left to right by default
 {         
  x = x1;
  y = y1;
 }
 else
 {
  x = x2;
  y = y2;
 }    
 putpixel(x + x0, y0 - y, RED);
 Sleep(50);
 P1 = 2 * ABS (Dy) - ABS (DX); // calculate the value of the initial PI
 for (i = min(x1, x2); i < max(x1, x2); i++) 
 {
  x = x + 1;
  if (p1 >= 0) 
  {
  if (dx*dy >= 0)
   y = y + 1;
  else
   Y = y - 1; // if PI > = 0, y (I + 1) = y (I) ± 1
  P1 = P1 + 2 * (ABS (Dy) - ABS (DX)); // update pi
  }
  else 
  {
  Y = y; // if PI < 0, y (I + 1) = y (I)
  P1 = P1 + 2 * ABS (Dy); // update pi
  }
  putpixel(x + x0, y0 - y, RED);
  Sleep(50);
 }
 }
 else 
 {
 if (y1 < y2) 
 {// the stepping direction is y-axis, and the default drawing point is from bottom to top     
  x = x1;
  y = y1;
 }
 else 
 {
  x = x2;
  y = y2;
 }
 putpixel(x + x0, y0 - y, RED);
 Sleep(50);
 p1 = 2 * abs(dx) - abs(dy);
 for (i = min(y1, y2); i < max(y1, y2); i++) 
 {
  y = y + 1;
  if (p1 >= 0) 
  {
  If (DX * dy > = 0) // it is critical to judge whether the X direction is increasing or decreasing
   x = x + 1;
  else
   x = x - 1;
  p1 = p1 + 2 * (abs(dx) - abs(dy));
  }
  else 
  {
  x = x;
  p1 = p1 + 2 * abs(dx);
  }
  putpixel(x + x0, y0 - y, RED);
  Sleep(50);
 }
 }
}
void main() 
{
 int x1, x2, y1, y2;
 Printf ("please enter the coordinates of two integer points (x1, Y1), (X2, Y2));
 scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
 initgraph(800, 600);  // Initializes the size of the graphics window
 setbkcolor(WHITE);
 cleardevice();
 setcolor(BLACK);
 Line (0, Y0, x0 * 2, Y0); // axis X
 Line (x0, 0, x0, Y0 * 2); // coordinate axis Y
 Bresenham (x1, Y1, X2, Y2); // Bresenham line drawing algorithm
 _ Getch(); // wait for an arbitrary input to end
 Closegraph(); // close the graphics window
}

The results are as follows

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.