Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add draw.aapolygon #3126

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion src_c/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ draw_fillpoly_for_aapolygon(SDL_Surface *surf, int *vx, int *vy, Py_ssize_t n,
static int
draw_filltri(SDL_Surface *surf, int *xlist, int *ylist, Uint32 color,
int *drawn_area);
static int
draw_filltri_for_aapolygon(SDL_Surface *surf, int *xlist, int *ylist,
Uint32 color, int *drawn_area);
static void
draw_rect(SDL_Surface *surf, int x1, int y1, int x2, int y2, int width,
Uint32 color);
Expand Down Expand Up @@ -1082,7 +1085,7 @@ aapolygon(PyObject *self, PyObject *arg, PyObject *kwargs)
drawn_area);
}
else {
draw_filltri(surf, xlist, ylist, color, drawn_area);
draw_filltri_for_aapolygon(surf, xlist, ylist, color, drawn_area);
mzivic7 marked this conversation as resolved.
Show resolved Hide resolved
}
PyMem_Free(xlist);
PyMem_Free(ylist);
Expand Down Expand Up @@ -1780,6 +1783,64 @@ draw_filltri(SDL_Surface *surf, int *xlist, int *ylist, Uint32 color,
return 0;
}

/* This is same algorythm as draw_filltri but modified for filled
* draw.aapolygon so borders of fillpoly are not covering outer
* antialiased pixels from draw.aalines
*/
static int
draw_filltri_for_aapolygon(SDL_Surface *surf, int *xlist, int *ylist,
Uint32 color, int *draw_area)
{
int p0x, p0y, p1x, p1y, p2x, p2y;

p0x = xlist[0];
p1x = xlist[1];
p2x = xlist[2];
p0y = ylist[0];
p1y = ylist[1];
p2y = ylist[2];

if (p1y < p0y) {
swap_coordinates(&p1x, &p1y, &p0x, &p0y);
}

if (p2y < p1y) {
swap_coordinates(&p1x, &p1y, &p2x, &p2y);

if (p1y < p0y) {
swap_coordinates(&p1x, &p1y, &p0x, &p0y);
}
}

if ((p0y == p1y) && (p1y == p2y) && (p0x == p1x) && (p1x != p2x)) {
swap_coordinates(&p1x, &p1y, &p2x, &p2y);
}

float d1 = (float)((p2x - p0x) / ((p2y - p0y) + 1e-17));
float d2 = (float)((p1x - p0x) / ((p1y - p0y) + 1e-17));
float d3 = (float)((p2x - p1x) / ((p2y - p1y) + 1e-17));
mzivic7 marked this conversation as resolved.
Show resolved Hide resolved
int y;
for (y = p0y; y <= p2y; y++) {
int x1 = p0x + (int)((y - p0y) * d1) + 1;

int x2;
if (y < p1y)
x2 = p0x + (int)((y - p0y) * d2);
else
x2 = p1x + (int)((y - p1y) * d3) - 1;
if (x1 > x2) {
if (x1 - x2 != 1) {
set_and_check_rect(surf, x1 - 1, y, color, draw_area);
}
}
else {
drawhorzlineclipbounding(surf, color, x1, y, x2, draw_area);
}
}

return 0;
}

static void
draw_line_width(SDL_Surface *surf, Uint32 color, int x1, int y1, int x2,
int y2, int width, int *drawn_area)
Expand Down
Loading