Skip to content

Commit

Permalink
Merge pull request #3192 from pygame-community/revert-3140-draw.aalin…
Browse files Browse the repository at this point in the history
…e_width

Revert 3140 draw.aaline width
  • Loading branch information
oddbookworm authored Oct 26, 2024
2 parents 1b52476 + 5ddc43c commit 19fea51
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 217 deletions.
1 change: 0 additions & 1 deletion buildconfig/stubs/pygame/draw.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ def aaline(
color: ColorLike,
start_pos: Point,
end_pos: Point,
width: int = 1,
) -> Rect: ...
def aalines(
surface: Surface,
Expand Down
Binary file modified docs/reST/ref/code_examples/draw_module_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 0 additions & 4 deletions docs/reST/ref/code_examples/draw_module_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@
# 5 pixels wide. Uses (r, g, b) color - medium sea green.
pygame.draw.line(screen, (60, 179, 113), [0, 0], [50, 30], 5)

# Draw on the screen a green antialiased line from (0, 25) to (50, 55)
# 5 pixels wide. Uses (r, g, b) color - medium sea green.
pygame.draw.aaline(screen, (60, 179, 113), [0, 25], [50, 55], 5)

# Draw on the screen a green line from (0, 50) to (50, 80)
# Because it is an antialiased line, it is 1 pixel wide.
# Uses (r, g, b) color - medium sea green.
Expand Down
74 changes: 65 additions & 9 deletions docs/reST/ref/draw.rst
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,72 @@ object around the draw calls (see :func:`pygame.Surface.lock` and

| :sl:`draw a straight antialiased line`
| :sg:`aaline(surface, color, start_pos, end_pos) -> Rect`
:sg:`aaline(surface, color, start_pos, end_pos, width=1) -> Rect`
Draws a straight antialiased line on the given surface. There are no endcaps.
For thick lines the ends are squared off.
Draws a straight antialiased line on the given surface.

The line has a thickness of one pixel and the endpoints have a height and
width of one pixel each.

The way a line and its endpoints are drawn:
If both endpoints are equal, only a single pixel is drawn (after
rounding floats to nearest integer).

Otherwise if the line is not steep (i.e. if the length along the x-axis
is greater than the height along the y-axis):

For each endpoint:

If ``x``, the endpoint's x-coordinate, is a whole number find
which pixels would be covered by it and draw them.

Otherwise:

Calculate the position of the nearest point with a whole number
for its x-coordinate, when extending the line past the
endpoint.

Find which pixels would be covered and how much by that point.

If the endpoint is the left one, multiply the coverage by (1 -
the decimal part of ``x``).

Otherwise multiply the coverage by the decimal part of ``x``.

Then draw those pixels.

*e.g.:*
| The left endpoint of the line ``((1, 1.3), (5, 3))`` would
cover 70% of the pixel ``(1, 1)`` and 30% of the pixel
``(1, 2)`` while the right one would cover 100% of the
pixel ``(5, 3)``.
| The left endpoint of the line ``((1.2, 1.4), (4.6, 3.1))``
would cover 56% *(i.e. 0.8 * 70%)* of the pixel ``(1, 1)``
and 24% *(i.e. 0.8 * 30%)* of the pixel ``(1, 2)`` while
the right one would cover 42% *(i.e. 0.6 * 70%)* of the
pixel ``(5, 3)`` and 18% *(i.e. 0.6 * 30%)* of the pixel
``(5, 4)`` while the right
Then for each point between the endpoints, along the line, whose
x-coordinate is a whole number:

Find which pixels would be covered and how much by that point and
draw them.

*e.g.:*
| The points along the line ``((1, 1), (4, 2.5))`` would be
``(2, 1.5)`` and ``(3, 2)`` and would cover 50% of the pixel
``(2, 1)``, 50% of the pixel ``(2, 2)`` and 100% of the pixel
``(3, 2)``.
| The points along the line ``((1.2, 1.4), (4.6, 3.1))`` would
be ``(2, 1.8)`` (covering 20% of the pixel ``(2, 1)`` and 80%
of the pixel ``(2, 2)``), ``(3, 2.3)`` (covering 70% of the
pixel ``(3, 2)`` and 30% of the pixel ``(3, 3)``) and ``(4,
2.8)`` (covering 20% of the pixel ``(2, 1)`` and 80% of the
pixel ``(2, 2)``)
Otherwise do the same for steep lines as for non-steep lines except
along the y-axis instead of the x-axis (using ``y`` instead of ``x``,
top instead of left and bottom instead of right).

.. note::
Regarding float values for coordinates, a point with coordinate
Expand All @@ -476,11 +538,6 @@ object around the draw calls (see :func:`pygame.Surface.lock` and
:param end_pos: end position of the line, (x, y)
:type end_pos: tuple(int or float, int or float) or
list(int or float, int or float) or Vector2(int or float, int or float)
:param int width: (optional) used for line thickness

| if width >= 1, used for line thickness (default is 1)
| if width < 1, nothing will be drawn
|

:returns: a rect bounding the changed pixels, if nothing is drawn the
bounding rect's position will be the ``start_pos`` parameter value (float
Expand All @@ -493,7 +550,6 @@ object around the draw calls (see :func:`pygame.Surface.lock` and
.. versionchangedold:: 2.0.0 Added support for keyword arguments.
.. versionchanged:: 2.4.0 Removed deprecated 'blend' argument
.. versionchanged:: 2.5.0 ``blend`` argument readded for backcompat, but will always raise a deprecation exception when used
.. versionchanged:: 2.5.2 Added line width

.. ## pygame.draw.aaline ##
Expand Down
2 changes: 1 addition & 1 deletion src_c/doc/draw_doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
#define DOC_DRAW_ARC "arc(surface, color, rect, start_angle, stop_angle) -> Rect\narc(surface, color, rect, start_angle, stop_angle, width=1) -> Rect\ndraw an elliptical arc"
#define DOC_DRAW_LINE "line(surface, color, start_pos, end_pos) -> Rect\nline(surface, color, start_pos, end_pos, width=1) -> Rect\ndraw a straight line"
#define DOC_DRAW_LINES "lines(surface, color, closed, points) -> Rect\nlines(surface, color, closed, points, width=1) -> Rect\ndraw multiple contiguous straight line segments"
#define DOC_DRAW_AALINE "aaline(surface, color, start_pos, end_pos) -> Rect\naaline(surface, color, start_pos, end_pos, width=1) -> Rect\ndraw a straight antialiased line"
#define DOC_DRAW_AALINE "aaline(surface, color, start_pos, end_pos) -> Rect\ndraw a straight antialiased line"
#define DOC_DRAW_AALINES "aalines(surface, color, closed, points) -> Rect\ndraw multiple contiguous straight antialiased line segments"
64 changes: 5 additions & 59 deletions src_c/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ draw_line_width(SDL_Surface *surf, Uint32 color, int x1, int y1, int x2,
static void
draw_line(SDL_Surface *surf, int x1, int y1, int x2, int y2, Uint32 color,
int *drawn_area);
void
line_width_corners(float from_x, float from_y, float to_x, float to_y,
int width, float *x1, float *y1, float *x2, float *y2,
float *x3, float *y3, float *x4, float *y4);
static void
draw_aaline(SDL_Surface *surf, Uint32 color, float startx, float starty,
float endx, float endy, int *drawn_area,
Expand Down Expand Up @@ -119,17 +115,16 @@ aaline(PyObject *self, PyObject *arg, PyObject *kwargs)
PyObject *colorobj, *start, *end;
SDL_Surface *surf = NULL;
float startx, starty, endx, endy;
int width = 1; /* Default width. */
PyObject *blend = NULL;
int drawn_area[4] = {INT_MAX, INT_MAX, INT_MIN,
INT_MIN}; /* Used to store bounding box values */
Uint32 color;
static char *keywords[] = {"surface", "color", "start_pos", "end_pos",
"width", "blend", NULL};
static char *keywords[] = {"surface", "color", "start_pos",
"end_pos", "blend", NULL};

if (!PyArg_ParseTupleAndKeywords(arg, kwargs, "O!OOO|iO", keywords,
if (!PyArg_ParseTupleAndKeywords(arg, kwargs, "O!OOO|O", keywords,
&pgSurface_Type, &surfobj, &colorobj,
&start, &end, &width, &blend)) {
&start, &end, &blend)) {
return NULL; /* Exception already set. */
}

Expand Down Expand Up @@ -162,27 +157,11 @@ aaline(PyObject *self, PyObject *arg, PyObject *kwargs)
return RAISE(PyExc_TypeError, "invalid end_pos argument");
}

if (width < 1) {
return pgRect_New4((int)startx, (int)starty, 0, 0);
}

if (!pgSurface_Lock(surfobj)) {
return RAISE(PyExc_RuntimeError, "error locking surface");
}

if (width > 1) {
float x1, y1, x2, y2, x3, y3, x4, y4;
line_width_corners(startx, starty, endx, endy, width, &x1, &y1, &x2,
&y2, &x3, &y3, &x4, &y4);
draw_line_width(surf, color, (int)startx, (int)starty, (int)endx,
(int)endy, width, drawn_area);
draw_aaline(surf, color, x1, y1, x2, y2, drawn_area, 0, 0, 0);
draw_aaline(surf, color, x3, y3, x4, y4, drawn_area, 0, 0, 0);
}
else {
draw_aaline(surf, color, startx, starty, endx, endy, drawn_area, 0, 0,
0);
}
draw_aaline(surf, color, startx, starty, endx, endy, drawn_area, 0, 0, 0);

if (!pgSurface_Unlock(surfobj)) {
return RAISE(PyExc_RuntimeError, "error unlocking surface");
Expand Down Expand Up @@ -1866,39 +1845,6 @@ draw_line_width(SDL_Surface *surf, Uint32 color, int x1, int y1, int x2,
}
}

// Calculates 4 points, representing corners of draw_line_width()
// first two points assemble left line and second two - right line
void
line_width_corners(float from_x, float from_y, float to_x, float to_y,
int width, float *x1, float *y1, float *x2, float *y2,
float *x3, float *y3, float *x4, float *y4)
{
float aa_width = (float)width / 2;
float extra_width = (1.0f - (width % 2)) / 2;
int steep = fabs(to_x - from_x) <= fabs(to_y - from_y);

if (steep) {
*x1 = from_x + extra_width + aa_width;
*y1 = from_y;
*x2 = to_x + extra_width + aa_width;
*y2 = to_y;
*x3 = from_x + extra_width - aa_width;
*y3 = from_y;
*x4 = to_x + extra_width - aa_width;
*y4 = to_y;
}
else {
*x1 = from_x;
*y1 = from_y + extra_width + aa_width;
*x2 = to_x;
*y2 = to_y + extra_width + aa_width;
*x3 = from_x;
*y3 = from_y + extra_width - aa_width;
*x4 = to_x;
*y4 = to_y + extra_width - aa_width;
}
}

/* Algorithm modified from
* https://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm
*/
Expand Down
Loading

0 comments on commit 19fea51

Please sign in to comment.