Skip to content

Commit e6c1f8c

Browse files
committed
remade the method to be VARARGS insted of FASTCALL
1 parent 317719e commit e6c1f8c

File tree

3 files changed

+45
-31
lines changed

3 files changed

+45
-31
lines changed

.clangd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CompileFlags:
2+
CompilationDatabase: ./.mesonpy-build/

.my_pygame_test/main.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pygame
2+
from pygame.geometry import Line
3+
4+
pygame.init()
5+
6+
window = pygame.display.set_mode((600, 600))
7+
8+
l = Line(10, 10, 200, 200)
9+
p = (1, 1)
10+
11+
running = True
12+
while running:
13+
14+
window.fill((255, 255, 255))
15+
16+
pygame.draw.line(window, (255, 0, 0), l.a, l.b, 5)
17+
18+
pygame.draw.circle(window, (0, 255, 0), p, 5)
19+
pygame.draw.circle(window, (0, 255, 0), l.project(p, do_clamp=True), 5)
20+
21+
pygame.display.flip()
22+
23+
for event in pygame.event.get():
24+
if event.type == pygame.QUIT:
25+
running = False
26+
27+
pygame.quit()

src_c/line.c

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "doc/geometry_doc.h"
22
#include "geometry_common.h"
3+
#include "methodobject.h"
4+
#include "pytypedefs.h"
35

46
static double
57
pgLine_Length(pgLineBase *line)
@@ -179,27 +181,6 @@ _line_scale_helper(pgLineBase *line, double factor, double origin)
179181
return 1;
180182
}
181183

182-
void
183-
_normalize_vector(double *vector)
184-
{
185-
double length = sqrt(vector[0] * vector[0] + vector[1] * vector[1]);
186-
// check to see if the vector is zero
187-
if (length == 0) {
188-
vector[0] = 0;
189-
vector[1] = 0;
190-
}
191-
else {
192-
vector[0] /= length;
193-
vector[1] /= length;
194-
}
195-
}
196-
197-
double
198-
_length_of_vector(double *vector)
199-
{
200-
return sqrt(vector[0] * vector[0] + vector[1] * vector[1]);
201-
}
202-
203184
static PyObject *
204185
_line_project_helper(pgLineBase *line, double *point, int clamp)
205186
{
@@ -281,21 +262,25 @@ pg_line_scale_ip(pgLineObject *self, PyObject *const *args, Py_ssize_t nargs)
281262
}
282263

283264
static PyObject *
284-
pg_line_project(pgLineObject *self, PyObject *const *args, Py_ssize_t nargs,
285-
PyObject *kwnames)
265+
pg_line_project(pgLineObject *self, PyObject *args, PyObject *kwnames)
286266
{
287267
double point[2] = {0.f, 0.f};
288268
int clamp = 0;
289269

290-
if (nargs >= 1) {
291-
if (!pg_TwoDoublesFromObj(args[0], &point[0], &point[1])) {
292-
return RAISE(PyExc_TypeError,
293-
"project requires a sequence of two numbers");
294-
}
270+
PyObject *point_obj = NULL;
271+
272+
static char *kwlist[] = {"point", "clamp", NULL};
273+
274+
if (!PyArg_ParseTupleAndKeywords(args, kwnames, "O|p:project", kwlist,
275+
&point_obj, &clamp)) {
276+
return RAISE(
277+
PyExc_TypeError,
278+
"project requires a sequence(point) and an optional clamp flag");
295279
}
296280

297-
if (kwnames != NULL) {
298-
clamp = PyObject_IsTrue(args[nargs]);
281+
if (!pg_TwoDoublesFromObj(point_obj, &point[0], &point[1])) {
282+
return RAISE(PyExc_TypeError,
283+
"project requires a sequence of two numbers");
299284
}
300285

301286
PyObject *projected_point;
@@ -319,7 +304,7 @@ static struct PyMethodDef pg_line_methods[] = {
319304
{"scale", (PyCFunction)pg_line_scale, METH_FASTCALL, DOC_LINE_SCALE},
320305
{"scale_ip", (PyCFunction)pg_line_scale_ip, METH_FASTCALL,
321306
DOC_LINE_SCALEIP},
322-
{"project", (PyCFunction)pg_line_project, METH_FASTCALL | METH_KEYWORDS,
307+
{"project", (PyCFunction)pg_line_project, METH_VARARGS | METH_KEYWORDS,
323308
DOC_LINE_PROJECT},
324309
{NULL, NULL, 0, NULL}};
325310

0 commit comments

Comments
 (0)