Skip to content

Commit e92113a

Browse files
authored
Clean up polycon code (#126)
* Use hagl_draw_hline_xyx() instead * Move all local variable declarations up * Add bounds check for nodes
1 parent 9334dec commit e92113a

1 file changed

Lines changed: 13 additions & 21 deletions

File tree

src/hagl_polygon.c

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
MIT License
44
5-
Copyright (c) 2018-2023 Mika Tuupola
5+
Copyright (c) 2018-2026 Mika Tuupola
66
77
Permission is hereby granted, free of charge, to any person obtaining a copy
88
of this software and associated documentation files (the "Software"), to deal
@@ -46,7 +46,7 @@ hagl_draw_polygon(void const *surface, int16_t amount, int16_t *vertices, hagl_c
4646
return;
4747
}
4848

49-
for(int16_t i = 0; i < amount - 1; i++) {
49+
for (int16_t i = 0; i < amount - 1; i++) {
5050
hagl_draw_line(
5151
surface,
5252
vertices[(i << 1 ) + 0],
@@ -66,25 +66,21 @@ hagl_draw_polygon(void const *surface, int16_t amount, int16_t *vertices, hagl_c
6666
);
6767
}
6868

69-
/* Adapted from http://alienryderflex.com/polygon_fill/ */
69+
/* Adapted from http://alienryderflex.com/polygon_fill/ */
7070
void
7171
hagl_fill_polygon(void const *_surface, int16_t amount, int16_t *vertices, hagl_color_t color)
7272
{
7373
const hagl_surface_t *surface = _surface;
7474
int16_t nodes[64];
75-
int16_t y;
75+
int16_t y, miny, maxy;
76+
float x0, y0, x1, y1;
7677

7778
if (amount < 3) {
7879
return;
7980
}
8081

81-
float x0;
82-
float y0;
83-
float x1;
84-
float y1;
85-
86-
int16_t miny = surface->height;
87-
int16_t maxy = 0;
82+
miny = surface->height;
83+
maxy = 0;
8884

8985
for (uint8_t i = 0; i < amount; i++) {
9086
if (miny > vertices[(i << 1) + 1]) {
@@ -112,15 +108,12 @@ hagl_fill_polygon(void const *_surface, int16_t amount, int16_t *vertices, hagl_
112108
(y0 < (float)y && y1 >= (float)y) ||
113109
(y1 < (float)y && y0 >= (float)y)
114110
) {
115-
nodes[count] = (int16_t)(x0 + (y - y0) / (y1 - y0) * (x1 - x0));
116-
count++;
117-
} else if (y == y0 && y == y1) {
118-
/* Draw horizontal lines */
119-
if (x0 < x1) {
120-
hagl_draw_hline(surface, x0, y0, x1 - x0 + 1, color);
121-
} else {
122-
hagl_draw_hline(surface, x1, y0, x0 - x1 + 1, color);
111+
if (count < 64) {
112+
nodes[count] = (int16_t)(x0 + (y - y0) / (y1 - y0) * (x1 - x0));
113+
count++;
123114
}
115+
} else if (y == y0 && y == y1) {
116+
hagl_draw_hline_xyx(surface, x0, y0, x1, color);
124117
}
125118
j = i;
126119
}
@@ -142,8 +135,7 @@ hagl_fill_polygon(void const *_surface, int16_t amount, int16_t *vertices, hagl_
142135

143136
/* Draw lines between nodes. */
144137
for (int16_t i = 0; i < count; i += 2) {
145-
int16_t width = nodes[i + 1] - nodes[i] + 1;
146-
hagl_draw_hline(surface, nodes[i], y, width, color);
138+
hagl_draw_hline_xyx(surface, nodes[i], y, nodes[i + 1], color);
147139
}
148140
}
149141
}

0 commit comments

Comments
 (0)