2 * Copyright © 2023 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 #include "ao_draw_int.h"
24 * Return if the given edge is 'live' at the specified y coordinate.
25 * That means the edge spans the y value. Horizontal lines are never
29 ao_edge_live(const struct ao_coord *coords,
34 int next_edge = (edge == ncoords - 1) ? 0 : edge + 1;
35 int16_t y1 = coords[edge].y;
36 int16_t y2 = coords[next_edge].y;
39 return y2 <= y && y < y1;
41 return y1 <= y && y < y2;
45 * Compute the X coordinate for a given edge at a specified y value
48 ao_edge_x(const struct ao_coord *coords,
53 int next_edge = (edge == ncoords - 1) ? 0 : edge + 1;
54 int16_t x1 = coords[edge].x;
55 int16_t x2 = coords[next_edge].x;
56 int16_t y1 = coords[edge].y;
57 int16_t y2 = coords[next_edge].y;
60 int16_t off_y = y - y1;
62 return x1 + (off_y * dx) / dy;
71 * Find the next X/edge pair along the given scanline. If two edges
72 * have the same X value, return them in edge index order. Returns false
73 * if there are no more edges.
76 ao_next_x(const struct ao_coord *coords,
78 struct next_x *this_x,
82 int16_t next_x = INT16_MAX;
83 uint16_t next_edge = UINT16_MAX;
86 for (edge = 0; edge < ncoords; edge++) {
87 if (ao_edge_live(coords, ncoords, edge, y)) {
88 int16_t nx = ao_edge_x(coords, ncoords, edge, y);
89 if (this_x->x < nx || (this_x->x == nx && this_x->edge < edge)) {
99 this_x->edge = next_edge;
104 * Fill a span at the specified y coordinate from x1 to x2
107 ao_span(const struct ao_bitmap *dst,
114 ao_rect(dst, x1, y, x2 - x1, 1, fill, rop);
118 * Compute the 'winding' for the specified edge. Winding is simply an
119 * indication of whether the edge goes upwards or downwards
122 ao_wind(const struct ao_coord *coords,
126 uint16_t next_edge = (edge == ncoords - 1) ? 0 : edge + 1;
127 return coords[edge].y > coords[next_edge].y ? 1 : -1;
131 * Fill the specified polygon with non-zero winding rule
134 ao_poly(const struct ao_bitmap *dst,
135 const struct ao_coord *coords,
140 int16_t y_min, y_max;
144 struct next_x next_x;
148 * Find the y limits of the polygon
150 y_min = y_max = coords[0].y;
151 for (edge = 1; edge < ncoords; edge++) {
160 * Walk each scanline in the range and fill included spans
162 for (y = y_min; y < y_max; y++) {
164 next_x.x = INT16_MIN;
167 while (ao_next_x(coords, ncoords, &next_x, y)) {
170 * Fill the previous span if winding is
174 ao_span(dst, x, next_x.x, y, fill, rop);
176 /* Adjust winding for the current span */
177 wind += ao_wind(coords, ncoords, next_x.edge);
179 /* Step to next span start x value */