@@ -523,6 +523,101 @@ STATIC mp_obj_t bitmaptools_obj_draw_line(size_t n_args, const mp_obj_t *pos_arg
523523MP_DEFINE_CONST_FUN_OBJ_KW (bitmaptools_draw_line_obj , 0 , bitmaptools_obj_draw_line );
524524// requires all 6 arguments
525525
526+ //| def draw_polygon(
527+ //| dest_bitmap: displayio.Bitmap,
528+ //| xs: ReadableBuffer,
529+ //| ys: ReadableBuffer,
530+ //| value: int,
531+ //| close: Optional[bool] = True,
532+ //| ) -> None:
533+ //| """Draw a polygon conecting points on provided bitmap with provided value
534+ //|
535+ //| :param bitmap dest_bitmap: Destination bitmap that will be written into
536+ //| :param ReadableBuffer xs: x-pixel position of the polygon's vertices
537+ //| :param ReadableBuffer ys: y-pixel position of the polygon's vertices
538+ //| :param int value: Bitmap palette index that will be written into the
539+ //| line in the destination bitmap
540+ //| :param bool close: (Optional) Wether to connect first and last point. (True)
541+ //|
542+ //| .. code-block:: Python
543+ //|
544+ //| import board
545+ //| import displayio
546+ //| import bitmaptools
547+ //|
548+ //| display = board.DISPLAY
549+ //| main_group = displayio.Group()
550+ //| display.root_group = main_group
551+ //|
552+ //| palette = displayio.Palette(3)
553+ //| palette[0] = 0xffffff
554+ //| palette[1] = 0x0000ff
555+ //| palette[2] = 0xff0000
556+ //|
557+ //| bmp = displayio.Bitmap(128,128, 3)
558+ //| bmp.fill(0)
559+ //|
560+ //| xs = bytes([4, 101, 101, 19])
561+ //| ys = bytes([4, 19, 121, 101])
562+ //| bitmaptools.draw_polygon(bmp, xs, ys, 1)
563+ //|
564+ //| xs = bytes([14, 60, 110])
565+ //| ys = bytes([14, 24, 90])
566+ //| bitmaptools.draw_polygon(bmp, xs, ys, 2)
567+ //|
568+ //| tilegrid = displayio.TileGrid(bitmap=bmp, pixel_shader=palette)
569+ //| main_group.append(tilegrid)
570+ //|
571+ //| while True:
572+ //| pass
573+ //| """
574+ //| ...
575+ //|
576+ STATIC mp_obj_t bitmaptools_obj_draw_polygon (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
577+ enum {ARG_dest_bitmap , ARG_xs , ARG_ys , ARG_value , ARG_close };
578+
579+ static const mp_arg_t allowed_args [] = {
580+ {MP_QSTR_dest_bitmap , MP_ARG_REQUIRED | MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL }},
581+ {MP_QSTR_xs , MP_ARG_REQUIRED | MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL }},
582+ {MP_QSTR_ys , MP_ARG_REQUIRED | MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL }},
583+ {MP_QSTR_value , MP_ARG_REQUIRED | MP_ARG_INT , {.u_obj = MP_OBJ_NULL }},
584+ {MP_QSTR_close , MP_ARG_BOOL , {.u_bool = true}},
585+ };
586+ mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
587+ mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
588+
589+ displayio_bitmap_t * destination = MP_OBJ_TO_PTR (args [ARG_dest_bitmap ].u_obj ); // the destination bitmap
590+
591+ mp_buffer_info_t xs_buf , ys_buf ;
592+ mp_get_buffer_raise (args [ARG_xs ].u_obj , & xs_buf , MP_BUFFER_READ );
593+ mp_get_buffer_raise (args [ARG_ys ].u_obj , & ys_buf , MP_BUFFER_READ );
594+ size_t xs_size = mp_binary_get_size ('@' , xs_buf .typecode , NULL );
595+ size_t ys_size = mp_binary_get_size ('@' , ys_buf .typecode , NULL );
596+ size_t xs_len = xs_buf .len / xs_size ;
597+ size_t ys_len = ys_buf .len / ys_size ;
598+ if (xs_size != ys_size ) {
599+ mp_raise_ValueError (translate ("Coordinate arrays types have different sizes" ));
600+ }
601+ if (xs_len != ys_len ) {
602+ mp_raise_ValueError (translate ("Coordinate arrays have different lengths" ));
603+ }
604+
605+ uint32_t value , color_depth ;
606+ value = args [ARG_value ].u_int ;
607+ color_depth = (1 << destination -> bits_per_value );
608+ if (color_depth <= value ) {
609+ mp_raise_ValueError (translate ("out of range of target" ));
610+ }
611+
612+ bool close = args [ARG_close ].u_bool ;
613+
614+ common_hal_bitmaptools_draw_polygon (destination , xs_buf .buf , ys_buf .buf , xs_len , xs_size , value , close );
615+
616+ return mp_const_none ;
617+ }
618+
619+ MP_DEFINE_CONST_FUN_OBJ_KW (bitmaptools_draw_polygon_obj , 0 , bitmaptools_obj_draw_polygon );
620+
526621//| def arrayblit(
527622//| bitmap: displayio.Bitmap,
528623//| data: ReadableBuffer,
@@ -784,6 +879,7 @@ STATIC const mp_rom_map_elem_t bitmaptools_module_globals_table[] = {
784879 { MP_ROM_QSTR (MP_QSTR_fill_region ), MP_ROM_PTR (& bitmaptools_fill_region_obj ) },
785880 { MP_ROM_QSTR (MP_QSTR_boundary_fill ), MP_ROM_PTR (& bitmaptools_boundary_fill_obj ) },
786881 { MP_ROM_QSTR (MP_QSTR_draw_line ), MP_ROM_PTR (& bitmaptools_draw_line_obj ) },
882+ { MP_ROM_QSTR (MP_QSTR_draw_polygon ), MP_ROM_PTR (& bitmaptools_draw_polygon_obj ) },
787883 { MP_ROM_QSTR (MP_QSTR_dither ), MP_ROM_PTR (& bitmaptools_dither_obj ) },
788884 { MP_ROM_QSTR (MP_QSTR_DitherAlgorithm ), MP_ROM_PTR (& bitmaptools_dither_algorithm_type ) },
789885};
0 commit comments