tgrekov-fdf
HIVE fdf May 2024
Loading...
Searching...
No Matches
draw_line.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* draw_line.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: tgrekov <tgrekov@student.hive.fi> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/06/13 06:17:39 by tgrekov #+# #+# */
9/* Updated: 2024/06/24 06:00:19 by tgrekov ### ########.fr */
10/* */
11/* ************************************************************************** */
12
20#include <MLX42.h>
21#include "../utils/utils.h"
22
32void draw_line(mlx_image_t *img, int x1, int y1, int *p2)
33{
34 int dx;
35 int dy;
36 int err;
37 int dir[2];
38
39 dx = abs(p2[0] - x1);
40 dy = -abs(p2[1] - y1);
41 err = dx + dy;
42 dir[0] = (x1 < p2[0]) * 2 - 1;
43 dir[1] = (y1 < p2[1]) * 2 - 1;
44 while (!(err * 2 >= dy && x1 == p2[0]) && !(err * 2 <= dx && y1 == p2[1]))
45 {
46 mlx_put_pixel(img, x1, y1, 0xFF0000FF);
47 if (err * 2 >= dy)
48 {
49 err += dy;
50 x1 += dir[0];
51 }
52 if (err * 2 <= dx)
53 {
54 err += dx;
55 y1 += dir[1];
56 }
57 }
58}
int abs(int n)
Get absolute value of n.
Definition abs.c:26
void draw_line(mlx_image_t *img, int x1, int y1, int *p2)
Multi-directional implementation of the Bresenham incremental error line drawing algorithm.
Definition draw_line.c:32
void * err(const char *str, void *retval)
Wrapper around perror() that always returns retval.
Definition err.c:29