diff --git a/Makefile b/Makefile index c5aa78b..f48547a 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ MLX42 = $(MLX42_PATH)/build/libmlx42.a OBJ_PATH = obj VPATH = src -SOURCES = fdf.c fdf_rotations.c +SOURCES = fdf.c fdf_rotations.c fdf_draw.c OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) diff --git a/src/fdf_draw.c b/src/fdf_draw.c new file mode 100644 index 0000000..62b34e8 --- /dev/null +++ b/src/fdf_draw.c @@ -0,0 +1,99 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* fdf_draw.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/08 11:45:12 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/08 11:45:12 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ +#include "fdf.h" + +bool fdf_put_pixel(mlx_image_t *img, t_point_2d point) +{ + if (point.x < 0 || point.x >= (int) img->width + || point.y < 0 || point.y >= (int) img->height) + return (false); + mlx_put_pixel(img, point.x, point.y, point.color); + return (true); +} + +void fdf_draw_line_low(mlx_image_t *img, 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 = end.y - start.y; + yi = 1; + if (delta_point.y < 0) + { + yi = -1; + delta_point.y = -delta_point.y; + } + delta = 2 * delta_point.y - delta_point.x; + current = start; + while (current.x <= end.x) + { + fdf_put_pixel(img, current); + if (delta > 0) + { + current.y += yi; + delta -= 2 * delta_point.x; + } + delta += 2 * delta_point.y; + current.x++; + } +} + +void fdf_draw_line_high(mlx_image_t *img, t_point_2d start, t_point_2d end) +{ + int delta; + int xi; + t_point_2d current; + t_point_2d delta_point; + + delta_point.x = end.x - start.x; + delta_point.y = end.y - start.y; + xi = 1; + if (delta_point.x < 0) + { + xi = -1; + delta_point.x = -delta_point.x; + } + delta = 2 * delta_point.x - delta_point.y; + current = start; + while (current.y <= end.y) + { + fdf_put_pixel(img, current); + if (delta > 0) + { + current.x += xi; + delta -= 2 * delta_point.y; + } + delta += 2 * delta_point.x; + current.y++; + } +} + +void fdf_draw_line(mlx_image_t *img, t_point_2d start, t_point_2d end) +{ + if (abs(end.y - start.y) < abs(end.x - start.x)) + { + if (start.x > end.x) + fdf_draw_line_low(img, end, start); + else + fdf_draw_line_low(img, start, end); + } + else + { + if (start.y > end.y) + fdf_draw_line_high(img, end, start); + else + fdf_draw_line_high(img, start, end); + } +}