-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathtcod.c
More file actions
32 lines (27 loc) · 1.02 KB
/
tcod.c
File metadata and controls
32 lines (27 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "tcod.h"
#include <stdlib.h>
#include "../libtcod/src/libtcod/bresenham.h"
#include "../libtcod/src/libtcod/console_drawing.h"
#include "../libtcod/src/libtcod/console_printing.h"
#include "../libtcod/src/libtcod/error.h"
#include "../libtcod/src/libtcod/utility.h"
/**
Write a Bresenham line to the `out[n * 2]` array.
The result includes both endpoints.
The length of the array is returned, when `out` is given `n` must be equal
or greater then the length.
*/
int bresenham(int x1, int y1, int x2, int y2, int n, int* __restrict out) {
// Bresenham length is Chebyshev distance.
int length = MAX(abs(x1 - x2), abs(y1 - y2)) + 1;
if (!out) { return length; }
if (n < length) { return TCOD_set_errorv("Bresenham output length mismatched."); }
TCOD_bresenham_data_t bresenham;
out[0] = x1;
out[1] = y1;
out += 2;
if (x1 == x2 && y1 == y2) { return length; }
TCOD_line_init_mt(x1, y1, x2, y2, &bresenham);
while (!TCOD_line_step_mt(&out[0], &out[1], &bresenham)) { out += 2; }
return length;
}