Skip to content

Commit 19fea51

Browse files
authored
Merge pull request #3192 from pygame-community/revert-3140-draw.aaline_width
Revert 3140 draw.aaline width
2 parents 1b52476 + 5ddc43c commit 19fea51

File tree

7 files changed

+105
-217
lines changed

7 files changed

+105
-217
lines changed

buildconfig/stubs/pygame/draw.pyi

-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ def aaline(
8282
color: ColorLike,
8383
start_pos: Point,
8484
end_pos: Point,
85-
width: int = 1,
8685
) -> Rect: ...
8786
def aalines(
8887
surface: Surface,
Loading

docs/reST/ref/code_examples/draw_module_example.py

-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@
3030
# 5 pixels wide. Uses (r, g, b) color - medium sea green.
3131
pygame.draw.line(screen, (60, 179, 113), [0, 0], [50, 30], 5)
3232

33-
# Draw on the screen a green antialiased line from (0, 25) to (50, 55)
34-
# 5 pixels wide. Uses (r, g, b) color - medium sea green.
35-
pygame.draw.aaline(screen, (60, 179, 113), [0, 25], [50, 55], 5)
36-
3733
# Draw on the screen a green line from (0, 50) to (50, 80)
3834
# Because it is an antialiased line, it is 1 pixel wide.
3935
# Uses (r, g, b) color - medium sea green.

docs/reST/ref/draw.rst

+65-9
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,72 @@ object around the draw calls (see :func:`pygame.Surface.lock` and
451451

452452
| :sl:`draw a straight antialiased line`
453453
| :sg:`aaline(surface, color, start_pos, end_pos) -> Rect`
454-
:sg:`aaline(surface, color, start_pos, end_pos, width=1) -> Rect`
455454
456-
Draws a straight antialiased line on the given surface. There are no endcaps.
457-
For thick lines the ends are squared off.
455+
Draws a straight antialiased line on the given surface.
456+
457+
The line has a thickness of one pixel and the endpoints have a height and
458+
width of one pixel each.
459+
460+
The way a line and its endpoints are drawn:
461+
If both endpoints are equal, only a single pixel is drawn (after
462+
rounding floats to nearest integer).
463+
464+
Otherwise if the line is not steep (i.e. if the length along the x-axis
465+
is greater than the height along the y-axis):
466+
467+
For each endpoint:
468+
469+
If ``x``, the endpoint's x-coordinate, is a whole number find
470+
which pixels would be covered by it and draw them.
471+
472+
Otherwise:
473+
474+
Calculate the position of the nearest point with a whole number
475+
for its x-coordinate, when extending the line past the
476+
endpoint.
477+
478+
Find which pixels would be covered and how much by that point.
479+
480+
If the endpoint is the left one, multiply the coverage by (1 -
481+
the decimal part of ``x``).
482+
483+
Otherwise multiply the coverage by the decimal part of ``x``.
484+
485+
Then draw those pixels.
486+
487+
*e.g.:*
488+
| The left endpoint of the line ``((1, 1.3), (5, 3))`` would
489+
cover 70% of the pixel ``(1, 1)`` and 30% of the pixel
490+
``(1, 2)`` while the right one would cover 100% of the
491+
pixel ``(5, 3)``.
492+
| The left endpoint of the line ``((1.2, 1.4), (4.6, 3.1))``
493+
would cover 56% *(i.e. 0.8 * 70%)* of the pixel ``(1, 1)``
494+
and 24% *(i.e. 0.8 * 30%)* of the pixel ``(1, 2)`` while
495+
the right one would cover 42% *(i.e. 0.6 * 70%)* of the
496+
pixel ``(5, 3)`` and 18% *(i.e. 0.6 * 30%)* of the pixel
497+
``(5, 4)`` while the right
498+
499+
Then for each point between the endpoints, along the line, whose
500+
x-coordinate is a whole number:
501+
502+
Find which pixels would be covered and how much by that point and
503+
draw them.
504+
505+
*e.g.:*
506+
| The points along the line ``((1, 1), (4, 2.5))`` would be
507+
``(2, 1.5)`` and ``(3, 2)`` and would cover 50% of the pixel
508+
``(2, 1)``, 50% of the pixel ``(2, 2)`` and 100% of the pixel
509+
``(3, 2)``.
510+
| The points along the line ``((1.2, 1.4), (4.6, 3.1))`` would
511+
be ``(2, 1.8)`` (covering 20% of the pixel ``(2, 1)`` and 80%
512+
of the pixel ``(2, 2)``), ``(3, 2.3)`` (covering 70% of the
513+
pixel ``(3, 2)`` and 30% of the pixel ``(3, 3)``) and ``(4,
514+
2.8)`` (covering 20% of the pixel ``(2, 1)`` and 80% of the
515+
pixel ``(2, 2)``)
516+
517+
Otherwise do the same for steep lines as for non-steep lines except
518+
along the y-axis instead of the x-axis (using ``y`` instead of ``x``,
519+
top instead of left and bottom instead of right).
458520

459521
.. note::
460522
Regarding float values for coordinates, a point with coordinate
@@ -476,11 +538,6 @@ object around the draw calls (see :func:`pygame.Surface.lock` and
476538
:param end_pos: end position of the line, (x, y)
477539
:type end_pos: tuple(int or float, int or float) or
478540
list(int or float, int or float) or Vector2(int or float, int or float)
479-
:param int width: (optional) used for line thickness
480-
481-
| if width >= 1, used for line thickness (default is 1)
482-
| if width < 1, nothing will be drawn
483-
|
484541

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

498554
.. ## pygame.draw.aaline ##
499555

src_c/doc/draw_doc.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
#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"
99
#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"
1010
#define DOC_DRAW_LINES "lines(surface, color, closed, points) -> Rect\nlines(surface, color, closed, points, width=1) -> Rect\ndraw multiple contiguous straight line segments"
11-
#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"
11+
#define DOC_DRAW_AALINE "aaline(surface, color, start_pos, end_pos) -> Rect\ndraw a straight antialiased line"
1212
#define DOC_DRAW_AALINES "aalines(surface, color, closed, points) -> Rect\ndraw multiple contiguous straight antialiased line segments"

src_c/draw.c

+5-59
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ draw_line_width(SDL_Surface *surf, Uint32 color, int x1, int y1, int x2,
4444
static void
4545
draw_line(SDL_Surface *surf, int x1, int y1, int x2, int y2, Uint32 color,
4646
int *drawn_area);
47-
void
48-
line_width_corners(float from_x, float from_y, float to_x, float to_y,
49-
int width, float *x1, float *y1, float *x2, float *y2,
50-
float *x3, float *y3, float *x4, float *y4);
5147
static void
5248
draw_aaline(SDL_Surface *surf, Uint32 color, float startx, float starty,
5349
float endx, float endy, int *drawn_area,
@@ -119,17 +115,16 @@ aaline(PyObject *self, PyObject *arg, PyObject *kwargs)
119115
PyObject *colorobj, *start, *end;
120116
SDL_Surface *surf = NULL;
121117
float startx, starty, endx, endy;
122-
int width = 1; /* Default width. */
123118
PyObject *blend = NULL;
124119
int drawn_area[4] = {INT_MAX, INT_MAX, INT_MIN,
125120
INT_MIN}; /* Used to store bounding box values */
126121
Uint32 color;
127-
static char *keywords[] = {"surface", "color", "start_pos", "end_pos",
128-
"width", "blend", NULL};
122+
static char *keywords[] = {"surface", "color", "start_pos",
123+
"end_pos", "blend", NULL};
129124

130-
if (!PyArg_ParseTupleAndKeywords(arg, kwargs, "O!OOO|iO", keywords,
125+
if (!PyArg_ParseTupleAndKeywords(arg, kwargs, "O!OOO|O", keywords,
131126
&pgSurface_Type, &surfobj, &colorobj,
132-
&start, &end, &width, &blend)) {
127+
&start, &end, &blend)) {
133128
return NULL; /* Exception already set. */
134129
}
135130

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

165-
if (width < 1) {
166-
return pgRect_New4((int)startx, (int)starty, 0, 0);
167-
}
168-
169160
if (!pgSurface_Lock(surfobj)) {
170161
return RAISE(PyExc_RuntimeError, "error locking surface");
171162
}
172163

173-
if (width > 1) {
174-
float x1, y1, x2, y2, x3, y3, x4, y4;
175-
line_width_corners(startx, starty, endx, endy, width, &x1, &y1, &x2,
176-
&y2, &x3, &y3, &x4, &y4);
177-
draw_line_width(surf, color, (int)startx, (int)starty, (int)endx,
178-
(int)endy, width, drawn_area);
179-
draw_aaline(surf, color, x1, y1, x2, y2, drawn_area, 0, 0, 0);
180-
draw_aaline(surf, color, x3, y3, x4, y4, drawn_area, 0, 0, 0);
181-
}
182-
else {
183-
draw_aaline(surf, color, startx, starty, endx, endy, drawn_area, 0, 0,
184-
0);
185-
}
164+
draw_aaline(surf, color, startx, starty, endx, endy, drawn_area, 0, 0, 0);
186165

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

1869-
// Calculates 4 points, representing corners of draw_line_width()
1870-
// first two points assemble left line and second two - right line
1871-
void
1872-
line_width_corners(float from_x, float from_y, float to_x, float to_y,
1873-
int width, float *x1, float *y1, float *x2, float *y2,
1874-
float *x3, float *y3, float *x4, float *y4)
1875-
{
1876-
float aa_width = (float)width / 2;
1877-
float extra_width = (1.0f - (width % 2)) / 2;
1878-
int steep = fabs(to_x - from_x) <= fabs(to_y - from_y);
1879-
1880-
if (steep) {
1881-
*x1 = from_x + extra_width + aa_width;
1882-
*y1 = from_y;
1883-
*x2 = to_x + extra_width + aa_width;
1884-
*y2 = to_y;
1885-
*x3 = from_x + extra_width - aa_width;
1886-
*y3 = from_y;
1887-
*x4 = to_x + extra_width - aa_width;
1888-
*y4 = to_y;
1889-
}
1890-
else {
1891-
*x1 = from_x;
1892-
*y1 = from_y + extra_width + aa_width;
1893-
*x2 = to_x;
1894-
*y2 = to_y + extra_width + aa_width;
1895-
*x3 = from_x;
1896-
*y3 = from_y + extra_width - aa_width;
1897-
*x4 = to_x;
1898-
*y4 = to_y + extra_width - aa_width;
1899-
}
1900-
}
1901-
19021848
/* Algorithm modified from
19031849
* https://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm
19041850
*/

0 commit comments

Comments
 (0)