
#include "fdf.h"

static void	swap_z(t_pnt *p1, t_pnt *p2)
{
	float 	tmp;
	t_color c;

	tmp = p1->z;
	p1->z = p2->z;
	p2->z = tmp;
	c = p1->color;
	p1->color = p2->color;
	p2->color = c;
}

static void	swap_x_y(t_pnt *p1, t_pnt *p2)
{
	int temp;

	temp = p1->x;
	p1->x = p1->y;
	p1->y = temp;
	temp = p2->x;
	p2->x = p2->y;
	p2->y = temp;
}

static void	swap_points(t_pnt *p1, t_pnt *p2)
{
	t_pnt temp;

	temp = *p1;
	*p1 = *p2;
	*p2 = temp;

}

static void	draw_line(t_line *line, t_swap *s, t_FDF *FDF)
{
	int dx;
	int dy;
	int derr;
	double dz;

	dx = abs(line->p2.x - line->p1.x);
	dy = abs(line->p2.y - line->p1.y);
	dz = fabs(line->p2.z - line->p1.z);
	derr = 2 * dy - dx;
	while (line->p1.x < line->p2.x)
	{
		put_pixel_to_image(line, s, FDF);
		if (derr > 0)
		{
			line->p1.y += s->swap_x;
			derr -= dx;
		}
		(line->p1.x)++;
		(line->p1.z) += dz / dx;
		derr += dy;
	}
}

void	line(t_pnt p1, t_pnt p2, t_FDF *FDF)
{
	int		dx;
	int		dy;
	t_swap	swap;
	t_line	line;

	dx = abs(p2.x - p1.x);
	dy = abs(p2.y - p1.y);
	swap_init(&swap);
	if (dy > dx)
	{
		swap_x_y(&p1, &p2);
		swap.swap_coord= 1;
	}
	if (p1.x > p2.x)
		swap_points(&p1, &p2);
	if (p1.y > p2.y)
		swap.swap_x = -1;
	if (p1.z > p2.z)
	{
		swap_z(&p1, &p2);
		swap.swap_z = -1;
	}
	line.p1 = p1;
	line.p2 = p2;
	draw_line(&line, &swap, FDF);
}

