yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
paint_util.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EDITOR_DUNGEON_INTERACTION_PAINT_UTIL_H_
2#define YAZE_APP_EDITOR_DUNGEON_INTERACTION_PAINT_UTIL_H_
3
4#include <algorithm>
5#include <cstdlib>
6#include <utility>
7
9
10// Iterates integer grid points on a line segment (inclusive endpoints).
11// Uses a classic Bresenham formulation; order is from (x0,y0) -> (x1,y1).
12template <typename Fn>
13inline void ForEachPointOnLine(int x0, int y0, int x1, int y1, Fn&& fn) {
14 int dx = std::abs(x1 - x0);
15 int sx = x0 < x1 ? 1 : -1;
16 int dy = -std::abs(y1 - y0);
17 int sy = y0 < y1 ? 1 : -1;
18 int err = dx + dy; // error value e_xy
19
20 while (true) {
21 fn(x0, y0);
22 if (x0 == x1 && y0 == y1) {
23 break;
24 }
25 const int e2 = 2 * err;
26 if (e2 >= dy) {
27 err += dy;
28 x0 += sx;
29 }
30 if (e2 <= dx) {
31 err += dx;
32 y0 += sy;
33 }
34 }
35}
36
37// Iterates integer grid points in a square brush centered at (cx,cy).
38// Bounds are inclusive.
39template <typename Fn>
40inline void ForEachPointInSquareBrush(int cx, int cy, int radius, int min_x,
41 int min_y, int max_x, int max_y, Fn&& fn) {
42 radius = std::max(0, radius);
43 const int x0 = std::max(min_x, cx - radius);
44 const int x1 = std::min(max_x, cx + radius);
45 const int y0 = std::max(min_y, cy - radius);
46 const int y1 = std::min(max_y, cy + radius);
47
48 for (int y = y0; y <= y1; ++y) {
49 for (int x = x0; x <= x1; ++x) {
50 fn(x, y);
51 }
52 }
53}
54
55} // namespace yaze::editor::paint_util
56
57#endif // YAZE_APP_EDITOR_DUNGEON_INTERACTION_PAINT_UTIL_H_
58
void ForEachPointInSquareBrush(int cx, int cy, int radius, int min_x, int min_y, int max_x, int max_y, Fn &&fn)
Definition paint_util.h:40
void ForEachPointOnLine(int x0, int y0, int x1, int y1, Fn &&fn)
Definition paint_util.h:13