DDA画线算法

void DrawLine_DDA(HDC hdc, int x1, int y1, int x2, int y2, COLORREF crLine)
{
	if (x1 == x2)
	{
		if (y1 <= y2)
		{
			while (y1 <= y2)
				SetPixel(hdc, x1, y1++, crLine);
		}
		else
		{
			while (y2 <= y1)
				SetPixel(hdc, x1, y2++, crLine);
		}
		return;
	}
	if (y1 == y2)
	{
		if (x1 <= x2)
		{
			while (y1 <= y2)
				SetPixel(hdc, x1++, y1, crLine);
		}
		else
		{
			while (x2 <= x1)
				SetPixel(hdc, x2++, y1, crLine);
		}
		return;
	}
	double dy = y2 - y1;
	double k = dy / (x2 - x1);
	if (k >= -1 && k <= 1)
	{
		int start_x, end_x;
		double start_y;
		if (x1 >= x2)
			start_x = x2, end_x = x1, start_y = y2;
		else
			start_x = x1, end_x = x2, start_y = y1;
		while (start_x <= end_x)
		{
			SetPixel(hdc, start_x++, start_y + 0.5, crLine);
			start_y += k;
		}
	}
	else
	{
		k = 1 / k;
		int start_y, end_y;
		double start_x;
		if (y1 >= y2)
			start_y = y2, end_y = y1, start_x = x2;
		else
			start_y = y1, end_y = y2, start_x = x1;
		while (start_y <= end_y)
		{
			SetPixel(hdc, start_x+0.5, start_y++, crLine);
			start_x += k;
		}
	}
}

猜你喜欢

转载自blog.csdn.net/csdn_gddf102384398/article/details/84988412
dda