Computer graphics: point-by-point comparison method to generate straight lines

This article refers to the book "Computer Graphics Tutorial", and some pictures are quoted.

1. Generate a straight line

 The so-called generating a straight line is to give you a set of (x1, y1, x2, y2) parameters and ask you to generate a straight line from (x1, y1) to (x2, y2).
 On a raster display full of pixels, we can control the brightness of each pixel, how do you generate this line?
raster display

2. Point-by-point comparison method

(1) Basic derivation process

The derivation process of the point-by-point comparison method (taking the first quadrant as an example) is as follows: Alt
The promotion of the point-by-point comparison method:
Alt
the algorithm process of the point-by-point comparison method:
insert image description here

Explanation:
 Taking the origin O of the coordinate system as the starting point above, the process of generating a straight line from the starting point O to the ending point A is discussed.
 The reason for comparing the slopes of OA and OM is that OA is the target straight line. Comparing the slopes of OM and OA can determine the relative position of the current generated point M, and determine whether M is above or below the target straight line.
 When the straight line is generated to point M, the above figure shows the transition direction determined according to the state value F, so that M moves to a new generation point and calculates a new state value F.
 The walking direction above can be analyzed based on the situation of a single quadrant, and the reason why the absolute value of the deviation value is added is to unify the calculation formula. In fact, it is equivalent to calculate the formula according to the walking direction.

Note:
 For computer screens, the upper left corner is usually used as the coordinate starting point O, and the horizontal and vertical directions are used as the x-axis and y-axis, and the coordinates are all positive numbers. So in fact, there are no quadrants that can be divided, only directions can be divided. The four quadrants in the book are actually the four directions (upper left, upper right, lower left, right) of the end point (x2, y2) relative to the starting point (x1, y1). lower corner), that is, discuss the four directions of the end point relative to the starting point. (Because the direction is different and the rules are different, so discuss it separately)
 From the above, when the end point is in a different direction relative to the starting point, the straight line generated from the starting point to the end point follows different rules. The rules are shown in Table 4.1 in the above figure.

(2) Popularization of derivation

 The book discusses the process of generating a straight line from O to A with the origin O of the coordinate system as the starting point, and specifically discusses the change process of the generated point M (it can be seen as from M to A, but the two-dimensional image on the raster screen The positions are all integers, so may be a close position). But when the computer needs to generate a straight line, most of the time the starting point of the straight line is not at the origin of the coordinate system (or the origin of the screen, that is, the upper left corner of the screen), what should we do at this time?
AltAlt
 The process of generating a straight line from any point (x1, y1) to (x2, y2) is derived as above. It can be known that when in the first quadrant (when the end point is in the upper right corner relative to the starting point), if my✖dx-mx✖dy<0, then ym++, my'=my +1. Let F=my✖dx-mx✖dy, then F'=my'✖dx-mx✖dy=(my +1)✖dx-mx✖dy=F+dx. Therefore, for any starting point and end point, the recursive formula and walking rules can also be derived.

(3) End point judgment

insert image description here

(4) Code display

The code for generating a straight line by the point-by-point comparison method is shown below:

#include<stdlib.h>
#include<stdio.h>
#include<graphics.h>
#include<string.h>
#include<iostream>
using namespace std;
void cb_line(int x1,int y1,int x2,int y2)
{
    
    
	int dx, dy, n, k, i, f;
	int x, y;
	dx = abs(x2 - x1);
	dy = abs(y2 - y1);
	n = dx + dy;
	if (x2 >= x1)
		k = (y2 >= y1 ? 1 : 4);
	else
		k = (y2 >= y1 ? 2 : 3);
	x = x1;
	y = y1;
	putpixel(x, y, RED);
	for (i = 0, f = 0; i < n; i++)
	{
    
    
		if (f >= 0) {
    
    
			switch (k)
			{
    
    
			case 1:
				putpixel(++x, y, RED);
				f -= dy;
				break;
			case 3:
				putpixel(--x, y, RED);
				f -= dx;
				break;
			case 2:
				putpixel(x, ++y, RED);
				f -= dx;
				break;
			case 4:
				putpixel(x, --y, RED);
				f -= dx;
				break;
			}
		}
		else
		{
    
    
			switch (k)
			{
    
    
			case 1:
				putpixel(x, ++y, RED);
				f += dx;
				break;
			case 3:
				putpixel(x, --y, RED);
				f += dx;
				break;
			case 2:
				putpixel(--x, y, RED);
				f += dy;
				break;
			case 4:
				putpixel(++x, y, RED);
				f += dy;
				break;
			}
		}
	}
}
int main()
{
    
    
	initgraph(640, 480);
	cb_line(10, 10, 100, 100);
	cb_line(10, 100, 100, 10);
	getchar();
	closegraph();
	return 0;
}

 In the above code, each addition and subtraction of the state value of f is dx and dy. This is a derivation rule for any starting point and end point. What F adds and subtracts each time is not the specific x or y value, but dx or dy.

Guess you like

Origin blog.csdn.net/qq_51563654/article/details/129876766