Skip to content

Commit

Permalink
PicoVector: MPY bindings for line and arc.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Nov 26, 2024
1 parent a2c48a8 commit ad8ddea
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
4 changes: 4 additions & 0 deletions micropython/modules/picovector/picovector.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ static MP_DEFINE_CONST_FUN_OBJ_VAR(POLYGON_path_obj, 4, POLYGON_path);
static MP_DEFINE_CONST_FUN_OBJ_KW(POLYGON_regular_obj, 5, POLYGON_regular);
static MP_DEFINE_CONST_FUN_OBJ_KW(POLYGON_circle_obj, 4, POLYGON_circle);
static MP_DEFINE_CONST_FUN_OBJ_KW(POLYGON_arc_obj, 6, POLYGON_arc);
static MP_DEFINE_CONST_FUN_OBJ_KW(POLYGON_star_obj, 6, POLYGON_arc);
static MP_DEFINE_CONST_FUN_OBJ_KW(POLYGON_line_obj, 5, POLYGON_arc);


static const mp_rom_map_elem_t POLYGON_locals_dict_table[] = {
Expand All @@ -39,6 +41,8 @@ static const mp_rom_map_elem_t POLYGON_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_path), MP_ROM_PTR(&POLYGON_path_obj) },
{ MP_ROM_QSTR(MP_QSTR_circle), MP_ROM_PTR(&POLYGON_circle_obj) },
{ MP_ROM_QSTR(MP_QSTR_arc), MP_ROM_PTR(&POLYGON_arc_obj) },
{ MP_ROM_QSTR(MP_QSTR_star), MP_ROM_PTR(&POLYGON_star_obj) },
{ MP_ROM_QSTR(MP_QSTR_line), MP_ROM_PTR(&POLYGON_line_obj) },
};

static MP_DEFINE_CONST_DICT(POLYGON_locals_dict, POLYGON_locals_dict_table);
Expand Down
66 changes: 66 additions & 0 deletions micropython/modules/picovector/picovector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,72 @@ mp_obj_t POLYGON_arc(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args)
return self;
}

mp_obj_t POLYGON_star(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_self, ARG_x, ARG_y, ARG_points, ARG_inner_radius, ARG_outer_radius, ARG_stroke };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_x, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_y, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_points, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_inner_radius, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_outer_radius, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_stroke, MP_ARG_OBJ, { .u_obj = mp_const_none }},
};

mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

_POLY_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _POLY_obj_t);

picovector_point_type x = mp_picovector_get_point_type(args[ARG_x].u_obj);
picovector_point_type y = mp_picovector_get_point_type(args[ARG_y].u_obj);
int p = args[ARG_points].u_int;
picovector_point_type r1 = mp_picovector_get_point_type(args[ARG_inner_radius].u_obj);
picovector_point_type r2 = mp_picovector_get_point_type(args[ARG_outer_radius].u_obj);
picovector_point_type s = args[ARG_stroke].u_obj == mp_const_none ? 0 : mp_picovector_get_point_type(args[ARG_stroke].u_obj);

pp_poly_merge(self->poly, ppp_star({
x, y,
p,
r1,
r2,
s
}));

return self;
}

mp_obj_t POLYGON_line(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_self, ARG_x, ARG_y, ARG_x2, ARG_y2, ARG_thickness };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_x, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_y, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_x2, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_y2, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_thickness, MP_ARG_OBJ, { .u_obj = mp_const_none }},
};

mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

_POLY_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _POLY_obj_t);

picovector_point_type x = mp_picovector_get_point_type(args[ARG_x].u_obj);
picovector_point_type y = mp_picovector_get_point_type(args[ARG_y].u_obj);
picovector_point_type x2 = mp_picovector_get_point_type(args[ARG_x2].u_obj);
picovector_point_type y2 = mp_picovector_get_point_type(args[ARG_y2].u_obj);
picovector_point_type t = args[ARG_thickness].u_obj == mp_const_none ? 0 : mp_picovector_get_point_type(args[ARG_thickness].u_obj);

pp_poly_merge(self->poly, ppp_line({
x, y,
x2, y2,
t
}));

return self;
}

// Utility functions

mp_obj_t POLYGON_centroid(mp_obj_t self_in) {
Expand Down

0 comments on commit ad8ddea

Please sign in to comment.