Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions docs/polygon.rst
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ Polygon Methods

.. ## Polygon.collidepoint ##

.. method:: as_segments

| :sl:`returns the line segments of the polygon`
| :sg:`as_segments() -> list[Line]`

Returns a list of the line segments of the polygon given as self.

.. ## Polygon.as_segments ##

.. method:: copy

| :sl:`returns a copy of the polygon`
Expand Down
2 changes: 2 additions & 0 deletions geometry.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ class Polygon:
@overload
def move(self, move_by: Coordinate) -> Polygon: ...
@overload
def as_segments(self) -> List[Line]: ...
Comment on lines 210 to +211
Copy link
Member

@itzpr3d4t0r itzpr3d4t0r Jan 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need @overload here, there's just one way to call this function

@overload
def move_ip(self, x: float, y: float) -> None: ...
@overload
def move_ip(self, move_by: Coordinate) -> None: ...
Expand Down
1 change: 1 addition & 0 deletions src_c/geometry.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,4 @@ MODINIT_DEFINE(geometry)
}
return module;
}

30 changes: 29 additions & 1 deletion src_c/polygon.c
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,33 @@ pg_polygon_move(pgPolygonObject *self, PyObject *const *args, Py_ssize_t nargs)
return (PyObject *)ret;
}

static PyObject *
pg_polygon_as_segments(pgPolygonObject *self, PyObject *_null) {
double *vertices = self->polygon.vertices;
Py_ssize_t verts_num = self->polygon.verts_num;
Py_ssize_t verts_num_double = verts_num * 2;

PyObject *list = PyList_New(verts_num);
if (!list) {
return NULL;
}

for (Py_ssize_t i = 0; i < verts_num; i++) {
Py_ssize_t i2 = i * 2;
PyObject *line = pgLine_New4(vertices[i2], vertices[i2 + 1],
vertices[(i2 + 2) % verts_num_double],
vertices[(i2 + 3) % verts_num_double]);
if (!line) {
Py_DECREF(list);
return NULL;
}

PyList_SET_ITEM(list, i, line);
}

return list;
}

static PyObject *
pg_polygon_move_ip(pgPolygonObject *self, PyObject *const *args,
Py_ssize_t nargs)
Expand Down Expand Up @@ -987,6 +1014,7 @@ pg_polygon_is_convex(pgPolygonObject *self, PyObject *_null)
}

static struct PyMethodDef pg_polygon_methods[] = {
{"as_segments", (PyCFunction)pg_polygon_as_segments, METH_NOARGS, NULL},
{"move", (PyCFunction)pg_polygon_move, METH_FASTCALL, NULL},
{"move_ip", (PyCFunction)pg_polygon_move_ip, METH_FASTCALL, NULL},
{"rotate", (PyCFunction)pg_polygon_rotate, METH_O, NULL},
Expand Down Expand Up @@ -1243,4 +1271,4 @@ static PyTypeObject pgPolygon_Type = {
.tp_getset = pg_polygon_getsets,
.tp_init = (initproc)pg_polygon_init,
.tp_new = pg_polygon_new,
};
};
29 changes: 28 additions & 1 deletion test/test_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pygame import Vector2, Vector3, Rect

import geometry
from geometry import Polygon
from geometry import Polygon, Line

import math

Expand Down Expand Up @@ -582,6 +582,33 @@ def test__repr__(self):
self.assertEqual(repr(polygon), p_repr)
self.assertEqual(polygon.__repr__(), p_repr)

def test_as_segments(self):
"""Checks whether polygon segments are correct"""
poly = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
self.assertEqual(
poly.as_segments(),
[
Line((0, 0), (1, 0)),
Line((1, 0), (1, 1)),
Line((1, 1), (0, 1)),
Line((0, 1), (0, 0)),
],
)
poly = Polygon([(123.23, 35.6), (56.4, 87.45), (43.1, 12.3)])
self.assertEqual(
poly.as_segments(),
[
Line((123.23, 35.6), (56.4, 87.45)),
Line((56.4, 87.45), (43.1, 12.3)),
Line((43.1, 12.3), (123.23, 35.6)),
],
)
poly = Polygon([[1, 2], [3, 4], [5, 6]])
self.assertEqual(
poly.as_segments(),
[Line((1, 2), (3, 4)), Line((3, 4), (5, 6)), Line((5, 6), (1, 2))],
)
Comment on lines +585 to +610
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add more tests for invalid argument types and argument number.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically you should make sure that this is actually a no arguments method now and in the future


def test_move_xy(self):
"""Checks whether polygon move function works correctly with an x-y pair."""
poly = Polygon(_some_vertices.copy())
Expand Down