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;
		}
	}
}