Implement linedrawing algo

This commit is contained in:
whaffman 2024-12-08 13:18:56 +01:00
parent be46b3ccf9
commit 33f398bd60
2 changed files with 100 additions and 1 deletions

View File

@ -27,7 +27,7 @@ MLX42 = $(MLX42_PATH)/build/libmlx42.a
OBJ_PATH = obj OBJ_PATH = obj
VPATH = src VPATH = src
SOURCES = fdf.c fdf_rotations.c SOURCES = fdf.c fdf_rotations.c fdf_draw.c
OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o))

99
src/fdf_draw.c Normal file
View File

@ -0,0 +1,99 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* fdf_draw.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \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);
}
}