Skip to content

Commit 231ceb7

Browse files
committed
PicoVector: Add basic polygon center of mass function.
1 parent c9fd68e commit 231ceb7

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

micropython/modules/picovector/picovector.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
/* Polygon */
44

55
STATIC MP_DEFINE_CONST_FUN_OBJ_1(POLYGON__del__obj, POLYGON__del__);
6+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(POLYGON_centroid_obj, POLYGON_centroid);
67

78

89
STATIC const mp_rom_map_elem_t POLYGON_locals_dict_table[] = {
910
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&POLYGON__del__obj) },
11+
{ MP_ROM_QSTR(MP_QSTR_centroid), MP_ROM_PTR(&POLYGON_centroid_obj) },
1012
};
1113

1214
STATIC MP_DEFINE_CONST_DICT(POLYGON_locals_dict, POLYGON_locals_dict_table);

micropython/modules/picovector/picovector.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,24 @@ mp_obj_t POLYGON_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw,
214214
return self;
215215
}
216216

217+
mp_obj_t POLYGON_centroid(mp_obj_t self_in) {
218+
_POLYGON_obj_t *self = MP_OBJ_TO_PTR2(self_in, _POLYGON_obj_t);
219+
220+
pretty_poly::point_t<picovector_point_type> sum(0, 0);
221+
222+
for(auto i = 0u; i < self->contour.count; i++) {
223+
sum += self->contour.points[i];
224+
}
225+
226+
sum /= (float)self->contour.count;
227+
228+
mp_obj_t tuple[2];
229+
tuple[0] = mp_obj_new_int((int)(sum.x));
230+
tuple[1] = mp_obj_new_int((int)(sum.y));
231+
232+
return mp_obj_new_tuple(2, tuple);
233+
}
234+
217235
void POLYGON_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
218236
(void)kind;
219237
_POLYGON_obj_t *self = MP_OBJ_TO_PTR2(self_in, _POLYGON_obj_t);

micropython/modules/picovector/picovector.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extern mp_obj_t POLYGON_make_new(const mp_obj_type_t *type, size_t n_args, size_
1010
extern mp_obj_t REGULAR_POLYGON_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args);
1111
extern mp_obj_t RECTANGLE_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args);
1212
extern void POLYGON_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind);
13+
extern mp_obj_t POLYGON_centroid(mp_obj_t self_in);
1314
extern mp_obj_t POLYGON_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf);
1415

1516
extern mp_obj_t POLYGON__del__(mp_obj_t self_in);

0 commit comments

Comments
 (0)