From 3a475cbad877c0613483b70d2de0847e8ce030bb Mon Sep 17 00:00:00 2001 From: foamyguy Date: Mon, 5 Aug 2024 20:54:36 -0500 Subject: [PATCH 01/17] starting is_colliding --- shared-bindings/vectorio/Rectangle.c | 9 +++++++++ shared-bindings/vectorio/Rectangle.h | 2 ++ 2 files changed, 11 insertions(+) diff --git a/shared-bindings/vectorio/Rectangle.c b/shared-bindings/vectorio/Rectangle.c index d1f2e6193ce3e..345d5241b1253 100644 --- a/shared-bindings/vectorio/Rectangle.c +++ b/shared-bindings/vectorio/Rectangle.c @@ -136,6 +136,15 @@ const mp_obj_property_t vectorio_rectangle_color_index_obj = { MP_ROM_NONE}, }; +//| is_colliding: bool +//| """Return true if this shape is colliding with another shape.""" +static mp_obj_t vectorio_rectangle_obj_is_colliding(mp_obj_t self_in, mp_obj_t other_shape) { + vectorio_rectangle_t *self = MP_OBJ_TO_PTR(self_in); + return mp_obj_new_bool(common_hal_vectorio_rectangle_is_colliding(self, other_shape)); +} +MP_DEFINE_CONST_FUN_OBJ_1(vectorio_rectangle_is_colliding_obj, vectorio_rectangle_obj_is_colliding); + + // Documentation for properties inherited from VectorShape. //| x: int diff --git a/shared-bindings/vectorio/Rectangle.h b/shared-bindings/vectorio/Rectangle.h index 6831fa21029a7..b0e786a44d272 100644 --- a/shared-bindings/vectorio/Rectangle.h +++ b/shared-bindings/vectorio/Rectangle.h @@ -29,3 +29,5 @@ void common_hal_vectorio_rectangle_set_color_index(void *obj, uint16_t color_ind int16_t common_hal_vectorio_rectangle_get_height(void *obj); void common_hal_vectorio_rectangle_set_height(void *obj, int16_t height); + +bool common_hal_vectorio_rectangle_is_colliding(vectorio_rectangle_t *self, mp_obj_t other_shape); From 9c0100c6cc921a72a3c909b9b9d2f81fed9acfdd Mon Sep 17 00:00:00 2001 From: foamyguy Date: Tue, 6 Aug 2024 16:52:28 -0500 Subject: [PATCH 02/17] implement rectangle.is_colliding --- shared-bindings/vectorio/Rectangle.c | 8 ++-- shared-module/vectorio/Rectangle.c | 60 ++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/shared-bindings/vectorio/Rectangle.c b/shared-bindings/vectorio/Rectangle.c index 345d5241b1253..6ae07822d148b 100644 --- a/shared-bindings/vectorio/Rectangle.c +++ b/shared-bindings/vectorio/Rectangle.c @@ -136,13 +136,14 @@ const mp_obj_property_t vectorio_rectangle_color_index_obj = { MP_ROM_NONE}, }; -//| is_colliding: bool -//| """Return true if this shape is colliding with another shape.""" +//| def is_colliding(self, other_shape: Union[vectorio.Circle, vectorio.Rectangle]) -> bool: +//| """Return true if this shape is colliding with another shape.""" +//| ... static mp_obj_t vectorio_rectangle_obj_is_colliding(mp_obj_t self_in, mp_obj_t other_shape) { vectorio_rectangle_t *self = MP_OBJ_TO_PTR(self_in); return mp_obj_new_bool(common_hal_vectorio_rectangle_is_colliding(self, other_shape)); } -MP_DEFINE_CONST_FUN_OBJ_1(vectorio_rectangle_is_colliding_obj, vectorio_rectangle_obj_is_colliding); +MP_DEFINE_CONST_FUN_OBJ_2(vectorio_rectangle_is_colliding_obj, vectorio_rectangle_obj_is_colliding); // Documentation for properties inherited from VectorShape. @@ -171,6 +172,7 @@ static const mp_rom_map_elem_t vectorio_rectangle_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&vectorio_vector_shape_y_obj) }, { MP_ROM_QSTR(MP_QSTR_hidden), MP_ROM_PTR(&vectorio_vector_shape_hidden_obj) }, { MP_ROM_QSTR(MP_QSTR_color_index), MP_ROM_PTR(&vectorio_rectangle_color_index_obj) }, + { MP_ROM_QSTR(MP_QSTR_is_colliding), MP_ROM_PTR(&vectorio_rectangle_is_colliding_obj) }, { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&vectorio_rectangle_width_obj) }, { MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&vectorio_rectangle_height_obj) }, { MP_ROM_QSTR(MP_QSTR_location), MP_ROM_PTR(&vectorio_vector_shape_location_obj) }, diff --git a/shared-module/vectorio/Rectangle.c b/shared-module/vectorio/Rectangle.c index 42cdf7cca96e4..1037d5f8c3a45 100644 --- a/shared-module/vectorio/Rectangle.c +++ b/shared-module/vectorio/Rectangle.c @@ -6,10 +6,13 @@ #include "shared-module/vectorio/__init__.h" #include "shared-bindings/vectorio/Rectangle.h" +#include "shared-bindings/vectorio/VectorShape.h" +#include "shared-bindings/vectorio/Circle.h" #include "shared-module/displayio/area.h" #include "py/runtime.h" #include "stdlib.h" +#include void common_hal_vectorio_rectangle_construct(vectorio_rectangle_t *self, uint32_t width, uint32_t height, uint16_t color_index) { @@ -48,6 +51,63 @@ mp_obj_t common_hal_vectorio_rectangle_get_draw_protocol(void *rectangle) { return self->draw_protocol_instance; } +bool common_hal_vectorio_rectangle_is_colliding(vectorio_rectangle_t *self, mp_obj_t other_shape) { + + mp_obj_t possible_rect = mp_obj_cast_to_native_base(other_shape, &vectorio_rectangle_type); + if (possible_rect != MP_OBJ_NULL) { + + vectorio_rectangle_t *other_rect = possible_rect; + + mp_int_t self_left = common_hal_vectorio_vector_shape_get_x(self->draw_protocol_instance); + mp_int_t self_right = self_left + self->width; + mp_int_t self_top = common_hal_vectorio_vector_shape_get_y(self->draw_protocol_instance); + mp_int_t self_bottom = self_top + self->height; + + mp_int_t other_left = common_hal_vectorio_vector_shape_get_x(other_rect->draw_protocol_instance); + mp_int_t other_right = other_left + other_rect->width; + mp_int_t other_top = common_hal_vectorio_vector_shape_get_y(other_rect->draw_protocol_instance); + mp_int_t other_bottom = other_top + other_rect->height; + + return self_left < other_right && self_right > other_left && + self_top < other_bottom && self_bottom > other_top; + } + mp_obj_t possible_circle = mp_obj_cast_to_native_base(other_shape, &vectorio_circle_type); + if (possible_circle != MP_OBJ_NULL) { + vectorio_circle_t *other_circle = possible_circle; + + mp_int_t self_left = common_hal_vectorio_vector_shape_get_x(self->draw_protocol_instance); + mp_int_t self_right = self_left + self->width; + mp_int_t self_top = common_hal_vectorio_vector_shape_get_y(self->draw_protocol_instance); + mp_int_t self_bottom = self_top + self->height; + + mp_int_t other_circle_x = common_hal_vectorio_vector_shape_get_x(other_circle->draw_protocol_instance); + mp_int_t other_circle_y = common_hal_vectorio_vector_shape_get_y(other_circle->draw_protocol_instance); + + mp_int_t test_x = other_circle_x; + mp_int_t test_y = other_circle_y; + + if (other_circle_x < self_left) { + test_x = self_left; + } else if (other_circle_x > self_right) { + test_x = self_right; + } + + if (other_circle_y < self_top) { + test_y = self_top; + } else if (other_circle_y > self_bottom) { + test_y = self_bottom; + } + + mp_int_t dist_x = other_circle_x - test_x; + mp_int_t dist_y = other_circle_y - test_y; + mp_int_t dist = sqrtf((dist_x * dist_x) + (dist_y * dist_y)); + + return dist <= other_circle->radius; + } + + return false; +} + int16_t common_hal_vectorio_rectangle_get_width(void *obj) { vectorio_rectangle_t *self = obj; return self->width; From 58aeb27f0cefc869d4f6de4c666e90c3e6083ea6 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Tue, 6 Aug 2024 20:23:55 -0500 Subject: [PATCH 03/17] implement rectangle.is_colliding --- shared-bindings/vectorio/Rectangle.c | 10 +++++----- shared-bindings/vectorio/Rectangle.h | 2 +- shared-module/vectorio/Rectangle.c | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/shared-bindings/vectorio/Rectangle.c b/shared-bindings/vectorio/Rectangle.c index 6ae07822d148b..22e11c4e680b1 100644 --- a/shared-bindings/vectorio/Rectangle.c +++ b/shared-bindings/vectorio/Rectangle.c @@ -136,14 +136,14 @@ const mp_obj_property_t vectorio_rectangle_color_index_obj = { MP_ROM_NONE}, }; -//| def is_colliding(self, other_shape: Union[vectorio.Circle, vectorio.Rectangle]) -> bool: +//| def intersecting(self, other_shape: Union[vectorio.Circle, vectorio.Rectangle]) -> bool: //| """Return true if this shape is colliding with another shape.""" //| ... -static mp_obj_t vectorio_rectangle_obj_is_colliding(mp_obj_t self_in, mp_obj_t other_shape) { +static mp_obj_t vectorio_rectangle_obj_intersecting(mp_obj_t self_in, mp_obj_t other_shape) { vectorio_rectangle_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_bool(common_hal_vectorio_rectangle_is_colliding(self, other_shape)); + return mp_obj_new_bool(common_hal_vectorio_rectangle_intersecting(self, other_shape)); } -MP_DEFINE_CONST_FUN_OBJ_2(vectorio_rectangle_is_colliding_obj, vectorio_rectangle_obj_is_colliding); +MP_DEFINE_CONST_FUN_OBJ_2(vectorio_rectangle_intersecting_obj, vectorio_rectangle_obj_intersecting); // Documentation for properties inherited from VectorShape. @@ -172,7 +172,7 @@ static const mp_rom_map_elem_t vectorio_rectangle_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&vectorio_vector_shape_y_obj) }, { MP_ROM_QSTR(MP_QSTR_hidden), MP_ROM_PTR(&vectorio_vector_shape_hidden_obj) }, { MP_ROM_QSTR(MP_QSTR_color_index), MP_ROM_PTR(&vectorio_rectangle_color_index_obj) }, - { MP_ROM_QSTR(MP_QSTR_is_colliding), MP_ROM_PTR(&vectorio_rectangle_is_colliding_obj) }, + { MP_ROM_QSTR(MP_QSTR_intersecting), MP_ROM_PTR(&vectorio_rectangle_intersecting_obj) }, { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&vectorio_rectangle_width_obj) }, { MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&vectorio_rectangle_height_obj) }, { MP_ROM_QSTR(MP_QSTR_location), MP_ROM_PTR(&vectorio_vector_shape_location_obj) }, diff --git a/shared-bindings/vectorio/Rectangle.h b/shared-bindings/vectorio/Rectangle.h index b0e786a44d272..24570b9b036b4 100644 --- a/shared-bindings/vectorio/Rectangle.h +++ b/shared-bindings/vectorio/Rectangle.h @@ -30,4 +30,4 @@ void common_hal_vectorio_rectangle_set_color_index(void *obj, uint16_t color_ind int16_t common_hal_vectorio_rectangle_get_height(void *obj); void common_hal_vectorio_rectangle_set_height(void *obj, int16_t height); -bool common_hal_vectorio_rectangle_is_colliding(vectorio_rectangle_t *self, mp_obj_t other_shape); +bool common_hal_vectorio_rectangle_intersecting(vectorio_rectangle_t *self, mp_obj_t other_shape); diff --git a/shared-module/vectorio/Rectangle.c b/shared-module/vectorio/Rectangle.c index 1037d5f8c3a45..8d42e3f11ba68 100644 --- a/shared-module/vectorio/Rectangle.c +++ b/shared-module/vectorio/Rectangle.c @@ -51,7 +51,7 @@ mp_obj_t common_hal_vectorio_rectangle_get_draw_protocol(void *rectangle) { return self->draw_protocol_instance; } -bool common_hal_vectorio_rectangle_is_colliding(vectorio_rectangle_t *self, mp_obj_t other_shape) { +bool common_hal_vectorio_rectangle_intersecting(vectorio_rectangle_t *self, mp_obj_t other_shape) { mp_obj_t possible_rect = mp_obj_cast_to_native_base(other_shape, &vectorio_rectangle_type); if (possible_rect != MP_OBJ_NULL) { From b9e020af1e90bc71de80f6c255ebe908317ebc07 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Tue, 6 Aug 2024 20:23:55 -0500 Subject: [PATCH 04/17] disable vectorio for mini_sam_m4 --- ports/atmel-samd/boards/mini_sam_m4/mpconfigboard.mk | 1 + shared-bindings/vectorio/Rectangle.c | 10 +++++----- shared-bindings/vectorio/Rectangle.h | 2 +- shared-module/vectorio/Rectangle.c | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/ports/atmel-samd/boards/mini_sam_m4/mpconfigboard.mk b/ports/atmel-samd/boards/mini_sam_m4/mpconfigboard.mk index 933a0ee673ac4..e1ecfc91af5fe 100644 --- a/ports/atmel-samd/boards/mini_sam_m4/mpconfigboard.mk +++ b/ports/atmel-samd/boards/mini_sam_m4/mpconfigboard.mk @@ -12,6 +12,7 @@ LONGINT_IMPL = MPZ CIRCUITPY_SYNTHIO = 0 CIRCUITPY_FLOPPYIO = 0 +CIRCUITPY_VECTORIO = 0 CIRCUITPY_BITBANG_APA102 = 1 diff --git a/shared-bindings/vectorio/Rectangle.c b/shared-bindings/vectorio/Rectangle.c index 6ae07822d148b..22e11c4e680b1 100644 --- a/shared-bindings/vectorio/Rectangle.c +++ b/shared-bindings/vectorio/Rectangle.c @@ -136,14 +136,14 @@ const mp_obj_property_t vectorio_rectangle_color_index_obj = { MP_ROM_NONE}, }; -//| def is_colliding(self, other_shape: Union[vectorio.Circle, vectorio.Rectangle]) -> bool: +//| def intersecting(self, other_shape: Union[vectorio.Circle, vectorio.Rectangle]) -> bool: //| """Return true if this shape is colliding with another shape.""" //| ... -static mp_obj_t vectorio_rectangle_obj_is_colliding(mp_obj_t self_in, mp_obj_t other_shape) { +static mp_obj_t vectorio_rectangle_obj_intersecting(mp_obj_t self_in, mp_obj_t other_shape) { vectorio_rectangle_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_bool(common_hal_vectorio_rectangle_is_colliding(self, other_shape)); + return mp_obj_new_bool(common_hal_vectorio_rectangle_intersecting(self, other_shape)); } -MP_DEFINE_CONST_FUN_OBJ_2(vectorio_rectangle_is_colliding_obj, vectorio_rectangle_obj_is_colliding); +MP_DEFINE_CONST_FUN_OBJ_2(vectorio_rectangle_intersecting_obj, vectorio_rectangle_obj_intersecting); // Documentation for properties inherited from VectorShape. @@ -172,7 +172,7 @@ static const mp_rom_map_elem_t vectorio_rectangle_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&vectorio_vector_shape_y_obj) }, { MP_ROM_QSTR(MP_QSTR_hidden), MP_ROM_PTR(&vectorio_vector_shape_hidden_obj) }, { MP_ROM_QSTR(MP_QSTR_color_index), MP_ROM_PTR(&vectorio_rectangle_color_index_obj) }, - { MP_ROM_QSTR(MP_QSTR_is_colliding), MP_ROM_PTR(&vectorio_rectangle_is_colliding_obj) }, + { MP_ROM_QSTR(MP_QSTR_intersecting), MP_ROM_PTR(&vectorio_rectangle_intersecting_obj) }, { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&vectorio_rectangle_width_obj) }, { MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&vectorio_rectangle_height_obj) }, { MP_ROM_QSTR(MP_QSTR_location), MP_ROM_PTR(&vectorio_vector_shape_location_obj) }, diff --git a/shared-bindings/vectorio/Rectangle.h b/shared-bindings/vectorio/Rectangle.h index b0e786a44d272..24570b9b036b4 100644 --- a/shared-bindings/vectorio/Rectangle.h +++ b/shared-bindings/vectorio/Rectangle.h @@ -30,4 +30,4 @@ void common_hal_vectorio_rectangle_set_color_index(void *obj, uint16_t color_ind int16_t common_hal_vectorio_rectangle_get_height(void *obj); void common_hal_vectorio_rectangle_set_height(void *obj, int16_t height); -bool common_hal_vectorio_rectangle_is_colliding(vectorio_rectangle_t *self, mp_obj_t other_shape); +bool common_hal_vectorio_rectangle_intersecting(vectorio_rectangle_t *self, mp_obj_t other_shape); diff --git a/shared-module/vectorio/Rectangle.c b/shared-module/vectorio/Rectangle.c index 1037d5f8c3a45..8d42e3f11ba68 100644 --- a/shared-module/vectorio/Rectangle.c +++ b/shared-module/vectorio/Rectangle.c @@ -51,7 +51,7 @@ mp_obj_t common_hal_vectorio_rectangle_get_draw_protocol(void *rectangle) { return self->draw_protocol_instance; } -bool common_hal_vectorio_rectangle_is_colliding(vectorio_rectangle_t *self, mp_obj_t other_shape) { +bool common_hal_vectorio_rectangle_intersecting(vectorio_rectangle_t *self, mp_obj_t other_shape) { mp_obj_t possible_rect = mp_obj_cast_to_native_base(other_shape, &vectorio_rectangle_type); if (possible_rect != MP_OBJ_NULL) { From 05d5c2ad6259b17336ead9f651147471bb33d5dd Mon Sep 17 00:00:00 2001 From: foamyguy Date: Tue, 6 Aug 2024 21:29:16 -0500 Subject: [PATCH 05/17] rename to intersects. add circle.intersects --- shared-bindings/vectorio/Circle.c | 10 +++++ shared-bindings/vectorio/Circle.h | 2 + shared-bindings/vectorio/Rectangle.c | 10 ++--- shared-bindings/vectorio/Rectangle.h | 2 +- shared-module/vectorio/Circle.c | 62 +++++++++++++++++++++++++++- shared-module/vectorio/Rectangle.c | 2 +- 6 files changed, 80 insertions(+), 8 deletions(-) diff --git a/shared-bindings/vectorio/Circle.c b/shared-bindings/vectorio/Circle.c index d2f31d6b86d82..acc379c3ef541 100644 --- a/shared-bindings/vectorio/Circle.c +++ b/shared-bindings/vectorio/Circle.c @@ -106,6 +106,15 @@ MP_PROPERTY_GETSET(vectorio_circle_color_index_obj, (mp_obj_t)&vectorio_circle_set_color_index_obj); +//| def intersects(self, other_shape: Union[vectorio.Circle, vectorio.Rectangle]) -> bool: +//| """Return true if this shape is colliding with another shape.""" +//| ... +static mp_obj_t vectorio_circle_obj_intersects(mp_obj_t self_in, mp_obj_t other_shape) { + vectorio_circle_t *self = MP_OBJ_TO_PTR(self_in); + return mp_obj_new_bool(common_hal_vectorio_circle_intersects(self, other_shape)); +} +MP_DEFINE_CONST_FUN_OBJ_2(vectorio_circle_intersects_obj, vectorio_circle_obj_intersects); + // Documentation for properties inherited from VectorShape. //| x: int @@ -133,6 +142,7 @@ static const mp_rom_map_elem_t vectorio_circle_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&vectorio_vector_shape_y_obj) }, { MP_ROM_QSTR(MP_QSTR_hidden), MP_ROM_PTR(&vectorio_vector_shape_hidden_obj) }, { MP_ROM_QSTR(MP_QSTR_color_index), MP_ROM_PTR(&vectorio_circle_color_index_obj) }, + { MP_ROM_QSTR(MP_QSTR_intersects), MP_ROM_PTR(&vectorio_circle_intersects_obj) }, { MP_ROM_QSTR(MP_QSTR_location), MP_ROM_PTR(&vectorio_vector_shape_location_obj) }, { MP_ROM_QSTR(MP_QSTR_pixel_shader), MP_ROM_PTR(&vectorio_vector_shape_pixel_shader_obj) }, }; diff --git a/shared-bindings/vectorio/Circle.h b/shared-bindings/vectorio/Circle.h index 2c72e81881cd0..269891d8196bb 100644 --- a/shared-bindings/vectorio/Circle.h +++ b/shared-bindings/vectorio/Circle.h @@ -28,3 +28,5 @@ uint16_t common_hal_vectorio_circle_get_color_index(void *obj); void common_hal_vectorio_circle_set_color_index(void *obj, uint16_t color_index); mp_obj_t common_hal_vectorio_circle_get_draw_protocol(void *circle); + +bool common_hal_vectorio_circle_intersects(vectorio_circle_t *self, mp_obj_t other_shape); diff --git a/shared-bindings/vectorio/Rectangle.c b/shared-bindings/vectorio/Rectangle.c index 22e11c4e680b1..70d251d6ba3b2 100644 --- a/shared-bindings/vectorio/Rectangle.c +++ b/shared-bindings/vectorio/Rectangle.c @@ -136,14 +136,14 @@ const mp_obj_property_t vectorio_rectangle_color_index_obj = { MP_ROM_NONE}, }; -//| def intersecting(self, other_shape: Union[vectorio.Circle, vectorio.Rectangle]) -> bool: +//| def intersects(self, other_shape: Union[vectorio.Circle, vectorio.Rectangle]) -> bool: //| """Return true if this shape is colliding with another shape.""" //| ... -static mp_obj_t vectorio_rectangle_obj_intersecting(mp_obj_t self_in, mp_obj_t other_shape) { +static mp_obj_t vectorio_rectangle_obj_intersects(mp_obj_t self_in, mp_obj_t other_shape) { vectorio_rectangle_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_bool(common_hal_vectorio_rectangle_intersecting(self, other_shape)); + return mp_obj_new_bool(common_hal_vectorio_rectangle_intersects(self, other_shape)); } -MP_DEFINE_CONST_FUN_OBJ_2(vectorio_rectangle_intersecting_obj, vectorio_rectangle_obj_intersecting); +MP_DEFINE_CONST_FUN_OBJ_2(vectorio_rectangle_intersects_obj, vectorio_rectangle_obj_intersects); // Documentation for properties inherited from VectorShape. @@ -172,7 +172,7 @@ static const mp_rom_map_elem_t vectorio_rectangle_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&vectorio_vector_shape_y_obj) }, { MP_ROM_QSTR(MP_QSTR_hidden), MP_ROM_PTR(&vectorio_vector_shape_hidden_obj) }, { MP_ROM_QSTR(MP_QSTR_color_index), MP_ROM_PTR(&vectorio_rectangle_color_index_obj) }, - { MP_ROM_QSTR(MP_QSTR_intersecting), MP_ROM_PTR(&vectorio_rectangle_intersecting_obj) }, + { MP_ROM_QSTR(MP_QSTR_intersects), MP_ROM_PTR(&vectorio_rectangle_intersects_obj) }, { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&vectorio_rectangle_width_obj) }, { MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&vectorio_rectangle_height_obj) }, { MP_ROM_QSTR(MP_QSTR_location), MP_ROM_PTR(&vectorio_vector_shape_location_obj) }, diff --git a/shared-bindings/vectorio/Rectangle.h b/shared-bindings/vectorio/Rectangle.h index 24570b9b036b4..26187eb716c48 100644 --- a/shared-bindings/vectorio/Rectangle.h +++ b/shared-bindings/vectorio/Rectangle.h @@ -30,4 +30,4 @@ void common_hal_vectorio_rectangle_set_color_index(void *obj, uint16_t color_ind int16_t common_hal_vectorio_rectangle_get_height(void *obj); void common_hal_vectorio_rectangle_set_height(void *obj, int16_t height); -bool common_hal_vectorio_rectangle_intersecting(vectorio_rectangle_t *self, mp_obj_t other_shape); +bool common_hal_vectorio_rectangle_intersects(vectorio_rectangle_t *self, mp_obj_t other_shape); diff --git a/shared-module/vectorio/Circle.c b/shared-module/vectorio/Circle.c index 6f7ee2f3406ea..6c02bc64c86f6 100644 --- a/shared-module/vectorio/Circle.c +++ b/shared-module/vectorio/Circle.c @@ -7,9 +7,11 @@ #include "shared-bindings/vectorio/Circle.h" #include "shared-module/vectorio/__init__.h" #include "shared-module/displayio/area.h" - +#include "shared-bindings/vectorio/Rectangle.h" +#include "shared-bindings/vectorio/VectorShape.h" #include "py/runtime.h" #include "stdlib.h" +#include void common_hal_vectorio_circle_construct(vectorio_circle_t *self, uint16_t radius, uint16_t color_index) { @@ -79,6 +81,64 @@ void common_hal_vectorio_circle_set_color_index(void *obj, uint16_t color_index) } } +bool common_hal_vectorio_circle_intersects(vectorio_circle_t *self, mp_obj_t other_shape) { + + mp_obj_t possible_rect = mp_obj_cast_to_native_base(other_shape, &vectorio_rectangle_type); + if (possible_rect != MP_OBJ_NULL) { + + vectorio_rectangle_t *other_rect = possible_rect; + + mp_int_t self_x = common_hal_vectorio_vector_shape_get_x(self->draw_protocol_instance); + mp_int_t self_y = common_hal_vectorio_vector_shape_get_y(self->draw_protocol_instance); + + mp_int_t other_left = common_hal_vectorio_vector_shape_get_x(other_rect->draw_protocol_instance); + mp_int_t other_right = other_left + other_rect->width; + mp_int_t other_top = common_hal_vectorio_vector_shape_get_y(other_rect->draw_protocol_instance); + mp_int_t other_bottom = other_top + other_rect->height; + + mp_int_t test_x = self_x; + mp_int_t test_y = self_y; + + if (self_x < other_left) { + test_x = other_left; + } else if (self_x > other_right) { + test_x = other_right; + } + + if (self_y < other_top) { + test_y = other_top; + } else if (self_y > other_bottom) { + test_y = other_bottom; + } + + mp_int_t dist_x = self_x - test_x; + mp_int_t dist_y = self_y - test_y; + mp_int_t dist = sqrtf((dist_x * dist_x) + (dist_y * dist_y)); + + return dist <= self->radius; + } + mp_obj_t possible_circle = mp_obj_cast_to_native_base(other_shape, &vectorio_circle_type); + if (possible_circle != MP_OBJ_NULL) { + vectorio_circle_t *other_circle = possible_circle; + + mp_int_t self_x = common_hal_vectorio_vector_shape_get_x(self->draw_protocol_instance); + mp_int_t self_y = common_hal_vectorio_vector_shape_get_y(self->draw_protocol_instance); + + mp_int_t other_circle_x = common_hal_vectorio_vector_shape_get_x(other_circle->draw_protocol_instance); + mp_int_t other_circle_y = common_hal_vectorio_vector_shape_get_y(other_circle->draw_protocol_instance); + + mp_int_t dist_x = self_x - other_circle_x; + mp_int_t dist_y = self_y - other_circle_y; + + mp_int_t dist = sqrtf((dist_x * dist_x) + (dist_y * dist_y)); + + return (dist <= self->radius + other_circle->radius); + + } + + return false; +} + mp_obj_t common_hal_vectorio_circle_get_draw_protocol(void *circle) { vectorio_circle_t *self = circle; return self->draw_protocol_instance; diff --git a/shared-module/vectorio/Rectangle.c b/shared-module/vectorio/Rectangle.c index 8d42e3f11ba68..47d81d105792d 100644 --- a/shared-module/vectorio/Rectangle.c +++ b/shared-module/vectorio/Rectangle.c @@ -51,7 +51,7 @@ mp_obj_t common_hal_vectorio_rectangle_get_draw_protocol(void *rectangle) { return self->draw_protocol_instance; } -bool common_hal_vectorio_rectangle_intersecting(vectorio_rectangle_t *self, mp_obj_t other_shape) { +bool common_hal_vectorio_rectangle_intersects(vectorio_rectangle_t *self, mp_obj_t other_shape) { mp_obj_t possible_rect = mp_obj_cast_to_native_base(other_shape, &vectorio_rectangle_type); if (possible_rect != MP_OBJ_NULL) { From 8f12113da454aa4eb03baff39f24c8fc91034691 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Tue, 6 Aug 2024 21:30:55 -0500 Subject: [PATCH 06/17] remove redundant parens --- shared-module/vectorio/Circle.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared-module/vectorio/Circle.c b/shared-module/vectorio/Circle.c index 6c02bc64c86f6..2f78756573dfe 100644 --- a/shared-module/vectorio/Circle.c +++ b/shared-module/vectorio/Circle.c @@ -132,7 +132,7 @@ bool common_hal_vectorio_circle_intersects(vectorio_circle_t *self, mp_obj_t oth mp_int_t dist = sqrtf((dist_x * dist_x) + (dist_y * dist_y)); - return (dist <= self->radius + other_circle->radius); + return dist <= self->radius + other_circle->radius; } From 3ee0f4d4c7ad70efab157b3b39d3112a093550f0 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Tue, 6 Aug 2024 21:32:43 -0500 Subject: [PATCH 07/17] update docstring to intersects Co-authored-by: Dan Halbert --- shared-bindings/vectorio/Circle.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared-bindings/vectorio/Circle.c b/shared-bindings/vectorio/Circle.c index acc379c3ef541..c30c7b4e8987f 100644 --- a/shared-bindings/vectorio/Circle.c +++ b/shared-bindings/vectorio/Circle.c @@ -107,7 +107,7 @@ MP_PROPERTY_GETSET(vectorio_circle_color_index_obj, //| def intersects(self, other_shape: Union[vectorio.Circle, vectorio.Rectangle]) -> bool: -//| """Return true if this shape is colliding with another shape.""" +//| """Return true if this shape intersects (overlaps with) another shape.""" //| ... static mp_obj_t vectorio_circle_obj_intersects(mp_obj_t self_in, mp_obj_t other_shape) { vectorio_circle_t *self = MP_OBJ_TO_PTR(self_in); From 9e94db992475f46833d4788288a8fdbb7caf7a61 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Tue, 6 Aug 2024 21:33:36 -0500 Subject: [PATCH 08/17] update docstring rectangle --- shared-bindings/vectorio/Rectangle.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared-bindings/vectorio/Rectangle.c b/shared-bindings/vectorio/Rectangle.c index 70d251d6ba3b2..d9113046792ff 100644 --- a/shared-bindings/vectorio/Rectangle.c +++ b/shared-bindings/vectorio/Rectangle.c @@ -137,7 +137,7 @@ const mp_obj_property_t vectorio_rectangle_color_index_obj = { }; //| def intersects(self, other_shape: Union[vectorio.Circle, vectorio.Rectangle]) -> bool: -//| """Return true if this shape is colliding with another shape.""" +//| """Return true if this shape intersects (overlaps with) another shape.""" //| ... static mp_obj_t vectorio_rectangle_obj_intersects(mp_obj_t self_in, mp_obj_t other_shape) { vectorio_rectangle_t *self = MP_OBJ_TO_PTR(self_in); From f11cd0c40507c986838743a6e83feed7a3654c6f Mon Sep 17 00:00:00 2001 From: foamyguy Date: Wed, 7 Aug 2024 07:45:02 -0500 Subject: [PATCH 09/17] disable vectorio on boards without room --- ports/espressif/boards/beetle-esp32-c3/mpconfigboard.mk | 1 + ports/espressif/boards/lolin_c3_mini/mpconfigboard.mk | 1 + ports/espressif/boards/microdev_micro_c3/mpconfigboard.mk | 1 + ports/nordic/boards/ohs2020_badge/mpconfigboard.mk | 1 + ports/nordic/boards/supermini_nrf52840/mpconfigboard.mk | 1 + 5 files changed, 5 insertions(+) diff --git a/ports/espressif/boards/beetle-esp32-c3/mpconfigboard.mk b/ports/espressif/boards/beetle-esp32-c3/mpconfigboard.mk index a03e6c32eb2c6..cb81ac76b0ceb 100644 --- a/ports/espressif/boards/beetle-esp32-c3/mpconfigboard.mk +++ b/ports/espressif/boards/beetle-esp32-c3/mpconfigboard.mk @@ -11,3 +11,4 @@ CIRCUITPY_ESP_FLASH_SIZE=4MB CIRCUITPY_LEGACY_4MB_FLASH_LAYOUT = 1 CIRCUITPY_ESP_USB_SERIAL_JTAG = 1 +CIRCUITPY_VECTORIO = 0 diff --git a/ports/espressif/boards/lolin_c3_mini/mpconfigboard.mk b/ports/espressif/boards/lolin_c3_mini/mpconfigboard.mk index 36dc38883ff39..3dff7069ea02e 100644 --- a/ports/espressif/boards/lolin_c3_mini/mpconfigboard.mk +++ b/ports/espressif/boards/lolin_c3_mini/mpconfigboard.mk @@ -10,3 +10,4 @@ CIRCUITPY_ESP_FLASH_SIZE=4MB CIRCUITPY_LEGACY_4MB_FLASH_LAYOUT = 1 CIRCUITPY_ESP_USB_SERIAL_JTAG = 1 +CIRCUITPY_VECTORIO = 0 diff --git a/ports/espressif/boards/microdev_micro_c3/mpconfigboard.mk b/ports/espressif/boards/microdev_micro_c3/mpconfigboard.mk index b2ea6005db53a..fa0d5054784d3 100644 --- a/ports/espressif/boards/microdev_micro_c3/mpconfigboard.mk +++ b/ports/espressif/boards/microdev_micro_c3/mpconfigboard.mk @@ -10,3 +10,4 @@ CIRCUITPY_ESP_FLASH_SIZE = 4MB CIRCUITPY_LEGACY_4MB_FLASH_LAYOUT = 1 CIRCUITPY_ESP_USB_SERIAL_JTAG = 1 +CIRCUITPY_VECTORIO = 0 diff --git a/ports/nordic/boards/ohs2020_badge/mpconfigboard.mk b/ports/nordic/boards/ohs2020_badge/mpconfigboard.mk index 045939e65252c..bdba4575090f4 100644 --- a/ports/nordic/boards/ohs2020_badge/mpconfigboard.mk +++ b/ports/nordic/boards/ohs2020_badge/mpconfigboard.mk @@ -7,3 +7,4 @@ MCU_CHIP = nrf52840 QSPI_FLASH_FILESYSTEM = 1 EXTERNAL_FLASH_DEVICES = "W25Q128JVxQ" +CIRCUITPY_VECTORIO = 0 diff --git a/ports/nordic/boards/supermini_nrf52840/mpconfigboard.mk b/ports/nordic/boards/supermini_nrf52840/mpconfigboard.mk index 699d19b7706ba..9a675bd3e108b 100644 --- a/ports/nordic/boards/supermini_nrf52840/mpconfigboard.mk +++ b/ports/nordic/boards/supermini_nrf52840/mpconfigboard.mk @@ -6,3 +6,4 @@ USB_MANUFACTURER = "ICBbuy" MCU_CHIP = nrf52840 INTERNAL_FLASH_FILESYSTEM = 1 +CIRCUITPY_VECTORIO = 0 From 2ba399d9a5cca2d4a7a918bdb3c0afd698798f39 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Wed, 7 Aug 2024 07:58:32 -0500 Subject: [PATCH 10/17] revert disable vectorio --- ports/espressif/boards/beetle-esp32-c3/mpconfigboard.mk | 1 - ports/espressif/boards/lolin_c3_mini/mpconfigboard.mk | 1 - ports/espressif/boards/microdev_micro_c3/mpconfigboard.mk | 1 - ports/nordic/boards/ohs2020_badge/mpconfigboard.mk | 1 - ports/nordic/boards/supermini_nrf52840/mpconfigboard.mk | 1 - 5 files changed, 5 deletions(-) diff --git a/ports/espressif/boards/beetle-esp32-c3/mpconfigboard.mk b/ports/espressif/boards/beetle-esp32-c3/mpconfigboard.mk index cb81ac76b0ceb..a03e6c32eb2c6 100644 --- a/ports/espressif/boards/beetle-esp32-c3/mpconfigboard.mk +++ b/ports/espressif/boards/beetle-esp32-c3/mpconfigboard.mk @@ -11,4 +11,3 @@ CIRCUITPY_ESP_FLASH_SIZE=4MB CIRCUITPY_LEGACY_4MB_FLASH_LAYOUT = 1 CIRCUITPY_ESP_USB_SERIAL_JTAG = 1 -CIRCUITPY_VECTORIO = 0 diff --git a/ports/espressif/boards/lolin_c3_mini/mpconfigboard.mk b/ports/espressif/boards/lolin_c3_mini/mpconfigboard.mk index 3dff7069ea02e..36dc38883ff39 100644 --- a/ports/espressif/boards/lolin_c3_mini/mpconfigboard.mk +++ b/ports/espressif/boards/lolin_c3_mini/mpconfigboard.mk @@ -10,4 +10,3 @@ CIRCUITPY_ESP_FLASH_SIZE=4MB CIRCUITPY_LEGACY_4MB_FLASH_LAYOUT = 1 CIRCUITPY_ESP_USB_SERIAL_JTAG = 1 -CIRCUITPY_VECTORIO = 0 diff --git a/ports/espressif/boards/microdev_micro_c3/mpconfigboard.mk b/ports/espressif/boards/microdev_micro_c3/mpconfigboard.mk index fa0d5054784d3..b2ea6005db53a 100644 --- a/ports/espressif/boards/microdev_micro_c3/mpconfigboard.mk +++ b/ports/espressif/boards/microdev_micro_c3/mpconfigboard.mk @@ -10,4 +10,3 @@ CIRCUITPY_ESP_FLASH_SIZE = 4MB CIRCUITPY_LEGACY_4MB_FLASH_LAYOUT = 1 CIRCUITPY_ESP_USB_SERIAL_JTAG = 1 -CIRCUITPY_VECTORIO = 0 diff --git a/ports/nordic/boards/ohs2020_badge/mpconfigboard.mk b/ports/nordic/boards/ohs2020_badge/mpconfigboard.mk index bdba4575090f4..045939e65252c 100644 --- a/ports/nordic/boards/ohs2020_badge/mpconfigboard.mk +++ b/ports/nordic/boards/ohs2020_badge/mpconfigboard.mk @@ -7,4 +7,3 @@ MCU_CHIP = nrf52840 QSPI_FLASH_FILESYSTEM = 1 EXTERNAL_FLASH_DEVICES = "W25Q128JVxQ" -CIRCUITPY_VECTORIO = 0 diff --git a/ports/nordic/boards/supermini_nrf52840/mpconfigboard.mk b/ports/nordic/boards/supermini_nrf52840/mpconfigboard.mk index 9a675bd3e108b..699d19b7706ba 100644 --- a/ports/nordic/boards/supermini_nrf52840/mpconfigboard.mk +++ b/ports/nordic/boards/supermini_nrf52840/mpconfigboard.mk @@ -6,4 +6,3 @@ USB_MANUFACTURER = "ICBbuy" MCU_CHIP = nrf52840 INTERNAL_FLASH_FILESYSTEM = 1 -CIRCUITPY_VECTORIO = 0 From c44edcb2e360d1605f7dc99ecf3a3c17627a38df Mon Sep 17 00:00:00 2001 From: foamyguy Date: Fri, 30 Aug 2024 18:00:11 -0500 Subject: [PATCH 11/17] starting refactor to module functions instead of class functions --- shared-bindings/vectorio/__init__.c | 141 ++++++++++++++++++++++++++++ shared-bindings/vectorio/__init__.h | 12 +++ shared-module/vectorio/__init__.c | 69 ++++++++++++++ 3 files changed, 222 insertions(+) diff --git a/shared-bindings/vectorio/__init__.c b/shared-bindings/vectorio/__init__.c index 7e9473eefdcb5..009295f6b1fb4 100644 --- a/shared-bindings/vectorio/__init__.c +++ b/shared-bindings/vectorio/__init__.c @@ -9,9 +9,11 @@ #include "py/obj.h" #include "py/runtime.h" + #include "shared-bindings/vectorio/Circle.h" #include "shared-bindings/vectorio/Polygon.h" #include "shared-bindings/vectorio/Rectangle.h" +#include "shared-bindings/vectorio/__init__.h" //| """Lightweight 2D shapes for displays //| @@ -37,8 +39,147 @@ //| //| """ + +//| def circle_rectangle_intersects( +//| cx: int, cy: int, cr: int, rx: int, ry: int, rw: int, rh: int +//| :param int cx: Circle center x coordinate +//| :param int cy: Circle center y coordinate +//| :param int cr: Circle radius +//| :param int rx: Rectangle x coordinate +//| :param int ry: Rectangle y coordinate +//| :param int rw: Rectangle width +//| :param int rh: Rectangle height +//| ) -> None: +static mp_obj_t vectorio_circle_rectangle_intersects(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum {ARG_cx, ARG_cy, ARG_cr, ARG_rx, ARG_ry, ARG_rw, ARG_rh}; + + static const mp_arg_t allowed_args[] = { + {MP_QSTR_cx, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_cy, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_cr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_rx, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_ry, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_rw, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_rh, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + }; + + 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); + + int16_t cx = args[ARG_cx].u_int; + int16_t cy = args[ARG_cy].u_int; + int16_t cr = args[ARG_cr].u_int; + int16_t rx = args[ARG_rx].u_int; + int16_t ry = args[ARG_ry].u_int; + int16_t rw = args[ARG_rw].u_int; + int16_t rh = args[ARG_rh].u_int; + + bool result = common_hal_vectorio_circle_rectangle_intersects(cx, cy, cr, rx, ry, rw, rh); + if (result){ + return mp_const_true; + }else{ + return mp_const_false; + } +} +MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_circle_rectangle_intersects_obj, 0, vectorio_circle_rectangle_intersects); + + +//| def rectangle_rectangle_intersects( +//| r1x: int, r1y: int, r1w: int, r1h: int, +// r2x: int, r2y: int, r2w: int, r2h: int +//| +//| :param int r1x: Rectangle x coordinate +//| :param int r1y: Rectangle y coordinate +//| :param int r1w: Rectangle width +//| :param int r1h: Rectangle height +//| :param int r2x: Other Rectangle x coordinate +//| :param int r2y: Other Rectangle y coordinate +//| :param int r2w: Other Rectangle width +//| :param int r2h: Other Rectangle height +//| ) -> None: +static mp_obj_t vectorio_rectangle_rectangle_intersects(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum {ARG_r1x, ARG_r1y, ARG_r1w, ARG_r1h, ARG_r2x, ARG_r2y, ARG_r2w, ARG_r2h}; + + static const mp_arg_t allowed_args[] = { + {MP_QSTR_r1x, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_r1y, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_r1w, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_r1h, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_r2x, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_r2y, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_r2w, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_r2h, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + }; + + 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); + + int16_t r1x = args[ARG_r1x].u_int; + int16_t r1y = args[ARG_r1y].u_int; + int16_t r1w = args[ARG_r1w].u_int; + int16_t r1h = args[ARG_r1h].u_int; + + int16_t r2x = args[ARG_r2x].u_int; + int16_t r2y = args[ARG_r2y].u_int; + int16_t r2w = args[ARG_r2w].u_int; + int16_t r2h = args[ARG_r2h].u_int; + + + bool result = common_hal_vectorio_rectangle_rectangle_intersects(r1x, r1y, r1w, r1h, r2x, r2y, r2w, r2h); + if (result){ + return mp_const_true; + }else{ + return mp_const_false; + } +} +MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_rectangle_rectangle_intersects_obj, 0, vectorio_rectangle_rectangle_intersects); + + +//| def circle_circle_intersects( +//| cx: int, cy: int, cr: int, rx: int, ry: int, rw: int, rh: int +//| :param int c1x: Circle center x coordinate +//| :param int c1y: Circle center y coordinate +//| :param int c1r: Circle radius +//| :param int c2x: Other Circle center x coordinate +//| :param int c2y: Other Circle center y coordinate +//| :param int c2r: Other Circle radius +//| ) -> None: +static mp_obj_t vectorio_circle_circle_intersects(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum {ARG_c1x, ARG_c1y, ARG_c1r, ARG_c2x, ARG_c2y, ARG_c2r}; + + static const mp_arg_t allowed_args[] = { + {MP_QSTR_c1x, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_c1y, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_c1r, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_c2x, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_c2y, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_c2r, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + }; + + 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); + + int16_t c1x = args[ARG_c1x].u_int; + int16_t c1y = args[ARG_c1y].u_int; + int16_t c1r = args[ARG_c1r].u_int; + int16_t c2x = args[ARG_c2x].u_int; + int16_t c2y = args[ARG_c2y].u_int; + int16_t c2r = args[ARG_c2r].u_int; + + bool result = common_hal_vectorio_circle_circle_intersects(c1x, c1y, c1r, c2x, c2y, c2r); + if (result){ + return mp_const_true; + }else{ + return mp_const_false; + } +} +MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_circle_circle_intersects_obj, 0, vectorio_circle_circle_intersects); + static const mp_rom_map_elem_t vectorio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_vectorio) }, + { MP_ROM_QSTR(MP_QSTR_circle_rectangle_intersects), MP_ROM_PTR(&vectorio_circle_rectangle_intersects_obj) }, + { MP_ROM_QSTR(MP_QSTR_circle_circle_intersects), MP_ROM_PTR(&vectorio_circle_circle_intersects_obj) }, + { MP_ROM_QSTR(MP_QSTR_rectangle_rectangle_intersects), MP_ROM_PTR(&vectorio_rectangle_rectangle_intersects_obj) }, { MP_ROM_QSTR(MP_QSTR_Circle), MP_ROM_PTR(&vectorio_circle_type) }, { MP_ROM_QSTR(MP_QSTR_Polygon), MP_ROM_PTR(&vectorio_polygon_type) }, { MP_ROM_QSTR(MP_QSTR_Rectangle), MP_ROM_PTR(&vectorio_rectangle_type) }, diff --git a/shared-bindings/vectorio/__init__.h b/shared-bindings/vectorio/__init__.h index c982f63815b4d..48b97ff795f64 100644 --- a/shared-bindings/vectorio/__init__.h +++ b/shared-bindings/vectorio/__init__.h @@ -44,3 +44,15 @@ typedef struct _vectorio_draw_protocol_t { // Implementation functions for the draw protocol vectorio_draw_protocol_impl_t *draw_protocol_impl; } vectorio_draw_protocol_t; + +bool common_hal_vectorio_circle_rectangle_intersects( + int16_t cx, int16_t cy, int16_t cr, + int16_t rx, int16_t ry, int16_t rw, int16_t rh); + +bool common_hal_vectorio_rectangle_rectangle_intersects( + int16_t r1x, int16_t r1y, int16_t r1w, int16_t r1h, + int16_t r2x, int16_t r2y, int16_t r2w, int16_t r2h); + +bool common_hal_vectorio_circle_circle_intersects( + int16_t c1x, int16_t c1y, int16_t c1r, + int16_t c2x, int16_t c2y, int16_t c2r); diff --git a/shared-module/vectorio/__init__.c b/shared-module/vectorio/__init__.c index 0c16efad6eda5..dca6bf60b8f21 100644 --- a/shared-module/vectorio/__init__.c +++ b/shared-module/vectorio/__init__.c @@ -5,3 +5,72 @@ // SPDX-License-Identifier: MIT // Don't need anything in here yet + +#include "shared-bindings/vectorio/__init__.h" +#include "py/runtime.h" +#include "stdlib.h" +#include + +bool common_hal_vectorio_circle_rectangle_intersects( + int16_t cx, int16_t cy, int16_t cr, + int16_t rx, int16_t ry, int16_t rw, int16_t rh){ + + mp_int_t rect_left = rx; + mp_int_t rect_right = rect_left + rw; + mp_int_t rect_top = ry; + mp_int_t rect_bottom = rect_top + rh; + + mp_int_t test_x = cx; + mp_int_t test_y = cy; + + if (cx < rect_left) { + test_x = rect_left; + } else if (cx > rect_right) { + test_x = rect_right; + } + + if (cy < rect_top) { + test_y = rect_top; + } else if (cy > rect_bottom) { + test_y = rect_bottom; + } + + mp_int_t dist_x = cx - test_x; + mp_int_t dist_y = cy - test_y; + mp_int_t dist = (dist_x * dist_x) + (dist_y * dist_y); + + return dist <= cr*cr; +} + +bool common_hal_vectorio_circle_circle_intersects( + int16_t c1x, int16_t c1y, int16_t c1r, + int16_t c2x, int16_t c2y, int16_t c2r +){ + + mp_int_t dist_x = c1x - c2x; + mp_int_t dist_y = c1y - c2y; + + mp_int_t dist = (dist_x * dist_x) + (dist_y * dist_y); + + return dist <= (c1r + c2r) * (c1r + c2r); + +} + +bool common_hal_vectorio_rectangle_rectangle_intersects( + int16_t r1x, int16_t r1y, int16_t r1w, int16_t r1h, + int16_t r2x, int16_t r2y, int16_t r2w, int16_t r2h){ + + + mp_int_t r1_left = r1x; + mp_int_t r1_right = r1_left + r1w; + mp_int_t r1_top = r1y; + mp_int_t r1_bottom = r1_top + r1h; + + mp_int_t r2_left = r2x; + mp_int_t r2_right = r2_left + r2w; + mp_int_t r2_top = r2y; + mp_int_t r2_bottom = r2_top + r2h; + + return r1_left < r2_right && r1_right > r2_left && + r1_top < r2_bottom && r1_bottom > r2_top; +} \ No newline at end of file From 5db420a4a2348da96d81418bd4d5c45c19be544b Mon Sep 17 00:00:00 2001 From: foamyguy Date: Mon, 2 Sep 2024 10:56:47 -0500 Subject: [PATCH 12/17] remove vectorio object arg implementations. Fix docstrings. --- shared-bindings/vectorio/Circle.c | 10 ----- shared-bindings/vectorio/Circle.h | 2 - shared-bindings/vectorio/Rectangle.c | 10 ----- shared-bindings/vectorio/Rectangle.h | 2 - shared-bindings/vectorio/__init__.c | 66 ++++++++++++++++------------ shared-module/vectorio/Circle.c | 57 ------------------------ shared-module/vectorio/Rectangle.c | 56 ----------------------- shared-module/vectorio/__init__.c | 10 ++--- 8 files changed, 43 insertions(+), 170 deletions(-) diff --git a/shared-bindings/vectorio/Circle.c b/shared-bindings/vectorio/Circle.c index c30c7b4e8987f..d2f31d6b86d82 100644 --- a/shared-bindings/vectorio/Circle.c +++ b/shared-bindings/vectorio/Circle.c @@ -106,15 +106,6 @@ MP_PROPERTY_GETSET(vectorio_circle_color_index_obj, (mp_obj_t)&vectorio_circle_set_color_index_obj); -//| def intersects(self, other_shape: Union[vectorio.Circle, vectorio.Rectangle]) -> bool: -//| """Return true if this shape intersects (overlaps with) another shape.""" -//| ... -static mp_obj_t vectorio_circle_obj_intersects(mp_obj_t self_in, mp_obj_t other_shape) { - vectorio_circle_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_bool(common_hal_vectorio_circle_intersects(self, other_shape)); -} -MP_DEFINE_CONST_FUN_OBJ_2(vectorio_circle_intersects_obj, vectorio_circle_obj_intersects); - // Documentation for properties inherited from VectorShape. //| x: int @@ -142,7 +133,6 @@ static const mp_rom_map_elem_t vectorio_circle_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&vectorio_vector_shape_y_obj) }, { MP_ROM_QSTR(MP_QSTR_hidden), MP_ROM_PTR(&vectorio_vector_shape_hidden_obj) }, { MP_ROM_QSTR(MP_QSTR_color_index), MP_ROM_PTR(&vectorio_circle_color_index_obj) }, - { MP_ROM_QSTR(MP_QSTR_intersects), MP_ROM_PTR(&vectorio_circle_intersects_obj) }, { MP_ROM_QSTR(MP_QSTR_location), MP_ROM_PTR(&vectorio_vector_shape_location_obj) }, { MP_ROM_QSTR(MP_QSTR_pixel_shader), MP_ROM_PTR(&vectorio_vector_shape_pixel_shader_obj) }, }; diff --git a/shared-bindings/vectorio/Circle.h b/shared-bindings/vectorio/Circle.h index 269891d8196bb..2c72e81881cd0 100644 --- a/shared-bindings/vectorio/Circle.h +++ b/shared-bindings/vectorio/Circle.h @@ -28,5 +28,3 @@ uint16_t common_hal_vectorio_circle_get_color_index(void *obj); void common_hal_vectorio_circle_set_color_index(void *obj, uint16_t color_index); mp_obj_t common_hal_vectorio_circle_get_draw_protocol(void *circle); - -bool common_hal_vectorio_circle_intersects(vectorio_circle_t *self, mp_obj_t other_shape); diff --git a/shared-bindings/vectorio/Rectangle.c b/shared-bindings/vectorio/Rectangle.c index d9113046792ff..aa44885f2128f 100644 --- a/shared-bindings/vectorio/Rectangle.c +++ b/shared-bindings/vectorio/Rectangle.c @@ -136,15 +136,6 @@ const mp_obj_property_t vectorio_rectangle_color_index_obj = { MP_ROM_NONE}, }; -//| def intersects(self, other_shape: Union[vectorio.Circle, vectorio.Rectangle]) -> bool: -//| """Return true if this shape intersects (overlaps with) another shape.""" -//| ... -static mp_obj_t vectorio_rectangle_obj_intersects(mp_obj_t self_in, mp_obj_t other_shape) { - vectorio_rectangle_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_bool(common_hal_vectorio_rectangle_intersects(self, other_shape)); -} -MP_DEFINE_CONST_FUN_OBJ_2(vectorio_rectangle_intersects_obj, vectorio_rectangle_obj_intersects); - // Documentation for properties inherited from VectorShape. @@ -172,7 +163,6 @@ static const mp_rom_map_elem_t vectorio_rectangle_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&vectorio_vector_shape_y_obj) }, { MP_ROM_QSTR(MP_QSTR_hidden), MP_ROM_PTR(&vectorio_vector_shape_hidden_obj) }, { MP_ROM_QSTR(MP_QSTR_color_index), MP_ROM_PTR(&vectorio_rectangle_color_index_obj) }, - { MP_ROM_QSTR(MP_QSTR_intersects), MP_ROM_PTR(&vectorio_rectangle_intersects_obj) }, { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&vectorio_rectangle_width_obj) }, { MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&vectorio_rectangle_height_obj) }, { MP_ROM_QSTR(MP_QSTR_location), MP_ROM_PTR(&vectorio_vector_shape_location_obj) }, diff --git a/shared-bindings/vectorio/Rectangle.h b/shared-bindings/vectorio/Rectangle.h index 26187eb716c48..6831fa21029a7 100644 --- a/shared-bindings/vectorio/Rectangle.h +++ b/shared-bindings/vectorio/Rectangle.h @@ -29,5 +29,3 @@ void common_hal_vectorio_rectangle_set_color_index(void *obj, uint16_t color_ind int16_t common_hal_vectorio_rectangle_get_height(void *obj); void common_hal_vectorio_rectangle_set_height(void *obj, int16_t height); - -bool common_hal_vectorio_rectangle_intersects(vectorio_rectangle_t *self, mp_obj_t other_shape); diff --git a/shared-bindings/vectorio/__init__.c b/shared-bindings/vectorio/__init__.c index 009295f6b1fb4..8bf07c5c6a408 100644 --- a/shared-bindings/vectorio/__init__.c +++ b/shared-bindings/vectorio/__init__.c @@ -38,18 +38,23 @@ //| group.append(polygon) //| //| """ +//| //| def circle_rectangle_intersects( //| cx: int, cy: int, cr: int, rx: int, ry: int, rw: int, rh: int +//| ) -> bool: +//| """Checks for intersection between a cricle and a rectangle. +//| //| :param int cx: Circle center x coordinate //| :param int cy: Circle center y coordinate //| :param int cr: Circle radius //| :param int rx: Rectangle x coordinate //| :param int ry: Rectangle y coordinate //| :param int rw: Rectangle width -//| :param int rh: Rectangle height -//| ) -> None: +//| :param int rh: Rectangle height""" +//| ... +//| static mp_obj_t vectorio_circle_rectangle_intersects(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum {ARG_cx, ARG_cy, ARG_cr, ARG_rx, ARG_ry, ARG_rw, ARG_rh}; @@ -75,9 +80,9 @@ static mp_obj_t vectorio_circle_rectangle_intersects(size_t n_args, const mp_obj int16_t rh = args[ARG_rh].u_int; bool result = common_hal_vectorio_circle_rectangle_intersects(cx, cy, cr, rx, ry, rw, rh); - if (result){ + if (result) { return mp_const_true; - }else{ + } else { return mp_const_false; } } @@ -85,18 +90,20 @@ MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_circle_rectangle_intersects_obj, 0, vectorio //| def rectangle_rectangle_intersects( -//| r1x: int, r1y: int, r1w: int, r1h: int, -// r2x: int, r2y: int, r2w: int, r2h: int +//| r1x: int, r1y: int, r1w: int, r1h: int, r2x: int, r2y: int, r2w: int, r2h: int +//| ) -> bool: +//| """Checks for intersection between a two rectangles. +//| +//| :param int r1x: First Rectangle x coordinate +//| :param int r1y: First Rectangle y coordinate +//| :param int r1w: First Rectangle width +//| :param int r1h: First Rectangle height +//| :param int r2x: Second Rectangle x coordinate +//| :param int r2y: Second Rectangle y coordinate +//| :param int r2w: Second Rectangle width +//| :param int r2h: Second Rectangle height""" +//| ... //| -//| :param int r1x: Rectangle x coordinate -//| :param int r1y: Rectangle y coordinate -//| :param int r1w: Rectangle width -//| :param int r1h: Rectangle height -//| :param int r2x: Other Rectangle x coordinate -//| :param int r2y: Other Rectangle y coordinate -//| :param int r2w: Other Rectangle width -//| :param int r2h: Other Rectangle height -//| ) -> None: static mp_obj_t vectorio_rectangle_rectangle_intersects(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum {ARG_r1x, ARG_r1y, ARG_r1w, ARG_r1h, ARG_r2x, ARG_r2y, ARG_r2w, ARG_r2h}; @@ -126,24 +133,27 @@ static mp_obj_t vectorio_rectangle_rectangle_intersects(size_t n_args, const mp_ bool result = common_hal_vectorio_rectangle_rectangle_intersects(r1x, r1y, r1w, r1h, r2x, r2y, r2w, r2h); - if (result){ + if (result) { return mp_const_true; - }else{ + } else { return mp_const_false; } } MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_rectangle_rectangle_intersects_obj, 0, vectorio_rectangle_rectangle_intersects); - //| def circle_circle_intersects( -//| cx: int, cy: int, cr: int, rx: int, ry: int, rw: int, rh: int -//| :param int c1x: Circle center x coordinate -//| :param int c1y: Circle center y coordinate -//| :param int c1r: Circle radius -//| :param int c2x: Other Circle center x coordinate -//| :param int c2y: Other Circle center y coordinate -//| :param int c2r: Other Circle radius -//| ) -> None: +//| c1x: int, c1y: int, c1r: int, c2x: int, c2y: int, c2r: int +//| ) -> bool: +//| """Checks for intersection between two circles. +//| +//| :param int c1x: First Circle center x coordinate +//| :param int c1y: First Circle center y coordinate +//| :param int c1r: First Circle radius +//| :param int c2x: Second Circle center x coordinate +//| :param int c2y: Second Circle center y coordinate +//| :param int c2r: Second Circle radius""" +//| ... +//| static mp_obj_t vectorio_circle_circle_intersects(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum {ARG_c1x, ARG_c1y, ARG_c1r, ARG_c2x, ARG_c2y, ARG_c2r}; @@ -167,9 +177,9 @@ static mp_obj_t vectorio_circle_circle_intersects(size_t n_args, const mp_obj_t int16_t c2r = args[ARG_c2r].u_int; bool result = common_hal_vectorio_circle_circle_intersects(c1x, c1y, c1r, c2x, c2y, c2r); - if (result){ + if (result) { return mp_const_true; - }else{ + } else { return mp_const_false; } } diff --git a/shared-module/vectorio/Circle.c b/shared-module/vectorio/Circle.c index 2f78756573dfe..fe50d84c2038f 100644 --- a/shared-module/vectorio/Circle.c +++ b/shared-module/vectorio/Circle.c @@ -81,63 +81,6 @@ void common_hal_vectorio_circle_set_color_index(void *obj, uint16_t color_index) } } -bool common_hal_vectorio_circle_intersects(vectorio_circle_t *self, mp_obj_t other_shape) { - - mp_obj_t possible_rect = mp_obj_cast_to_native_base(other_shape, &vectorio_rectangle_type); - if (possible_rect != MP_OBJ_NULL) { - - vectorio_rectangle_t *other_rect = possible_rect; - - mp_int_t self_x = common_hal_vectorio_vector_shape_get_x(self->draw_protocol_instance); - mp_int_t self_y = common_hal_vectorio_vector_shape_get_y(self->draw_protocol_instance); - - mp_int_t other_left = common_hal_vectorio_vector_shape_get_x(other_rect->draw_protocol_instance); - mp_int_t other_right = other_left + other_rect->width; - mp_int_t other_top = common_hal_vectorio_vector_shape_get_y(other_rect->draw_protocol_instance); - mp_int_t other_bottom = other_top + other_rect->height; - - mp_int_t test_x = self_x; - mp_int_t test_y = self_y; - - if (self_x < other_left) { - test_x = other_left; - } else if (self_x > other_right) { - test_x = other_right; - } - - if (self_y < other_top) { - test_y = other_top; - } else if (self_y > other_bottom) { - test_y = other_bottom; - } - - mp_int_t dist_x = self_x - test_x; - mp_int_t dist_y = self_y - test_y; - mp_int_t dist = sqrtf((dist_x * dist_x) + (dist_y * dist_y)); - - return dist <= self->radius; - } - mp_obj_t possible_circle = mp_obj_cast_to_native_base(other_shape, &vectorio_circle_type); - if (possible_circle != MP_OBJ_NULL) { - vectorio_circle_t *other_circle = possible_circle; - - mp_int_t self_x = common_hal_vectorio_vector_shape_get_x(self->draw_protocol_instance); - mp_int_t self_y = common_hal_vectorio_vector_shape_get_y(self->draw_protocol_instance); - - mp_int_t other_circle_x = common_hal_vectorio_vector_shape_get_x(other_circle->draw_protocol_instance); - mp_int_t other_circle_y = common_hal_vectorio_vector_shape_get_y(other_circle->draw_protocol_instance); - - mp_int_t dist_x = self_x - other_circle_x; - mp_int_t dist_y = self_y - other_circle_y; - - mp_int_t dist = sqrtf((dist_x * dist_x) + (dist_y * dist_y)); - - return dist <= self->radius + other_circle->radius; - - } - - return false; -} mp_obj_t common_hal_vectorio_circle_get_draw_protocol(void *circle) { vectorio_circle_t *self = circle; diff --git a/shared-module/vectorio/Rectangle.c b/shared-module/vectorio/Rectangle.c index 47d81d105792d..a4970b05f7e83 100644 --- a/shared-module/vectorio/Rectangle.c +++ b/shared-module/vectorio/Rectangle.c @@ -51,62 +51,6 @@ mp_obj_t common_hal_vectorio_rectangle_get_draw_protocol(void *rectangle) { return self->draw_protocol_instance; } -bool common_hal_vectorio_rectangle_intersects(vectorio_rectangle_t *self, mp_obj_t other_shape) { - - mp_obj_t possible_rect = mp_obj_cast_to_native_base(other_shape, &vectorio_rectangle_type); - if (possible_rect != MP_OBJ_NULL) { - - vectorio_rectangle_t *other_rect = possible_rect; - - mp_int_t self_left = common_hal_vectorio_vector_shape_get_x(self->draw_protocol_instance); - mp_int_t self_right = self_left + self->width; - mp_int_t self_top = common_hal_vectorio_vector_shape_get_y(self->draw_protocol_instance); - mp_int_t self_bottom = self_top + self->height; - - mp_int_t other_left = common_hal_vectorio_vector_shape_get_x(other_rect->draw_protocol_instance); - mp_int_t other_right = other_left + other_rect->width; - mp_int_t other_top = common_hal_vectorio_vector_shape_get_y(other_rect->draw_protocol_instance); - mp_int_t other_bottom = other_top + other_rect->height; - - return self_left < other_right && self_right > other_left && - self_top < other_bottom && self_bottom > other_top; - } - mp_obj_t possible_circle = mp_obj_cast_to_native_base(other_shape, &vectorio_circle_type); - if (possible_circle != MP_OBJ_NULL) { - vectorio_circle_t *other_circle = possible_circle; - - mp_int_t self_left = common_hal_vectorio_vector_shape_get_x(self->draw_protocol_instance); - mp_int_t self_right = self_left + self->width; - mp_int_t self_top = common_hal_vectorio_vector_shape_get_y(self->draw_protocol_instance); - mp_int_t self_bottom = self_top + self->height; - - mp_int_t other_circle_x = common_hal_vectorio_vector_shape_get_x(other_circle->draw_protocol_instance); - mp_int_t other_circle_y = common_hal_vectorio_vector_shape_get_y(other_circle->draw_protocol_instance); - - mp_int_t test_x = other_circle_x; - mp_int_t test_y = other_circle_y; - - if (other_circle_x < self_left) { - test_x = self_left; - } else if (other_circle_x > self_right) { - test_x = self_right; - } - - if (other_circle_y < self_top) { - test_y = self_top; - } else if (other_circle_y > self_bottom) { - test_y = self_bottom; - } - - mp_int_t dist_x = other_circle_x - test_x; - mp_int_t dist_y = other_circle_y - test_y; - mp_int_t dist = sqrtf((dist_x * dist_x) + (dist_y * dist_y)); - - return dist <= other_circle->radius; - } - - return false; -} int16_t common_hal_vectorio_rectangle_get_width(void *obj) { vectorio_rectangle_t *self = obj; diff --git a/shared-module/vectorio/__init__.c b/shared-module/vectorio/__init__.c index dca6bf60b8f21..51b6dda8869a3 100644 --- a/shared-module/vectorio/__init__.c +++ b/shared-module/vectorio/__init__.c @@ -13,7 +13,7 @@ bool common_hal_vectorio_circle_rectangle_intersects( int16_t cx, int16_t cy, int16_t cr, - int16_t rx, int16_t ry, int16_t rw, int16_t rh){ + int16_t rx, int16_t ry, int16_t rw, int16_t rh) { mp_int_t rect_left = rx; mp_int_t rect_right = rect_left + rw; @@ -39,13 +39,13 @@ bool common_hal_vectorio_circle_rectangle_intersects( mp_int_t dist_y = cy - test_y; mp_int_t dist = (dist_x * dist_x) + (dist_y * dist_y); - return dist <= cr*cr; + return dist <= cr * cr; } bool common_hal_vectorio_circle_circle_intersects( int16_t c1x, int16_t c1y, int16_t c1r, int16_t c2x, int16_t c2y, int16_t c2r -){ + ) { mp_int_t dist_x = c1x - c2x; mp_int_t dist_y = c1y - c2y; @@ -58,7 +58,7 @@ bool common_hal_vectorio_circle_circle_intersects( bool common_hal_vectorio_rectangle_rectangle_intersects( int16_t r1x, int16_t r1y, int16_t r1w, int16_t r1h, - int16_t r2x, int16_t r2y, int16_t r2w, int16_t r2h){ + int16_t r2x, int16_t r2y, int16_t r2w, int16_t r2h) { mp_int_t r1_left = r1x; @@ -73,4 +73,4 @@ bool common_hal_vectorio_rectangle_rectangle_intersects( return r1_left < r2_right && r1_right > r2_left && r1_top < r2_bottom && r1_bottom > r2_top; -} \ No newline at end of file +} From a1ac54bde7a3819c0cace84d15faf65d3272a149 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Sat, 14 Sep 2024 09:54:00 -0500 Subject: [PATCH 13/17] starting polygon circle. --- shared-bindings/vectorio/__init__.c | 183 ++++++++++++++++++++++++++++ shared-bindings/vectorio/__init__.h | 15 +++ shared-module/vectorio/__init__.c | 93 ++++++++++++++ shared-module/vectorio/__init__.h | 3 + 4 files changed, 294 insertions(+) diff --git a/shared-bindings/vectorio/__init__.c b/shared-bindings/vectorio/__init__.c index 8bf07c5c6a408..08a6711fcbece 100644 --- a/shared-bindings/vectorio/__init__.c +++ b/shared-bindings/vectorio/__init__.c @@ -185,10 +185,193 @@ static mp_obj_t vectorio_circle_circle_intersects(size_t n_args, const mp_obj_t } MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_circle_circle_intersects_obj, 0, vectorio_circle_circle_intersects); +//| def circle_contains_point( +//| cx: int, cy: int, cr: int, px: int, py: int +//| ) -> bool: +//| """Checks whether a circle contains the given point +//| +//| :param int cx: Circle center x coordinate +//| :param int cy: Circle center y coordinate +//| :param int cr: Circle radius +//| :param int px: Point x coordinate +//| :param int py: Point y coordinate +//| ... +//| +static mp_obj_t vectorio_circle_contains_point(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum {ARG_cx, ARG_cy, ARG_cr, ARG_px, ARG_py}; + + static const mp_arg_t allowed_args[] = { + {MP_QSTR_cx, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_cy, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_cr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_px, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_py, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + }; + + 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); + + int16_t cx = args[ARG_cx].u_int; + int16_t cy = args[ARG_cy].u_int; + int16_t cr = args[ARG_cr].u_int; + int16_t px = args[ARG_px].u_int; + int16_t py = args[ARG_py].u_int; + + bool result = common_hal_vectorio_circle_contains_point(cx, cy, cr, px, py); + if (result) { + return mp_const_true; + } else { + return mp_const_false; + } +} +MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_circle_contains_point_obj, 0, vectorio_circle_contains_point); + +//| def rectangle_contains_point( +//| rx: int, ry: int, rw: int, rh: int, px: int, py: int +//| ) -> bool: +//| """Checks whether a rectangle contains the given point +//| +//| :param int rx: Rectangle x coordinate +//| :param int ry: Rectangle y coordinate +//| :param int rw: Rectangle width +//| :param int rh: Rectangle height +//| :param int px: Point x coordinate +//| :param int py: Point y coordinate +//| ... +//| +static mp_obj_t vectorio_rectangle_contains_point(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum {ARG_rx, ARG_ry, ARG_rw, ARG_rh, ARG_px, ARG_py}; + + static const mp_arg_t allowed_args[] = { + {MP_QSTR_rx, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_ry, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_rw, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_rh, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_px, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_py, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + }; + + 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); + + int16_t rx = args[ARG_rx].u_int; + int16_t ry = args[ARG_ry].u_int; + int16_t rw = args[ARG_rw].u_int; + int16_t rh = args[ARG_rh].u_int; + int16_t px = args[ARG_px].u_int; + int16_t py = args[ARG_py].u_int; + + bool result = common_hal_vectorio_rectangle_contains_point(rx, ry, rw, rh, px, py); + if (result) { + return mp_const_true; + } else { + return mp_const_false; + } +} +MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_rectangle_contains_point_obj, 0, vectorio_rectangle_contains_point); + + +//| def line_contains_point( +//| x1: int, y1: int, x2: int, y2: int, px: int, py: int +//| ) -> bool: +//| """Checks whether a line contains the given point +//| +//| :param int x1: Line x1 coordinate +//| :param int y1: Line y1 coordinate +//| :param int x2: Line x2 coordinate +//| :param int y2: Line y2 coordinate +//| :param int px: Point x coordinate +//| :param int py: Point y coordinate +//| :param float padding: Extra padding outside of the line to consider as positive intersection +//| ... +//| +static mp_obj_t vectorio_line_contains_point(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum {ARG_x1, ARG_y1, ARG_x2, ARG_y2, ARG_px, ARG_py, ARG_padding}; + + static const mp_arg_t allowed_args[] = { + {MP_QSTR_x1, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_y1, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_x2, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_y2, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_px, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_py, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_padding, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, // None convert to 0.0 + }; + + 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); + + int16_t x1 = args[ARG_x1].u_int; + int16_t y1 = args[ARG_y1].u_int; + int16_t x2 = args[ARG_x2].u_int; + int16_t y2 = args[ARG_y2].u_int; + int16_t px = args[ARG_px].u_int; + int16_t py = args[ARG_py].u_int; + + // Confirm the angle value + mp_float_t padding = 0.0; + if (args[ARG_padding].u_obj != mp_const_none) { + padding = mp_obj_get_float(args[ARG_padding].u_obj); + } + + bool result = common_hal_vectorio_line_contains_point(x1, y1, x2, y2, px, py, padding); + if (result) { + return mp_const_true; + } else { + return mp_const_false; + } +} +MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_line_contains_point_obj, 0, vectorio_line_contains_point); + + +//| def polygon_circle_intersects( +//| points: List[Tuple[int, int]], cx: int, cy: int, cr: int +//| ) -> bool: +//| """Checks for intersection between a polygon and a cricle. +//| +//| :param List[Tuple[int,int]] points: Vertices for the polygon +//| :param int cx: Circle center x coordinate +//| :param int cy: Circle center y coordinate +//| :param int cr: Circle radius""" +//| ... +//| +static mp_obj_t vectorio_polygon_circle_intersects(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum {ARG_points_list, ARG_cx, ARG_cy, ARG_cr}; + + static const mp_arg_t allowed_args[] = { + {MP_QSTR_points, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_cx, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_cy, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_cr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}} + }; + + 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); + + mp_obj_t points_list = mp_arg_validate_type(args[ARG_points_list].u_obj, &mp_type_list, MP_QSTR_points); + + int16_t cx = args[ARG_cx].u_int; + int16_t cy = args[ARG_cy].u_int; + int16_t cr = args[ARG_cr].u_int; + + bool result = common_hal_vectorio_polygon_circle_intersects(points_list, cx, cy, cr); + if (result) { + return mp_const_true; + } else { + return mp_const_false; + } +} +MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_polygon_circle_intersects_obj, 0, vectorio_polygon_circle_intersects); + + static const mp_rom_map_elem_t vectorio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_vectorio) }, { MP_ROM_QSTR(MP_QSTR_circle_rectangle_intersects), MP_ROM_PTR(&vectorio_circle_rectangle_intersects_obj) }, + { MP_ROM_QSTR(MP_QSTR_polygon_circle_intersects), MP_ROM_PTR(&vectorio_polygon_circle_intersects_obj) }, { MP_ROM_QSTR(MP_QSTR_circle_circle_intersects), MP_ROM_PTR(&vectorio_circle_circle_intersects_obj) }, + { MP_ROM_QSTR(MP_QSTR_circle_contains_point), MP_ROM_PTR(&vectorio_circle_contains_point_obj) }, + { MP_ROM_QSTR(MP_QSTR_rectangle_contains_point), MP_ROM_PTR(&vectorio_rectangle_contains_point_obj) }, + { MP_ROM_QSTR(MP_QSTR_line_contains_point), MP_ROM_PTR(&vectorio_line_contains_point_obj) }, { MP_ROM_QSTR(MP_QSTR_rectangle_rectangle_intersects), MP_ROM_PTR(&vectorio_rectangle_rectangle_intersects_obj) }, { MP_ROM_QSTR(MP_QSTR_Circle), MP_ROM_PTR(&vectorio_circle_type) }, { MP_ROM_QSTR(MP_QSTR_Polygon), MP_ROM_PTR(&vectorio_polygon_type) }, diff --git a/shared-bindings/vectorio/__init__.h b/shared-bindings/vectorio/__init__.h index 48b97ff795f64..75a22baf38e82 100644 --- a/shared-bindings/vectorio/__init__.h +++ b/shared-bindings/vectorio/__init__.h @@ -56,3 +56,18 @@ bool common_hal_vectorio_rectangle_rectangle_intersects( bool common_hal_vectorio_circle_circle_intersects( int16_t c1x, int16_t c1y, int16_t c1r, int16_t c2x, int16_t c2y, int16_t c2r); + +bool common_hal_vectorio_circle_contains_point( + int16_t cx, int16_t cy, int16_t cr, + int16_t px, int16_t py); + +bool common_hal_vectorio_rectangle_contains_point( + int16_t rx, int16_t ry, int16_t rw, int16_t rh, + int16_t px, int16_t py); + +bool common_hal_vectorio_line_contains_point( + int16_t x1, int16_t y1, int16_t x2, int16_t y2, + int16_t px, int16_t py, mp_float_t padding); + +bool common_hal_vectorio_polygon_circle_intersects( + mp_obj_t points_list, int16_t cx, int16_t cy, int16_t cr); diff --git a/shared-module/vectorio/__init__.c b/shared-module/vectorio/__init__.c index 51b6dda8869a3..6feea2c1a6e56 100644 --- a/shared-module/vectorio/__init__.c +++ b/shared-module/vectorio/__init__.c @@ -7,6 +7,8 @@ // Don't need anything in here yet #include "shared-bindings/vectorio/__init__.h" +#include "shared-module/vectorio/__init__.h" + #include "py/runtime.h" #include "stdlib.h" #include @@ -56,6 +58,63 @@ bool common_hal_vectorio_circle_circle_intersects( } +bool common_hal_vectorio_circle_contains_point( + int16_t cx, int16_t cy, int16_t cr, + int16_t px, int16_t py + ) { + + mp_int_t dist_x = px - cx; + mp_int_t dist_y = py - cy; + + mp_int_t dist_sq = (dist_x * dist_x) + (dist_y * dist_y); + + return dist_sq <= cr * cr; +} + +bool common_hal_vectorio_rectangle_contains_point( + int16_t rx, int16_t ry, int16_t rw, int16_t rh, + int16_t px, int16_t py + ) { + + if (rx > px){ + return false; + } + if (px > rx + rw){ + return false; + } + if (ry > py){ + return false; + } + if (py > ry + rh){ + return false; + } + return true; +} + +float measure_distance(int x1, int y1, int x2, int y2){ + int dist_x = x1 - x2; + int dist_y = y1 - y2; + return sqrtf((dist_x * dist_x) + (dist_y * dist_y)); +} + +bool common_hal_vectorio_line_contains_point( + int16_t x1, int16_t y1, int16_t x2, int16_t y2, + int16_t px, int16_t py, mp_float_t padding + ) { + + float line_length = measure_distance(x1, y1, x2, y2); + float d1 = measure_distance(x1, y1, px, py); + float d2 = measure_distance(x2, y2, px, py); + + if (d1+d2 >= line_length-padding && d1+d2 <= line_length+padding) { + return true; + } + return false; + +} + + + bool common_hal_vectorio_rectangle_rectangle_intersects( int16_t r1x, int16_t r1y, int16_t r1w, int16_t r1h, int16_t r2x, int16_t r2y, int16_t r2w, int16_t r2h) { @@ -74,3 +133,37 @@ bool common_hal_vectorio_rectangle_rectangle_intersects( return r1_left < r2_right && r1_right > r2_left && r1_top < r2_bottom && r1_bottom > r2_top; } + +bool common_hal_vectorio_polygon_circle_intersects( + mp_obj_t points_list, int16_t cx, int16_t cy, int16_t cr +) { + size_t len = 0; + mp_obj_t *points_list_items; + mp_obj_list_get(points_list, &len, &points_list_items); + + for(uint16_t i = 0; i < len; i++){ + size_t cur_tuple_len = 0; + mp_obj_t *cur_point_tuple; + mp_arg_validate_type(points_list_items[i], &mp_type_tuple, MP_QSTR_point); + mp_obj_tuple_get(points_list_items[i], &cur_tuple_len, &cur_point_tuple); + + mp_int_t cur_x = mp_arg_validate_type_int(cur_point_tuple[0], MP_QSTR_x); + mp_int_t cur_y = mp_arg_validate_type_int(cur_point_tuple[1], MP_QSTR_y); + + size_t next_tuple_len = 0; + mp_obj_t *next_point_tuple; + int next_index = (i + 1) % len; + mp_arg_validate_type(points_list_items[next_index], &mp_type_tuple, MP_QSTR_point); + mp_obj_tuple_get(points_list_items[next_index], &next_tuple_len, &next_point_tuple); + + mp_int_t next_x = mp_arg_validate_type_int(next_point_tuple[0], MP_QSTR_x); + mp_int_t next_y = mp_arg_validate_type_int(next_point_tuple[1], MP_QSTR_y); + + mp_printf(&mp_plat_print, "%d,%d - %d,%d \n", cur_x, cur_y, next_x, next_y); + + + + } + + return false; +} \ No newline at end of file diff --git a/shared-module/vectorio/__init__.h b/shared-module/vectorio/__init__.h index 3db1071653c07..c49737274e4df 100644 --- a/shared-module/vectorio/__init__.h +++ b/shared-module/vectorio/__init__.h @@ -14,3 +14,6 @@ typedef struct { mp_obj_t obj; event_function *event; } vectorio_event_t; + +float measure_distance( + int x1, int y1, int x2, int y2); From b0fccc8b90e8b2a90f8401b247af3735dca6ddab Mon Sep 17 00:00:00 2001 From: foamyguy Date: Sat, 21 Sep 2024 12:01:21 -0500 Subject: [PATCH 14/17] line circle and polygon circle. --- shared-bindings/vectorio/__init__.c | 93 ++++++++++++++++++++++++++--- shared-bindings/vectorio/__init__.h | 7 ++- shared-module/vectorio/__init__.c | 51 +++++++++++++--- shared-module/vectorio/__init__.h | 2 +- 4 files changed, 135 insertions(+), 18 deletions(-) diff --git a/shared-bindings/vectorio/__init__.c b/shared-bindings/vectorio/__init__.c index 08a6711fcbece..7f61de75dddf6 100644 --- a/shared-bindings/vectorio/__init__.c +++ b/shared-bindings/vectorio/__init__.c @@ -282,7 +282,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_rectangle_contains_point_obj, 0, vectorio_re //| :param int y2: Line y2 coordinate //| :param int px: Point x coordinate //| :param int py: Point y coordinate -//| :param float padding: Extra padding outside of the line to consider as positive intersection +//| :param float padding: Extra padding outside of the line to consider as positive intersection""" //| ... //| static mp_obj_t vectorio_line_contains_point(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { @@ -308,7 +308,7 @@ static mp_obj_t vectorio_line_contains_point(size_t n_args, const mp_obj_t *pos_ int16_t px = args[ARG_px].u_int; int16_t py = args[ARG_py].u_int; - // Confirm the angle value + // Use default padding if None was passed mp_float_t padding = 0.0; if (args[ARG_padding].u_obj != mp_const_none) { padding = mp_obj_get_float(args[ARG_padding].u_obj); @@ -324,25 +324,90 @@ static mp_obj_t vectorio_line_contains_point(size_t n_args, const mp_obj_t *pos_ MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_line_contains_point_obj, 0, vectorio_line_contains_point); +//| def line_circle_intersects( +//| x1: int, y1: int, x2: int, y2: int, +// cx: int, cy: int, cr: int +//| ) -> bool: +//| """Checks whether a line intersects with a circle +//| +//| :param int x1: Line x1 coordinate +//| :param int y1: Line y1 coordinate +//| :param int x2: Line x2 coordinate +//| :param int y2: Line y2 coordinate +//| :param int cx: Circle center x coordinate +//| :param int cy: Circle center y coordinate +//| :param int cr: Circle radius +//| :param float padding: Extra padding outside of the line to consider as positive intersection +//| ... +//| +static mp_obj_t vectorio_line_circle_intersects(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum {ARG_x1, ARG_y1, ARG_x2, ARG_y2, ARG_cx, ARG_cy, ARG_cr, ARG_padding}; + + static const mp_arg_t allowed_args[] = { + {MP_QSTR_x1, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_y1, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_x2, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_y2, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_cx, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_cy, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_cr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_padding, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, // None convert to 0.0 + }; + + 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); + + int16_t x1 = args[ARG_x1].u_int; + int16_t y1 = args[ARG_y1].u_int; + int16_t x2 = args[ARG_x2].u_int; + int16_t y2 = args[ARG_y2].u_int; + int16_t cx = args[ARG_cx].u_int; + int16_t cy = args[ARG_cy].u_int; + int16_t cr = args[ARG_cr].u_int; + + // Use default padding if None was passed + mp_float_t padding = 0.0; + if (args[ARG_padding].u_obj != mp_const_none) { + padding = mp_obj_get_float(args[ARG_padding].u_obj); + } + + bool result = common_hal_vectorio_line_circle_intersects(x1, y1, x2, y2, cx, cy, cr, padding); + + if (result) { + return mp_const_true; + } else { + return mp_const_false; + } +} +MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_line_circle_intersects_obj, 0, vectorio_line_circle_intersects); + + //| def polygon_circle_intersects( -//| points: List[Tuple[int, int]], cx: int, cy: int, cr: int +//| points: List[Tuple[int, int]], polygon_x: int, polygon_y: int, +//| cx: int, cy: int, cr: int, padding: float //| ) -> bool: //| """Checks for intersection between a polygon and a cricle. //| //| :param List[Tuple[int,int]] points: Vertices for the polygon +//| :param int polygon_x: Polygon x coordinate. All other polygon points are relative to this +//| :param int polygon_y: Polygon y coordinate. All other polygon points are relative to this //| :param int cx: Circle center x coordinate //| :param int cy: Circle center y coordinate -//| :param int cr: Circle radius""" +//| :param int cr: Circle radius +//| :param float padding: Extra padding outside of the line to consider as positive intersection""" //| ... //| static mp_obj_t vectorio_polygon_circle_intersects(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum {ARG_points_list, ARG_cx, ARG_cy, ARG_cr}; + enum {ARG_points_list, ARG_polygon_x, ARG_polygon_y, ARG_cx, ARG_cy, ARG_cr, ARG_padding}; static const mp_arg_t allowed_args[] = { {MP_QSTR_points, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_polygon_x, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_polygon_y, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, {MP_QSTR_cx, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, {MP_QSTR_cy, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, - {MP_QSTR_cr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}} + {MP_QSTR_cr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_padding, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, // None convert to 0.0 }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; @@ -350,11 +415,24 @@ static mp_obj_t vectorio_polygon_circle_intersects(size_t n_args, const mp_obj_t mp_obj_t points_list = mp_arg_validate_type(args[ARG_points_list].u_obj, &mp_type_list, MP_QSTR_points); + int16_t polygon_x = args[ARG_polygon_x].u_int; + int16_t polygon_y = args[ARG_polygon_y].u_int; + int16_t cx = args[ARG_cx].u_int; int16_t cy = args[ARG_cy].u_int; int16_t cr = args[ARG_cr].u_int; - bool result = common_hal_vectorio_polygon_circle_intersects(points_list, cx, cy, cr); + // Use default padding if None was passed + mp_float_t padding = 0.0; + if (args[ARG_padding].u_obj != mp_const_none) { + padding = mp_obj_get_float(args[ARG_padding].u_obj); + } + + bool result = common_hal_vectorio_polygon_circle_intersects( + points_list, polygon_x, polygon_y, + cx, cy, cr, padding + ); + if (result) { return mp_const_true; } else { @@ -369,6 +447,7 @@ static const mp_rom_map_elem_t vectorio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_circle_rectangle_intersects), MP_ROM_PTR(&vectorio_circle_rectangle_intersects_obj) }, { MP_ROM_QSTR(MP_QSTR_polygon_circle_intersects), MP_ROM_PTR(&vectorio_polygon_circle_intersects_obj) }, { MP_ROM_QSTR(MP_QSTR_circle_circle_intersects), MP_ROM_PTR(&vectorio_circle_circle_intersects_obj) }, + { MP_ROM_QSTR(MP_QSTR_line_circle_intersects), MP_ROM_PTR(&vectorio_line_circle_intersects_obj) }, { MP_ROM_QSTR(MP_QSTR_circle_contains_point), MP_ROM_PTR(&vectorio_circle_contains_point_obj) }, { MP_ROM_QSTR(MP_QSTR_rectangle_contains_point), MP_ROM_PTR(&vectorio_rectangle_contains_point_obj) }, { MP_ROM_QSTR(MP_QSTR_line_contains_point), MP_ROM_PTR(&vectorio_line_contains_point_obj) }, diff --git a/shared-bindings/vectorio/__init__.h b/shared-bindings/vectorio/__init__.h index 75a22baf38e82..67f3d484c448b 100644 --- a/shared-bindings/vectorio/__init__.h +++ b/shared-bindings/vectorio/__init__.h @@ -69,5 +69,10 @@ bool common_hal_vectorio_line_contains_point( int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t px, int16_t py, mp_float_t padding); +bool common_hal_vectorio_line_circle_intersects( + int16_t x1, int16_t y1, int16_t x2, int16_t y2, + int16_t cx, int16_t cy, int16_t cr, mp_float_t padding); + bool common_hal_vectorio_polygon_circle_intersects( - mp_obj_t points_list, int16_t cx, int16_t cy, int16_t cr); + mp_obj_t points_list, int16_t polygon_x, int16_t polygon_y, + int16_t cx, int16_t cy, int16_t cr, mp_float_t padding); diff --git a/shared-module/vectorio/__init__.c b/shared-module/vectorio/__init__.c index 6feea2c1a6e56..a1c3bb12201b9 100644 --- a/shared-module/vectorio/__init__.c +++ b/shared-module/vectorio/__init__.c @@ -91,9 +91,9 @@ bool common_hal_vectorio_rectangle_contains_point( return true; } -float measure_distance(int x1, int y1, int x2, int y2){ - int dist_x = x1 - x2; - int dist_y = y1 - y2; +float measure_distance(float x1, float y1, float x2, float y2){ + float dist_x = x1 - x2; + float dist_y = y1 - y2; return sqrtf((dist_x * dist_x) + (dist_y * dist_y)); } @@ -113,7 +113,34 @@ bool common_hal_vectorio_line_contains_point( } +bool common_hal_vectorio_line_circle_intersects( + int16_t x1, int16_t y1, int16_t x2, int16_t y2, + int16_t cx, int16_t cy, int16_t cr, mp_float_t padding +){ + if (common_hal_vectorio_circle_contains_point(cx, cy, cr, x1, y1)){ + return true; + } + if (common_hal_vectorio_circle_contains_point(cx, cy, cr, x2, y2)){ + return true; + } + float line_length = measure_distance(x1, y1, x2, y2); + float dot = ( ((cx-x1)*(x2-x1)) + ((cy-y1)*(y2-y1)) ) / pow(line_length,2); + + float closestX = x1 + (dot * (x2-x1)); + float closestY = y1 + (dot * (y2-y1)); + + if (!common_hal_vectorio_line_contains_point(x1,y1,x2,y2, closestX,closestY, padding)){ + return false; + } + float distance = measure_distance(closestX, closestY, cx, cy); + if (distance <= cr){ + return true; + }else{ + return false; + } + +} bool common_hal_vectorio_rectangle_rectangle_intersects( int16_t r1x, int16_t r1y, int16_t r1w, int16_t r1h, @@ -135,7 +162,7 @@ bool common_hal_vectorio_rectangle_rectangle_intersects( } bool common_hal_vectorio_polygon_circle_intersects( - mp_obj_t points_list, int16_t cx, int16_t cy, int16_t cr + mp_obj_t points_list, int16_t polygon_x, int16_t polygon_y, int16_t cx, int16_t cy, int16_t cr, mp_float_t padding ) { size_t len = 0; mp_obj_t *points_list_items; @@ -147,8 +174,8 @@ bool common_hal_vectorio_polygon_circle_intersects( mp_arg_validate_type(points_list_items[i], &mp_type_tuple, MP_QSTR_point); mp_obj_tuple_get(points_list_items[i], &cur_tuple_len, &cur_point_tuple); - mp_int_t cur_x = mp_arg_validate_type_int(cur_point_tuple[0], MP_QSTR_x); - mp_int_t cur_y = mp_arg_validate_type_int(cur_point_tuple[1], MP_QSTR_y); + mp_int_t cur_x = mp_arg_validate_type_int(cur_point_tuple[0], MP_QSTR_x) + polygon_x; + mp_int_t cur_y = mp_arg_validate_type_int(cur_point_tuple[1], MP_QSTR_y) + polygon_y; size_t next_tuple_len = 0; mp_obj_t *next_point_tuple; @@ -156,11 +183,17 @@ bool common_hal_vectorio_polygon_circle_intersects( mp_arg_validate_type(points_list_items[next_index], &mp_type_tuple, MP_QSTR_point); mp_obj_tuple_get(points_list_items[next_index], &next_tuple_len, &next_point_tuple); - mp_int_t next_x = mp_arg_validate_type_int(next_point_tuple[0], MP_QSTR_x); - mp_int_t next_y = mp_arg_validate_type_int(next_point_tuple[1], MP_QSTR_y); + mp_int_t next_x = mp_arg_validate_type_int(next_point_tuple[0], MP_QSTR_x) + polygon_x; + mp_int_t next_y = mp_arg_validate_type_int(next_point_tuple[1], MP_QSTR_y) + polygon_y; - mp_printf(&mp_plat_print, "%d,%d - %d,%d \n", cur_x, cur_y, next_x, next_y); + //mp_printf(&mp_plat_print, "%d,%d - %d,%d \n", cur_x, cur_y, next_x, next_y); + if(common_hal_vectorio_line_circle_intersects( + cur_x, cur_y, next_x, next_y, + cx, cy, cr, padding + )){ + return true; + } } diff --git a/shared-module/vectorio/__init__.h b/shared-module/vectorio/__init__.h index c49737274e4df..d61990e37c3ad 100644 --- a/shared-module/vectorio/__init__.h +++ b/shared-module/vectorio/__init__.h @@ -16,4 +16,4 @@ typedef struct { } vectorio_event_t; float measure_distance( - int x1, int y1, int x2, int y2); + float x1, float y1, float x2, float y2); From f2dcb67da1b953cc2e49eaac4f0a1c54d6047a9f Mon Sep 17 00:00:00 2001 From: foamyguy Date: Wed, 23 Oct 2024 08:24:02 -0500 Subject: [PATCH 15/17] starting polygon rectangle --- shared-bindings/vectorio/__init__.c | 109 ++++++++++++++++++++++++++++ shared-bindings/vectorio/__init__.h | 10 +++ shared-module/vectorio/__init__.c | 71 +++++++++++++++++- 3 files changed, 188 insertions(+), 2 deletions(-) diff --git a/shared-bindings/vectorio/__init__.c b/shared-bindings/vectorio/__init__.c index 7f61de75dddf6..38bb7f7dc210d 100644 --- a/shared-bindings/vectorio/__init__.c +++ b/shared-bindings/vectorio/__init__.c @@ -442,12 +442,121 @@ static mp_obj_t vectorio_polygon_circle_intersects(size_t n_args, const mp_obj_t MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_polygon_circle_intersects_obj, 0, vectorio_polygon_circle_intersects); +//| def line_line_intersects( +//| x1: int, y1: int, x2: int, y2: int, +//| x3: int, y3: int, x4: int, y4: int, +//| ) -> bool: +//| """Checks whether a line intersects with another line +//| +//| :param int x1: Line x1 coordinate +//| :param int y1: Line y1 coordinate +//| :param int x2: Line x2 coordinate +//| :param int y2: Line y2 coordinate +//| :param int x3: Other Line x3 coordinate +//| :param int y3: Other Line y3 coordinate +//| :param int x4: Other Line x4 coordinate +//| :param int y4: Other Line y4 coordinate +//| ... +//| +static mp_obj_t vectorio_line_line_intersects(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum {ARG_x1, ARG_y1, ARG_x2, ARG_y2, ARG_x3, ARG_y3, ARG_x4, ARG_y4 }; + + static const mp_arg_t allowed_args[] = { + {MP_QSTR_x1, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_y1, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_x2, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_y2, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_x3, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_y3, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_x4, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_y4, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + }; + + 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); + + int16_t x1 = args[ARG_x1].u_int; + int16_t y1 = args[ARG_y1].u_int; + int16_t x2 = args[ARG_x2].u_int; + int16_t y2 = args[ARG_y2].u_int; + int16_t x3 = args[ARG_x3].u_int; + int16_t y3 = args[ARG_y3].u_int; + int16_t x4 = args[ARG_x4].u_int; + int16_t y4 = args[ARG_y4].u_int; + + bool result = common_hal_vectorio_line_line_intersects(x1, y1, x2, y2, x3, y3, x4, y4); + + if (result) { + return mp_const_true; + } else { + return mp_const_false; + } +} +MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_line_line_intersects_obj, 0, vectorio_line_line_intersects); + + +//| def line_rectangle_intersects( +//| x1: int, y1: int, x2: int, y2: int, +//| rx: int, ry: int, rw: int, rh: int +//| ) -> bool: +//| """Checks whether a line intersects with another line +//| +//| :param int x1: Line x1 coordinate +//| :param int y1: Line y1 coordinate +//| :param int x2: Line x2 coordinate +//| :param int y2: Line y2 coordinate +//| :param int rx: Rectangle x coordinate +//| :param int ry: Rectangle y coordinate +//| :param int rw: Rectangle width +//| :param int rh: Rectangle height +//| ... +//| +static mp_obj_t vectorio_line_rectangle_intersects(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum {ARG_x1, ARG_y1, ARG_x2, ARG_y2, ARG_rx, ARG_ry, ARG_rw, ARG_rh }; + + static const mp_arg_t allowed_args[] = { + {MP_QSTR_x1, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_y1, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_x2, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_y2, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_rx, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_ry, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_rw, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_rh, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + + }; + + 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); + + int16_t x1 = args[ARG_x1].u_int; + int16_t y1 = args[ARG_y1].u_int; + int16_t x2 = args[ARG_x2].u_int; + int16_t y2 = args[ARG_y2].u_int; + int16_t rx = args[ARG_rx].u_int; + int16_t ry = args[ARG_ry].u_int; + int16_t rw = args[ARG_rw].u_int; + int16_t rh = args[ARG_rh].u_int; + + bool result = common_hal_vectorio_line_rectangle_intersects(x1, y1, x2, y2, rx, ry, rw, rh); + + if (result) { + return mp_const_true; + } else { + return mp_const_false; + } +} +MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_line_rectangle_intersects_obj, 0, vectorio_line_rectangle_intersects); + + static const mp_rom_map_elem_t vectorio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_vectorio) }, { MP_ROM_QSTR(MP_QSTR_circle_rectangle_intersects), MP_ROM_PTR(&vectorio_circle_rectangle_intersects_obj) }, { MP_ROM_QSTR(MP_QSTR_polygon_circle_intersects), MP_ROM_PTR(&vectorio_polygon_circle_intersects_obj) }, { MP_ROM_QSTR(MP_QSTR_circle_circle_intersects), MP_ROM_PTR(&vectorio_circle_circle_intersects_obj) }, { MP_ROM_QSTR(MP_QSTR_line_circle_intersects), MP_ROM_PTR(&vectorio_line_circle_intersects_obj) }, + { MP_ROM_QSTR(MP_QSTR_line_line_intersects), MP_ROM_PTR(&vectorio_line_line_intersects_obj) }, + { MP_ROM_QSTR(MP_QSTR_line_rectangle_intersects), MP_ROM_PTR(&vectorio_line_rectangle_intersects_obj) }, { MP_ROM_QSTR(MP_QSTR_circle_contains_point), MP_ROM_PTR(&vectorio_circle_contains_point_obj) }, { MP_ROM_QSTR(MP_QSTR_rectangle_contains_point), MP_ROM_PTR(&vectorio_rectangle_contains_point_obj) }, { MP_ROM_QSTR(MP_QSTR_line_contains_point), MP_ROM_PTR(&vectorio_line_contains_point_obj) }, diff --git a/shared-bindings/vectorio/__init__.h b/shared-bindings/vectorio/__init__.h index 67f3d484c448b..821a394d7beca 100644 --- a/shared-bindings/vectorio/__init__.h +++ b/shared-bindings/vectorio/__init__.h @@ -76,3 +76,13 @@ bool common_hal_vectorio_line_circle_intersects( bool common_hal_vectorio_polygon_circle_intersects( mp_obj_t points_list, int16_t polygon_x, int16_t polygon_y, int16_t cx, int16_t cy, int16_t cr, mp_float_t padding); + +bool common_hal_vectorio_line_line_intersects( + int16_t x1, int16_t y1, int16_t x2, int16_t y2, + int16_t x3, int16_t y3, int16_t x4, int16_t y4 +); + +bool common_hal_vectorio_line_rectangle_intersects( + int16_t x1, int16_t y1, int16_t x2, int16_t y2, + int16_t rx, int16_t ry, int16_t rw, int16_t rh +); \ No newline at end of file diff --git a/shared-module/vectorio/__init__.c b/shared-module/vectorio/__init__.c index a1c3bb12201b9..510ed30f20903 100644 --- a/shared-module/vectorio/__init__.c +++ b/shared-module/vectorio/__init__.c @@ -167,7 +167,7 @@ bool common_hal_vectorio_polygon_circle_intersects( size_t len = 0; mp_obj_t *points_list_items; mp_obj_list_get(points_list, &len, &points_list_items); - + bool polygon_contains_point = false; for(uint16_t i = 0; i < len; i++){ size_t cur_tuple_len = 0; mp_obj_t *cur_point_tuple; @@ -195,8 +195,75 @@ bool common_hal_vectorio_polygon_circle_intersects( return true; } + if (((cur_y >= cy && cy > next_y) || (cur_y < cy && cy <= next_y)) && + (cx < (next_x - cur_x) * (cy - cur_y) / (next_y - cur_y) + cur_x)){ - } + polygon_contains_point = !polygon_contains_point; + } + } + if (polygon_contains_point == true){ + return true; + } return false; +} + + + +bool common_hal_vectorio_line_line_intersects( + int16_t x1, int16_t y1, int16_t x2, int16_t y2, + int16_t x3, int16_t y3, int16_t x4, int16_t y4 +){ + + double denom = ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1)); + + if (denom >= 0 && denom <= 0){ + return false; + } + + double dx1 = x1; + double dy1 = y1; + double dx2 = x2; + double dy2 = y2; + double dx3 = x3; + double dy3 = y3; + double dx4 = x4; + double dy4 = y4; + + + double dist_a = ((dx4 - dx3) * (dy1 - dy3) - (dy4 - dy3) * (dx1 - dx3)) / denom; + double dist_b = ((dx2 - dx1) * (dy1 - dy3) - (dy2 - dy1) * (dx1 - dx3)) / denom; + + if ((0 <= dist_a) && (dist_a <= 1) && (0 <= dist_b) && (dist_b <= 1)){ + return true; + } + return false; + +} + +bool common_hal_vectorio_line_rectangle_intersects( + int16_t x1, int16_t y1, int16_t x2, int16_t y2, + int16_t rx, int16_t ry, int16_t rw, int16_t rh +){ + + + if (common_hal_vectorio_line_line_intersects(x1, y1, x2, y2, + rx, ry, rx, ry + rh)){ + return true; + } + if (common_hal_vectorio_line_line_intersects(x1, y1, x2, y2, + rx + rw, ry, rx + rw, ry + rh)){ + return true; + } + if (common_hal_vectorio_line_line_intersects(x1, y1, x2, y2, + rx, ry, rx + rw, ry)){ + return true; + } + if (common_hal_vectorio_line_line_intersects(x1, y1, x2, y2, + rx, ry + rh, rx + rw, ry + rh)){ + return true; + } + + return false; + } \ No newline at end of file From 509398879cdeae2888d8fc2873c1c42ad4804926 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Wed, 23 Oct 2024 13:28:30 -0500 Subject: [PATCH 16/17] polygon rectangle function. cleanup uneeded changes. --- lib/tinyusb | 2 +- ports/espressif/esp-idf | 2 +- shared-bindings/vectorio/Rectangle.c | 1 - shared-bindings/vectorio/__init__.c | 123 +++++++++++--- shared-bindings/vectorio/__init__.h | 20 ++- shared-module/vectorio/Circle.c | 5 +- shared-module/vectorio/Rectangle.c | 4 - shared-module/vectorio/__init__.c | 230 ++++++++++++++++----------- 8 files changed, 248 insertions(+), 139 deletions(-) diff --git a/lib/tinyusb b/lib/tinyusb index ac1fd3266644a..4349e99fb29db 160000 --- a/lib/tinyusb +++ b/lib/tinyusb @@ -1 +1 @@ -Subproject commit ac1fd3266644a2d87e38ef3e353ed9f60a618d50 +Subproject commit 4349e99fb29dbc7d019aa2fbb6344864799d8c1b diff --git a/ports/espressif/esp-idf b/ports/espressif/esp-idf index aa149243fa332..3ecf6bafcf045 160000 --- a/ports/espressif/esp-idf +++ b/ports/espressif/esp-idf @@ -1 +1 @@ -Subproject commit aa149243fa332f46799aa9c9b4cc6be92d7c4cda +Subproject commit 3ecf6bafcf045e0d2705ef49524456801df4f754 diff --git a/shared-bindings/vectorio/Rectangle.c b/shared-bindings/vectorio/Rectangle.c index aa44885f2128f..d1f2e6193ce3e 100644 --- a/shared-bindings/vectorio/Rectangle.c +++ b/shared-bindings/vectorio/Rectangle.c @@ -136,7 +136,6 @@ const mp_obj_property_t vectorio_rectangle_color_index_obj = { MP_ROM_NONE}, }; - // Documentation for properties inherited from VectorShape. //| x: int diff --git a/shared-bindings/vectorio/__init__.c b/shared-bindings/vectorio/__init__.c index 38bb7f7dc210d..ceb366683e3a1 100644 --- a/shared-bindings/vectorio/__init__.c +++ b/shared-bindings/vectorio/__init__.c @@ -185,16 +185,14 @@ static mp_obj_t vectorio_circle_circle_intersects(size_t n_args, const mp_obj_t } MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_circle_circle_intersects_obj, 0, vectorio_circle_circle_intersects); -//| def circle_contains_point( -//| cx: int, cy: int, cr: int, px: int, py: int -//| ) -> bool: +//| def circle_contains_point(cx: int, cy: int, cr: int, px: int, py: int) -> bool: //| """Checks whether a circle contains the given point //| //| :param int cx: Circle center x coordinate //| :param int cy: Circle center y coordinate //| :param int cr: Circle radius //| :param int px: Point x coordinate -//| :param int py: Point y coordinate +//| :param int py: Point y coordinate""" //| ... //| static mp_obj_t vectorio_circle_contains_point(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { @@ -226,9 +224,7 @@ static mp_obj_t vectorio_circle_contains_point(size_t n_args, const mp_obj_t *po } MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_circle_contains_point_obj, 0, vectorio_circle_contains_point); -//| def rectangle_contains_point( -//| rx: int, ry: int, rw: int, rh: int, px: int, py: int -//| ) -> bool: +//| def rectangle_contains_point(rx: int, ry: int, rw: int, rh: int, px: int, py: int) -> bool: //| """Checks whether a rectangle contains the given point //| //| :param int rx: Rectangle x coordinate @@ -236,7 +232,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_circle_contains_point_obj, 0, vectorio_circl //| :param int rw: Rectangle width //| :param int rh: Rectangle height //| :param int px: Point x coordinate -//| :param int py: Point y coordinate +//| :param int py: Point y coordinate""" //| ... //| static mp_obj_t vectorio_rectangle_contains_point(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { @@ -271,9 +267,7 @@ static mp_obj_t vectorio_rectangle_contains_point(size_t n_args, const mp_obj_t MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_rectangle_contains_point_obj, 0, vectorio_rectangle_contains_point); -//| def line_contains_point( -//| x1: int, y1: int, x2: int, y2: int, px: int, py: int -//| ) -> bool: +//| def line_contains_point(x1: int, y1: int, x2: int, y2: int, px: int, py: int) -> bool: //| """Checks whether a line contains the given point //| //| :param int x1: Line x1 coordinate @@ -282,7 +276,8 @@ MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_rectangle_contains_point_obj, 0, vectorio_re //| :param int y2: Line y2 coordinate //| :param int px: Point x coordinate //| :param int py: Point y coordinate -//| :param float padding: Extra padding outside of the line to consider as positive intersection""" +//| :param float padding: Extra padding outside of the line to consider as positive intersection +//| """ //| ... //| static mp_obj_t vectorio_line_contains_point(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { @@ -325,7 +320,10 @@ MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_line_contains_point_obj, 0, vectorio_line_co //| def line_circle_intersects( -//| x1: int, y1: int, x2: int, y2: int, +//| x1: int, +//| y1: int, +//| x2: int, +//| y2: int, // cx: int, cy: int, cr: int //| ) -> bool: //| """Checks whether a line intersects with a circle @@ -338,6 +336,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_line_contains_point_obj, 0, vectorio_line_co //| :param int cy: Circle center y coordinate //| :param int cr: Circle radius //| :param float padding: Extra padding outside of the line to consider as positive intersection +//| """ //| ... //| static mp_obj_t vectorio_line_circle_intersects(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { @@ -383,8 +382,13 @@ MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_line_circle_intersects_obj, 0, vectorio_line //| def polygon_circle_intersects( -//| points: List[Tuple[int, int]], polygon_x: int, polygon_y: int, -//| cx: int, cy: int, cr: int, padding: float +//| points: List[Tuple[int, int]], +//| polygon_x: int, +//| polygon_y: int, +//| cx: int, +//| cy: int, +//| cr: int, +//| padding: float, //| ) -> bool: //| """Checks for intersection between a polygon and a cricle. //| @@ -394,14 +398,15 @@ MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_line_circle_intersects_obj, 0, vectorio_line //| :param int cx: Circle center x coordinate //| :param int cy: Circle center y coordinate //| :param int cr: Circle radius -//| :param float padding: Extra padding outside of the line to consider as positive intersection""" +//| :param float padding: Extra padding outside of the line to consider as positive intersection +//| """ //| ... //| static mp_obj_t vectorio_polygon_circle_intersects(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum {ARG_points_list, ARG_polygon_x, ARG_polygon_y, ARG_cx, ARG_cy, ARG_cr, ARG_padding}; static const mp_arg_t allowed_args[] = { - {MP_QSTR_points, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_points, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}}, {MP_QSTR_polygon_x, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, {MP_QSTR_polygon_y, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, {MP_QSTR_cx, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, @@ -413,7 +418,7 @@ static mp_obj_t vectorio_polygon_circle_intersects(size_t n_args, const mp_obj_t 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); - mp_obj_t points_list = mp_arg_validate_type(args[ARG_points_list].u_obj, &mp_type_list, MP_QSTR_points); + mp_obj_t points_list = mp_arg_validate_type(args[ARG_points_list].u_obj, &mp_type_list, MP_QSTR_points); int16_t polygon_x = args[ARG_polygon_x].u_int; int16_t polygon_y = args[ARG_polygon_y].u_int; @@ -431,7 +436,7 @@ static mp_obj_t vectorio_polygon_circle_intersects(size_t n_args, const mp_obj_t bool result = common_hal_vectorio_polygon_circle_intersects( points_list, polygon_x, polygon_y, cx, cy, cr, padding - ); + ); if (result) { return mp_const_true; @@ -443,8 +448,14 @@ MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_polygon_circle_intersects_obj, 0, vectorio_p //| def line_line_intersects( -//| x1: int, y1: int, x2: int, y2: int, -//| x3: int, y3: int, x4: int, y4: int, +//| x1: int, +//| y1: int, +//| x2: int, +//| y2: int, +//| x3: int, +//| y3: int, +//| x4: int, +//| y4: int, //| ) -> bool: //| """Checks whether a line intersects with another line //| @@ -455,7 +466,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_polygon_circle_intersects_obj, 0, vectorio_p //| :param int x3: Other Line x3 coordinate //| :param int y3: Other Line y3 coordinate //| :param int x4: Other Line x4 coordinate -//| :param int y4: Other Line y4 coordinate +//| :param int y4: Other Line y4 coordinate""" //| ... //| static mp_obj_t vectorio_line_line_intersects(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { @@ -496,8 +507,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_line_line_intersects_obj, 0, vectorio_line_l //| def line_rectangle_intersects( -//| x1: int, y1: int, x2: int, y2: int, -//| rx: int, ry: int, rw: int, rh: int +//| x1: int, y1: int, x2: int, y2: int, rx: int, ry: int, rw: int, rh: int //| ) -> bool: //| """Checks whether a line intersects with another line //| @@ -508,7 +518,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_line_line_intersects_obj, 0, vectorio_line_l //| :param int rx: Rectangle x coordinate //| :param int ry: Rectangle y coordinate //| :param int rw: Rectangle width -//| :param int rh: Rectangle height +//| :param int rh: Rectangle height""" //| ... //| static mp_obj_t vectorio_line_rectangle_intersects(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { @@ -523,7 +533,6 @@ static mp_obj_t vectorio_line_rectangle_intersects(size_t n_args, const mp_obj_t {MP_QSTR_ry, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, {MP_QSTR_rw, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, {MP_QSTR_rh, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, - }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; @@ -549,6 +558,67 @@ static mp_obj_t vectorio_line_rectangle_intersects(size_t n_args, const mp_obj_t MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_line_rectangle_intersects_obj, 0, vectorio_line_rectangle_intersects); +//| def polygon_rectangle_intersects( +//| points: List[Tuple[int, int]], +//| polygon_x: int, +//| polygon_y: int, +//| rx: int, +//| ry: int, +//| rw: int, +//| rh: int, +//| ) -> bool: +//| """Checks for intersection between a polygon and a cricle. +//| +//| :param List[Tuple[int,int]] points: Vertices for the polygon +//| :param int polygon_x: Polygon x coordinate. All other polygon points are relative to this +//| :param int polygon_y: Polygon y coordinate. All other polygon points are relative to this +//| :param int rx: Rectangle x coordinate +//| :param int ry: Rectangle y coordinate +//| :param int rw: Rectangle width +//| :param int rh: Rectangle height""" +//| ... +//| +static mp_obj_t vectorio_polygon_rectangle_intersects(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum {ARG_points_list, ARG_polygon_x, ARG_polygon_y, ARG_rx, ARG_ry, ARG_rw, ARG_rh}; + + static const mp_arg_t allowed_args[] = { + {MP_QSTR_points, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_polygon_x, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_polygon_y, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_rx, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_ry, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_rw, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_rh, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_padding, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, // None convert to 0.0 + }; + + 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); + + mp_obj_t points_list = mp_arg_validate_type(args[ARG_points_list].u_obj, &mp_type_list, MP_QSTR_points); + + int16_t polygon_x = args[ARG_polygon_x].u_int; + int16_t polygon_y = args[ARG_polygon_y].u_int; + + int16_t rx = args[ARG_rx].u_int; + int16_t ry = args[ARG_ry].u_int; + int16_t rw = args[ARG_rw].u_int; + int16_t rh = args[ARG_rh].u_int; + + bool result = common_hal_vectorio_polygon_rectangle_intersects( + points_list, polygon_x, polygon_y, + rx, ry, rw, rh + ); + + if (result) { + return mp_const_true; + } else { + return mp_const_false; + } +} +MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_polygon_rectangle_intersects_obj, 0, vectorio_polygon_rectangle_intersects); + + static const mp_rom_map_elem_t vectorio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_vectorio) }, { MP_ROM_QSTR(MP_QSTR_circle_rectangle_intersects), MP_ROM_PTR(&vectorio_circle_rectangle_intersects_obj) }, @@ -557,6 +627,7 @@ static const mp_rom_map_elem_t vectorio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_line_circle_intersects), MP_ROM_PTR(&vectorio_line_circle_intersects_obj) }, { MP_ROM_QSTR(MP_QSTR_line_line_intersects), MP_ROM_PTR(&vectorio_line_line_intersects_obj) }, { MP_ROM_QSTR(MP_QSTR_line_rectangle_intersects), MP_ROM_PTR(&vectorio_line_rectangle_intersects_obj) }, + { MP_ROM_QSTR(MP_QSTR_polygon_rectangle_intersects), MP_ROM_PTR(&vectorio_polygon_rectangle_intersects_obj) }, { MP_ROM_QSTR(MP_QSTR_circle_contains_point), MP_ROM_PTR(&vectorio_circle_contains_point_obj) }, { MP_ROM_QSTR(MP_QSTR_rectangle_contains_point), MP_ROM_PTR(&vectorio_rectangle_contains_point_obj) }, { MP_ROM_QSTR(MP_QSTR_line_contains_point), MP_ROM_PTR(&vectorio_line_contains_point_obj) }, diff --git a/shared-bindings/vectorio/__init__.h b/shared-bindings/vectorio/__init__.h index 821a394d7beca..beaba0cd3e5cd 100644 --- a/shared-bindings/vectorio/__init__.h +++ b/shared-bindings/vectorio/__init__.h @@ -74,15 +74,19 @@ bool common_hal_vectorio_line_circle_intersects( int16_t cx, int16_t cy, int16_t cr, mp_float_t padding); bool common_hal_vectorio_polygon_circle_intersects( - mp_obj_t points_list, int16_t polygon_x, int16_t polygon_y, - int16_t cx, int16_t cy, int16_t cr, mp_float_t padding); + mp_obj_t points_list, int16_t polygon_x, int16_t polygon_y, + int16_t cx, int16_t cy, int16_t cr, mp_float_t padding); bool common_hal_vectorio_line_line_intersects( - int16_t x1, int16_t y1, int16_t x2, int16_t y2, - int16_t x3, int16_t y3, int16_t x4, int16_t y4 -); + int16_t x1, int16_t y1, int16_t x2, int16_t y2, + int16_t x3, int16_t y3, int16_t x4, int16_t y4 + ); bool common_hal_vectorio_line_rectangle_intersects( - int16_t x1, int16_t y1, int16_t x2, int16_t y2, - int16_t rx, int16_t ry, int16_t rw, int16_t rh -); \ No newline at end of file + int16_t x1, int16_t y1, int16_t x2, int16_t y2, + int16_t rx, int16_t ry, int16_t rw, int16_t rh + ); + +bool common_hal_vectorio_polygon_rectangle_intersects( + mp_obj_t points_list, int16_t polygon_x, int16_t polygon_y, + int16_t rx, int16_t ry, int16_t rw, int16_t rh); diff --git a/shared-module/vectorio/Circle.c b/shared-module/vectorio/Circle.c index fe50d84c2038f..6f7ee2f3406ea 100644 --- a/shared-module/vectorio/Circle.c +++ b/shared-module/vectorio/Circle.c @@ -7,11 +7,9 @@ #include "shared-bindings/vectorio/Circle.h" #include "shared-module/vectorio/__init__.h" #include "shared-module/displayio/area.h" -#include "shared-bindings/vectorio/Rectangle.h" -#include "shared-bindings/vectorio/VectorShape.h" + #include "py/runtime.h" #include "stdlib.h" -#include void common_hal_vectorio_circle_construct(vectorio_circle_t *self, uint16_t radius, uint16_t color_index) { @@ -81,7 +79,6 @@ void common_hal_vectorio_circle_set_color_index(void *obj, uint16_t color_index) } } - mp_obj_t common_hal_vectorio_circle_get_draw_protocol(void *circle) { vectorio_circle_t *self = circle; return self->draw_protocol_instance; diff --git a/shared-module/vectorio/Rectangle.c b/shared-module/vectorio/Rectangle.c index a4970b05f7e83..42cdf7cca96e4 100644 --- a/shared-module/vectorio/Rectangle.c +++ b/shared-module/vectorio/Rectangle.c @@ -6,13 +6,10 @@ #include "shared-module/vectorio/__init__.h" #include "shared-bindings/vectorio/Rectangle.h" -#include "shared-bindings/vectorio/VectorShape.h" -#include "shared-bindings/vectorio/Circle.h" #include "shared-module/displayio/area.h" #include "py/runtime.h" #include "stdlib.h" -#include void common_hal_vectorio_rectangle_construct(vectorio_rectangle_t *self, uint32_t width, uint32_t height, uint16_t color_index) { @@ -51,7 +48,6 @@ mp_obj_t common_hal_vectorio_rectangle_get_draw_protocol(void *rectangle) { return self->draw_protocol_instance; } - int16_t common_hal_vectorio_rectangle_get_width(void *obj) { vectorio_rectangle_t *self = obj; return self->width; diff --git a/shared-module/vectorio/__init__.c b/shared-module/vectorio/__init__.c index 510ed30f20903..cb4dac5dec0e2 100644 --- a/shared-module/vectorio/__init__.c +++ b/shared-module/vectorio/__init__.c @@ -66,7 +66,7 @@ bool common_hal_vectorio_circle_contains_point( mp_int_t dist_x = px - cx; mp_int_t dist_y = py - cy; - mp_int_t dist_sq = (dist_x * dist_x) + (dist_y * dist_y); + mp_int_t dist_sq = (dist_x * dist_x) + (dist_y * dist_y); return dist_sq <= cr * cr; } @@ -76,25 +76,25 @@ bool common_hal_vectorio_rectangle_contains_point( int16_t px, int16_t py ) { - if (rx > px){ - return false; - } - if (px > rx + rw){ - return false; - } - if (ry > py){ - return false; - } - if (py > ry + rh){ - return false; - } + if (rx > px) { + return false; + } + if (px > rx + rw) { + return false; + } + if (ry > py) { + return false; + } + if (py > ry + rh) { + return false; + } return true; } -float measure_distance(float x1, float y1, float x2, float y2){ - float dist_x = x1 - x2; - float dist_y = y1 - y2; - return sqrtf((dist_x * dist_x) + (dist_y * dist_y)); +float measure_distance(float x1, float y1, float x2, float y2) { + float dist_x = x1 - x2; + float dist_y = y1 - y2; + return sqrtf((dist_x * dist_x) + (dist_y * dist_y)); } bool common_hal_vectorio_line_contains_point( @@ -102,43 +102,43 @@ bool common_hal_vectorio_line_contains_point( int16_t px, int16_t py, mp_float_t padding ) { - float line_length = measure_distance(x1, y1, x2, y2); - float d1 = measure_distance(x1, y1, px, py); - float d2 = measure_distance(x2, y2, px, py); + float line_length = measure_distance(x1, y1, x2, y2); + float d1 = measure_distance(x1, y1, px, py); + float d2 = measure_distance(x2, y2, px, py); - if (d1+d2 >= line_length-padding && d1+d2 <= line_length+padding) { - return true; - } - return false; + if (d1 + d2 >= line_length - padding && d1 + d2 <= line_length + padding) { + return true; + } + return false; } bool common_hal_vectorio_line_circle_intersects( - int16_t x1, int16_t y1, int16_t x2, int16_t y2, - int16_t cx, int16_t cy, int16_t cr, mp_float_t padding -){ - if (common_hal_vectorio_circle_contains_point(cx, cy, cr, x1, y1)){ - return true; - } - if (common_hal_vectorio_circle_contains_point(cx, cy, cr, x2, y2)){ - return true; - } - float line_length = measure_distance(x1, y1, x2, y2); - - float dot = ( ((cx-x1)*(x2-x1)) + ((cy-y1)*(y2-y1)) ) / pow(line_length,2); - - float closestX = x1 + (dot * (x2-x1)); - float closestY = y1 + (dot * (y2-y1)); - - if (!common_hal_vectorio_line_contains_point(x1,y1,x2,y2, closestX,closestY, padding)){ - return false; - } - float distance = measure_distance(closestX, closestY, cx, cy); - if (distance <= cr){ - return true; - }else{ - return false; - } + int16_t x1, int16_t y1, int16_t x2, int16_t y2, + int16_t cx, int16_t cy, int16_t cr, mp_float_t padding + ) { + if (common_hal_vectorio_circle_contains_point(cx, cy, cr, x1, y1)) { + return true; + } + if (common_hal_vectorio_circle_contains_point(cx, cy, cr, x2, y2)) { + return true; + } + float line_length = measure_distance(x1, y1, x2, y2); + + float dot = (((cx - x1) * (x2 - x1)) + ((cy - y1) * (y2 - y1))) / pow(line_length, 2); + + float closestX = x1 + (dot * (x2 - x1)); + float closestY = y1 + (dot * (y2 - y1)); + + if (!common_hal_vectorio_line_contains_point(x1, y1, x2, y2, closestX, closestY, padding)) { + return false; + } + float distance = measure_distance(closestX, closestY, cx, cy); + if (distance <= cr) { + return true; + } else { + return false; + } } @@ -162,62 +162,62 @@ bool common_hal_vectorio_rectangle_rectangle_intersects( } bool common_hal_vectorio_polygon_circle_intersects( - mp_obj_t points_list, int16_t polygon_x, int16_t polygon_y, int16_t cx, int16_t cy, int16_t cr, mp_float_t padding -) { - size_t len = 0; - mp_obj_t *points_list_items; + mp_obj_t points_list, int16_t polygon_x, int16_t polygon_y, int16_t cx, int16_t cy, int16_t cr, mp_float_t padding + ) { + size_t len = 0; + mp_obj_t *points_list_items; mp_obj_list_get(points_list, &len, &points_list_items); - bool polygon_contains_point = false; - for(uint16_t i = 0; i < len; i++){ - size_t cur_tuple_len = 0; - mp_obj_t *cur_point_tuple; + bool polygon_contains_point = false; + for (uint16_t i = 0; i < len; i++) { + size_t cur_tuple_len = 0; + mp_obj_t *cur_point_tuple; mp_arg_validate_type(points_list_items[i], &mp_type_tuple, MP_QSTR_point); mp_obj_tuple_get(points_list_items[i], &cur_tuple_len, &cur_point_tuple); - mp_int_t cur_x = mp_arg_validate_type_int(cur_point_tuple[0], MP_QSTR_x) + polygon_x; - mp_int_t cur_y = mp_arg_validate_type_int(cur_point_tuple[1], MP_QSTR_y) + polygon_y; + mp_int_t cur_x = mp_arg_validate_type_int(cur_point_tuple[0], MP_QSTR_x) + polygon_x; + mp_int_t cur_y = mp_arg_validate_type_int(cur_point_tuple[1], MP_QSTR_y) + polygon_y; - size_t next_tuple_len = 0; - mp_obj_t *next_point_tuple; - int next_index = (i + 1) % len; + size_t next_tuple_len = 0; + mp_obj_t *next_point_tuple; + int next_index = (i + 1) % len; mp_arg_validate_type(points_list_items[next_index], &mp_type_tuple, MP_QSTR_point); mp_obj_tuple_get(points_list_items[next_index], &next_tuple_len, &next_point_tuple); - mp_int_t next_x = mp_arg_validate_type_int(next_point_tuple[0], MP_QSTR_x) + polygon_x; - mp_int_t next_y = mp_arg_validate_type_int(next_point_tuple[1], MP_QSTR_y) + polygon_y; + mp_int_t next_x = mp_arg_validate_type_int(next_point_tuple[0], MP_QSTR_x) + polygon_x; + mp_int_t next_y = mp_arg_validate_type_int(next_point_tuple[1], MP_QSTR_y) + polygon_y; - //mp_printf(&mp_plat_print, "%d,%d - %d,%d \n", cur_x, cur_y, next_x, next_y); + // mp_printf(&mp_plat_print, "%d,%d - %d,%d \n", cur_x, cur_y, next_x, next_y); - if(common_hal_vectorio_line_circle_intersects( - cur_x, cur_y, next_x, next_y, - cx, cy, cr, padding - )){ - return true; - } + if (common_hal_vectorio_line_circle_intersects( + cur_x, cur_y, next_x, next_y, + cx, cy, cr, padding + )) { + return true; + } - if (((cur_y >= cy && cy > next_y) || (cur_y < cy && cy <= next_y)) && - (cx < (next_x - cur_x) * (cy - cur_y) / (next_y - cur_y) + cur_x)){ + if (((cur_y >= cy && cy > next_y) || (cur_y < cy && cy <= next_y)) && + (cx < (next_x - cur_x) * (cy - cur_y) / (next_y - cur_y) + cur_x)) { - polygon_contains_point = !polygon_contains_point; - } + polygon_contains_point = !polygon_contains_point; + } - } - if (polygon_contains_point == true){ - return true; - } - return false; + } + if (polygon_contains_point == true) { + return true; + } + return false; } bool common_hal_vectorio_line_line_intersects( - int16_t x1, int16_t y1, int16_t x2, int16_t y2, - int16_t x3, int16_t y3, int16_t x4, int16_t y4 -){ + int16_t x1, int16_t y1, int16_t x2, int16_t y2, + int16_t x3, int16_t y3, int16_t x4, int16_t y4 + ) { double denom = ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1)); - if (denom >= 0 && denom <= 0){ + if (denom >= 0 && denom <= 0) { return false; } @@ -230,11 +230,10 @@ bool common_hal_vectorio_line_line_intersects( double dx4 = x4; double dy4 = y4; - double dist_a = ((dx4 - dx3) * (dy1 - dy3) - (dy4 - dy3) * (dx1 - dx3)) / denom; double dist_b = ((dx2 - dx1) * (dy1 - dy3) - (dy2 - dy1) * (dx1 - dx3)) / denom; - if ((0 <= dist_a) && (dist_a <= 1) && (0 <= dist_b) && (dist_b <= 1)){ + if ((0 <= dist_a) && (dist_a <= 1) && (0 <= dist_b) && (dist_b <= 1)) { return true; } return false; @@ -242,28 +241,71 @@ bool common_hal_vectorio_line_line_intersects( } bool common_hal_vectorio_line_rectangle_intersects( - int16_t x1, int16_t y1, int16_t x2, int16_t y2, + int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t rx, int16_t ry, int16_t rw, int16_t rh -){ + ) { if (common_hal_vectorio_line_line_intersects(x1, y1, x2, y2, - rx, ry, rx, ry + rh)){ + rx, ry, rx, ry + rh)) { return true; } if (common_hal_vectorio_line_line_intersects(x1, y1, x2, y2, - rx + rw, ry, rx + rw, ry + rh)){ + rx + rw, ry, rx + rw, ry + rh)) { return true; } if (common_hal_vectorio_line_line_intersects(x1, y1, x2, y2, - rx, ry, rx + rw, ry)){ + rx, ry, rx + rw, ry)) { return true; } if (common_hal_vectorio_line_line_intersects(x1, y1, x2, y2, - rx, ry + rh, rx + rw, ry + rh)){ + rx, ry + rh, rx + rw, ry + rh)) { return true; } - return false; +} -} \ No newline at end of file +bool common_hal_vectorio_polygon_rectangle_intersects( + mp_obj_t points_list, int16_t polygon_x, int16_t polygon_y, + int16_t rx, int16_t ry, int16_t rw, int16_t rh + ) { + size_t len = 0; + mp_obj_t *points_list_items; + mp_obj_list_get(points_list, &len, &points_list_items); + bool polygon_contains_point = false; + for (uint16_t i = 0; i < len; i++) { + size_t cur_tuple_len = 0; + mp_obj_t *cur_point_tuple; + mp_arg_validate_type(points_list_items[i], &mp_type_tuple, MP_QSTR_point); + mp_obj_tuple_get(points_list_items[i], &cur_tuple_len, &cur_point_tuple); + + mp_int_t cur_x = mp_arg_validate_type_int(cur_point_tuple[0], MP_QSTR_x) + polygon_x; + mp_int_t cur_y = mp_arg_validate_type_int(cur_point_tuple[1], MP_QSTR_y) + polygon_y; + + size_t next_tuple_len = 0; + mp_obj_t *next_point_tuple; + int next_index = (i + 1) % len; + mp_arg_validate_type(points_list_items[next_index], &mp_type_tuple, MP_QSTR_point); + mp_obj_tuple_get(points_list_items[next_index], &next_tuple_len, &next_point_tuple); + + mp_int_t next_x = mp_arg_validate_type_int(next_point_tuple[0], MP_QSTR_x) + polygon_x; + mp_int_t next_y = mp_arg_validate_type_int(next_point_tuple[1], MP_QSTR_y) + polygon_y; + + if (common_hal_vectorio_line_rectangle_intersects( + cur_x, cur_y, next_x, next_y, + rx, ry, rw, rh + )) { + return true; + } + + if (((cur_y >= ry && ry > next_y) || (cur_y < ry && ry <= next_y)) && + (rx < (next_x - cur_x) * (ry - cur_y) / (next_y - cur_y) + cur_x)) { + + polygon_contains_point = !polygon_contains_point; + } + } + if (polygon_contains_point) { + return true; + } + return false; +} From 3fe7fdf6b640ce966fb29d2d12b7aa12cb80cdfa Mon Sep 17 00:00:00 2001 From: foamyguy Date: Wed, 23 Oct 2024 13:34:07 -0500 Subject: [PATCH 17/17] trying to remove unrelated changes --- lib/tinyusb | 1 - 1 file changed, 1 deletion(-) delete mode 160000 lib/tinyusb diff --git a/lib/tinyusb b/lib/tinyusb deleted file mode 160000 index 4349e99fb29db..0000000000000 --- a/lib/tinyusb +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 4349e99fb29dbc7d019aa2fbb6344864799d8c1b