fdf/src/fdf_draw.c
2024-12-16 17:54:39 +01:00

110 lines
3.1 KiB
C

/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* fdf_draw.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/12/08 11:45:12 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/12/13 15:23:12 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "fdf.h"
bool fdf_put_pixel(t_fdf *fdf, t_point_2d point)
{
int color;
color = 0xFFFFFFFF;
if (point.x < 0 || point.x >= (int) fdf->img->width
|| point.y < 0 || point.y >= (int) fdf->img->height)
return (false);
if (fdf->colormode == COLOR_MODE_DEFAULT)
color = point.color;
else if (fdf->colormode == COLOR_MODE_Z)
color = get_z_color(point.orig_z, fdf->map->z_max);
else if (fdf->colormode == COLOR_MODE_GRADIENT)
color = get_gradient_color(point.orig_z, fdf->map->z_max);
mlx_put_pixel(fdf->img, point.x, point.y, color);
return (true);
}
void fdf_draw_line_low(t_fdf *fdf, t_point_2d start, t_point_2d end)
{
int delta;
int yi;
t_point_2d current;
t_point_2d delta_point;
delta_point.x = end.x - start.x;
delta_point.y = abs(end.y - start.y);
yi = 1;
if (end.y - start.y < 0)
yi = -1;
delta = 2 * delta_point.y - delta_point.x;
current = start;
while (current.x <= end.x)
{
current.color = interpolate_color(start, end, current);
fdf_put_pixel(fdf, current);
if (delta > 0)
{
current.y += yi;
delta -= 2 * delta_point.x;
}
delta += 2 * delta_point.y;
current.x++;
}
}
void fdf_draw_line_high(t_fdf *fdf, t_point_2d start, t_point_2d end)
{
int delta;
int xi;
t_point_2d current;
t_point_2d delta_point;
delta_point.x = abs(end.x - start.x);
delta_point.y = end.y - start.y;
xi = 1;
if (end.x - start.x < 0)
xi = -1;
delta = 2 * delta_point.x - delta_point.y;
current = start;
while (current.y <= end.y)
{
current.color = interpolate_color(start, end, current);
fdf_put_pixel(fdf, current);
if (delta > 0)
{
current.x += xi;
delta -= 2 * delta_point.y;
}
delta += 2 * delta_point.x;
current.y++;
}
}
void fdf_draw_line(t_fdf *fdf, t_point_2d start, t_point_2d end)
{
if ((start.x < 0 || start.x >= (int) fdf->img->width
|| start.y < 0 || start.y >= (int) fdf->img->height)
&& (end.x < 0 || end.x >= (int) fdf->img->width
|| end.y < 0 || end.y >= (int) fdf->img->height))
return ;
if (abs(end.y - start.y) < abs(end.x - start.x))
{
if (start.x > end.x)
fdf_draw_line_low(fdf, end, start);
else
fdf_draw_line_low(fdf, start, end);
}
else
{
if (start.y > end.y)
fdf_draw_line_high(fdf, end, start);
else
fdf_draw_line_high(fdf, start, end);
}
}