@@ -490,6 +490,129 @@ pg_line_flip_ip(pgLineObject *self, PyObject *_null)
490490 Py_RETURN_NONE ;
491491}
492492
493+ static PyObject *
494+ pg_line_as_points (pgLineObject * self , PyObject * arg )
495+ {
496+ int N = 0 ;
497+ if (!pg_IntFromObj (arg , & N )) {
498+ return RAISE (PyExc_TypeError , "as_points requires an integer" );
499+ }
500+ if (N < 0 ) {
501+ return RAISE (PyExc_ValueError , "as_points requires a positive integer" );
502+ }
503+
504+ PyObject * point = NULL ;
505+ PyObject * list = PyList_New (2 + N );
506+ if (!list ) {
507+ return NULL ;
508+ }
509+
510+ pgLineBase * line = & self -> line ;
511+
512+ // Add the start and end points to the list
513+ point = pg_TupleFromDoublePair (line -> x1 , line -> y1 );
514+ if (!point ) {
515+ Py_DECREF (list );
516+ return NULL ;
517+ }
518+ PyList_SET_ITEM (list , 0 , point );
519+ point = pg_TupleFromDoublePair (line -> x2 , line -> y2 );
520+ if (!point ) {
521+ Py_DECREF (list );
522+ return NULL ;
523+ }
524+ PyList_SET_ITEM (list , N + 1 , point );
525+
526+ if (!N ) {
527+ return list ;
528+ }
529+ else if (N == 1 ) {
530+ point = pg_TupleFromDoublePair ((line -> x1 + line -> x2 ) / 2 ,
531+ (line -> y1 + line -> y2 ) / 2 );
532+ if (!point ) {
533+ Py_DECREF (list );
534+ return NULL ;
535+ }
536+ PyList_SET_ITEM (list , 1 , point );
537+ return list ;
538+ }
539+
540+ double step_x = (line -> x2 - line -> x1 ) / (N + 1 );
541+ double step_y = (line -> y2 - line -> y1 ) / (N + 1 );
542+ double x = line -> x1 + step_x ;
543+ double y = line -> y1 + step_y ;
544+
545+ Py_ssize_t i ;
546+ for (i = 1 ; i < N + 1 ; i ++ ) {
547+ point = pg_TupleFromDoublePair (x , y );
548+ if (!point ) {
549+ Py_DECREF (list );
550+ return NULL ;
551+ }
552+ PyList_SET_ITEM (list , i , point );
553+ x += step_x ;
554+ y += step_y ;
555+ }
556+
557+ return list ;
558+ }
559+
560+ static PyObject *
561+ pg_line_as_segments (pgLineObject * self , PyObject * arg )
562+ {
563+ /* Segments the line into N Lines of equal length and returns a list of
564+ * them. */
565+
566+ int N = 1 ;
567+ if (!pg_IntFromObj (arg , & N )) {
568+ return RAISE (PyExc_TypeError ,
569+ "as_segments requires an integer" );
570+ }
571+ if (N < 1 ) {
572+ return RAISE (PyExc_ValueError ,
573+ "as_segments requires a positive integer" );
574+ }
575+
576+ PyObject * line_obj = NULL ;
577+ PyObject * list = PyList_New (N );
578+ if (!list ) {
579+ return NULL ;
580+ }
581+
582+ if (N == 1 ) {
583+ line_obj = pg_line_copy (self , NULL );
584+ if (!line_obj ) {
585+ Py_DECREF (list );
586+ return NULL ;
587+ }
588+ PyList_SET_ITEM (list , 0 , line_obj );
589+ return list ;
590+ }
591+
592+ pgLineBase * line = & self -> line ;
593+
594+ double step_x = (line -> x2 - line -> x1 ) / N ;
595+ double step_y = (line -> y2 - line -> y1 ) / N ;
596+ double x1 = line -> x1 , y1 = line -> y1 ;
597+ double x2 = x1 + step_x , y2 = y1 + step_y ;
598+
599+ Py_ssize_t i ;
600+ for (i = 0 ; i < N ; i ++ ) {
601+ line_obj = _pg_line_subtype_new4 (Py_TYPE (self ), x1 , y1 , x2 , y2 );
602+ if (!line_obj ) {
603+ Py_DECREF (list );
604+ return NULL ;
605+ }
606+ PyList_SET_ITEM (list , i , line_obj );
607+ x1 = x2 ;
608+ y1 = y2 ;
609+ x2 += step_x ;
610+ y2 += step_y ;
611+ }
612+
613+ return list ;
614+ }
615+
493616static struct PyMethodDef pg_line_methods [] = {
494617 {"__copy__" , (PyCFunction )pg_line_copy , METH_NOARGS , NULL },
495618 {"copy" , (PyCFunction )pg_line_copy , METH_NOARGS , NULL },
@@ -508,6 +631,8 @@ static struct PyMethodDef pg_line_methods[] = {
508631 {"at" , (PyCFunction )pg_line_at , METH_O , NULL },
509632 {"flip" , (PyCFunction )pg_line_flip , METH_NOARGS , NULL },
510633 {"flip_ip" , (PyCFunction )pg_line_flip_ip , METH_NOARGS , NULL },
634+ {"as_points" , (PyCFunction )pg_line_as_points , METH_O , NULL },
635+ {"as_segments" , (PyCFunction )pg_line_as_segments , METH_O , NULL },
511636 {NULL , NULL , 0 , NULL }};
512637
513638/* sequence functions */
0 commit comments