dda直线绘制算法

dda直线绘制算法,理论不好描述,这里就直接贴代码了:

/* Draw Line */
void line_dda(struct state *state, int x1, int y1, int x2, int y2)
{
	float dx, dy, k, x, y;
	int r;
	int t;
	dx = x1 - x2;
	dy = y1 - y2;
	if (abs(dx) > abs(dy))
	{
		if (x1 > x2) 
		{
			t = x1; x1 = x2; x2 = t;
			t = y1; y1 = y2; y2 = t;
		}
		k = dy / dx;
		y = y1;
		for (r = x1;  r <= x2; r++) {
			putpixel(state->screen, r, (int)(y + 0.5), COLOR_BLACK(state->screen));
			y += k;
		}
	}
	else
	{
		if (y1 > y2) 
		{
			t = x1; x1 = x2; x2 = t;
			t = y1; y1 = y2; y2 = t;
		}
		k = dx / dy;
		x = x1;
		for (r = y1;  r <= y2; r++) {
			putpixel(state->screen, (int)(x + 0.5), r, COLOR_BLACK(state->screen));
			x += k;
		}
	}
}


你可能感兴趣的:(算法,struct,float)