Skip to content

Commit 9d0501a

Browse files
committed
PicoVector: Polygon iter interface.
1 parent 7c5ebfc commit 9d0501a

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

micropython/modules/picovector/picovector.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ MP_DEFINE_CONST_OBJ_TYPE(
1818
MP_TYPE_FLAG_NONE,
1919
make_new, POLYGON_make_new,
2020
print, POLYGON_print,
21+
iter, POLYGON_getiter,
2122
locals_dict, (mp_obj_dict_t*)&POLYGON_locals_dict
2223
);
2324
MP_DEFINE_CONST_OBJ_TYPE(
@@ -40,6 +41,7 @@ const mp_obj_type_t POLYGON_type = {
4041
.name = MP_QSTR_polygon,
4142
.make_new = POLYGON_make_new,
4243
.print = POLYGON_print,
44+
.iter = POLYGON_getiter,
4345
.locals_dict = (mp_obj_dict_t*)&POLYGON_locals_dict,
4446
};
4547
const mp_obj_type_t REGULAR_POLYGON_type = {

micropython/modules/picovector/picovector.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,7 @@ void POLYGON_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t ki
217217
(void)kind;
218218
_POLYGON_obj_t *self = MP_OBJ_TO_PTR2(self_in, _POLYGON_obj_t);
219219

220-
mp_print_str(print, "Polygon(");
221-
mp_print_str(print, ", points = ");
220+
mp_print_str(print, "Polygon(points = ");
222221
mp_obj_print_helper(print, mp_obj_new_int(self->contour.count), PRINT_REPR);
223222
mp_print_str(print, ", bounds = ");
224223
mp_obj_print_helper(print, mp_obj_new_int(self->contour.bounds().x), PRINT_REPR);
@@ -238,6 +237,38 @@ mp_obj_t POLYGON__del__(mp_obj_t self_in) {
238237
return mp_const_none;
239238
}
240239

240+
typedef struct _mp_obj_polygon_it_t {
241+
mp_obj_base_t base;
242+
mp_fun_1_t iternext;
243+
mp_obj_t polygon;
244+
size_t cur;
245+
} mp_obj_polygon_it_t;
246+
247+
STATIC mp_obj_t py_image_it_iternext(mp_obj_t self_in) {
248+
mp_obj_polygon_it_t *self = MP_OBJ_TO_PTR2(self_in, mp_obj_polygon_it_t);
249+
_POLYGON_obj_t *polygon = MP_OBJ_TO_PTR2(self->polygon, _POLYGON_obj_t);
250+
251+
//mp_printf(&mp_plat_print, "points: %d, current: %d\n", polygon->contour.count, self->cur);
252+
253+
if(self->cur >= polygon->contour.count) return MP_OBJ_STOP_ITERATION;
254+
255+
mp_obj_t tuple[2];
256+
tuple[0] = mp_obj_new_int((int)(polygon->contour.points[self->cur].x));
257+
tuple[1] = mp_obj_new_int((int)(polygon->contour.points[self->cur].y));
258+
259+
self->cur++;
260+
return mp_obj_new_tuple(2, tuple);
261+
}
262+
263+
mp_obj_t POLYGON_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) {
264+
mp_obj_polygon_it_t *o = (mp_obj_polygon_it_t *)iter_buf;
265+
o->base.type = &mp_type_polymorph_iter;
266+
o->iternext = py_image_it_iternext;
267+
o->polygon = o_in;
268+
o->cur = 0;
269+
return MP_OBJ_FROM_PTR(o);
270+
}
271+
241272
/* VECTOR */
242273

243274
mp_obj_t VECTOR_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {

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_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf);
1314

1415
extern mp_obj_t POLYGON__del__(mp_obj_t self_in);
1516

0 commit comments

Comments
 (0)