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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented geometry.regular_polygon #73

Merged
merged 9 commits into from Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions geometry.pyi
Expand Up @@ -180,4 +180,8 @@ class Polygon:
@overload
def __init__(self, polygon: Polygon) -> None: ...
def __copy__(self) -> "Polygon": ...
@staticmethod
def normal_polygon(
sides: int, center: Sequence[float], radius: float, angle: float = 0
itzpr3d4t0r marked this conversation as resolved.
Show resolved Hide resolved
) -> "Polygon": ...
copy = __copy__
73 changes: 73 additions & 0 deletions src_c/polygon.c
Expand Up @@ -7,6 +7,10 @@
#include <stddef.h>
#include <math.h>

#ifndef PI
#define PI 3.14159265358979323846
#endif /* ~PI */

static PyObject *
pg_tuple_from_values_double(double val1, double val2)
{
Expand Down Expand Up @@ -315,6 +319,73 @@ pg_polygon_dealloc(pgPolygonObject *self)
Py_TYPE(self)->tp_free((PyObject *)self);
}

static PyObject *
pg_polygon_normal_polygon(PyObject *_null, PyObject *const *args,
Py_ssize_t nargs)
{
int sides;
double radius;
double angle = 0;
double Cx, Cy;

if (nargs == 3) {
if (!PyLong_Check(args[0])) {
goto error;
}
sides = (int)PyLong_AsLong(args[0]);

if (!pg_TwoDoublesFromObj(args[1], &Cx, &Cy) ||
!pg_DoubleFromObj(args[2], &radius)) {
goto error;
}
}
else if (nargs == 4) {
if (!PyLong_Check(args[0])) {
goto error;
}
sides = (int)PyLong_AsLong(args[0]);

if (!pg_TwoDoublesFromObj(args[1], &Cx, &Cy) ||
!pg_DoubleFromObj(args[2], &radius) ||
!pg_DoubleFromObj(args[3], &angle)) {
goto error;
}
}
itzpr3d4t0r marked this conversation as resolved.
Show resolved Hide resolved
else {
goto error;
}

angle *= PI / 180.0;

if (sides < 3) {
if (sides < 0) {
return RAISE(PyExc_ValueError,
"the sides can not be a negative number");
}
return RAISE(PyExc_ValueError, "polygons need at least 3 sides");
}

double *verticies = PyMem_New(double, sides * 2);
itzpr3d4t0r marked this conversation as resolved.
Show resolved Hide resolved
if (!verticies) {
return PyErr_NoMemory();
itzpr3d4t0r marked this conversation as resolved.
Show resolved Hide resolved
}

int loop;
for (loop = 0; loop < sides; loop++) {
verticies[loop * 2 + 0] =
itzpr3d4t0r marked this conversation as resolved.
Show resolved Hide resolved
Cx + radius * cos(angle + PI * 2 * loop / sides);
verticies[loop * 2 + 1] =
Cy + radius * sin(angle + PI * 2 * loop / sides);
itzpr3d4t0r marked this conversation as resolved.
Show resolved Hide resolved
}

PyObject *ret = pgPolygon_New2(verticies, sides);
PyMem_Free(verticies);

return ret;
error:
return RAISE(PyExc_TypeError, "mismatched argument types");
itzpr3d4t0r marked this conversation as resolved.
Show resolved Hide resolved
}

static PyObject *
pg_polygon_repr(pgPolygonObject *self)
{
Expand Down Expand Up @@ -342,6 +413,8 @@ pg_polygon_copy(pgPolygonObject *self, PyObject *_null)
}

static struct PyMethodDef pg_polygon_methods[] = {
{"normal_polygon", (PyCFunction)pg_polygon_normal_polygon,
itzpr3d4t0r marked this conversation as resolved.
Show resolved Hide resolved
METH_STATIC | METH_FASTCALL, NULL},
{"__copy__", (PyCFunction)pg_polygon_copy, METH_NOARGS, NULL},
{"copy", (PyCFunction)pg_polygon_copy, METH_NOARGS, NULL},
{NULL, NULL, 0, NULL}};
Expand Down
64 changes: 64 additions & 0 deletions test/test_polygon.py
Expand Up @@ -4,6 +4,8 @@

from geometry import Polygon

import math

p1 = (12.0, 12.0)
p2 = (32.0, 43.0)
p3 = (22.0, 4.0)
Expand Down Expand Up @@ -123,6 +125,68 @@ def test_copy_invalid_args(self):
with self.assertRaises(TypeError):
po.copy(*value)

def test_static_normal_polygon(self):
center = (150.5, 100.1)
radius = 50.2
sides = 10
angle = 20.6

polygon_pg = Polygon.normal_polygon(sides, center, radius, angle)
vertices_pg = polygon_pg.vertices

vertices = []

for i in range(sides):
vertices.append(
(
center[0]
+ radius * math.cos(math.radians(angle) + math.pi * 2 * i / sides),
center[1]
+ radius * math.sin(math.radians(angle) + math.pi * 2 * i / sides),
)
)

self.assertEqual(vertices_pg, vertices)

invalid_types = [
None,
[],
"1",
"123",
(1,),
[1, 2, 3],
[p1, p2, p3, 32],
[p1, p2, "(1, 1)"],
]

for invalid_type in invalid_types + [(1, 2)]:
with self.assertRaises(TypeError):
Polygon.normal_polygon(invalid_type, (1, 2.2), 5.5, 1)

for invalid_type in invalid_types:
with self.assertRaises(TypeError):
Polygon.normal_polygon(5, invalid_type, 5.5, 1)

for invalid_type in invalid_types + [(1, 2)]:
with self.assertRaises(TypeError):
Polygon.normal_polygon(5, (1, 2.2), invalid_type, 1)

for invalid_type in invalid_types + [(1, 2)]:
with self.assertRaises(TypeError):
Polygon.normal_polygon(5, (1, 2.2), 5.5, invalid_type)

with self.assertRaises(TypeError):
Polygon.normal_polygon(1, (1, 2.2), 5.5, 1, 5)

with self.assertRaises(TypeError):
Polygon.normal_polygon()

with self.assertRaises(ValueError):
Polygon.normal_polygon(-1, center, radius, angle)

with self.assertRaises(ValueError):
Polygon.normal_polygon(2, center, radius, angle)

def test_copy_return_type(self):
"""Checks whether the copy method returns a polygon"""
po = Polygon([p1, p2, p3, p4])
Expand Down