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;
		}
	}
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值