Skip to content

Commit

Permalink
rename to intersects. add circle.intersects
Browse files Browse the repository at this point in the history
  • Loading branch information
FoamyGuy committed Aug 7, 2024
1 parent 1fda272 commit 05d5c2a
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 8 deletions.
10 changes: 10 additions & 0 deletions shared-bindings/vectorio/Circle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) },
};
Expand Down
2 changes: 2 additions & 0 deletions shared-bindings/vectorio/Circle.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
10 changes: 5 additions & 5 deletions shared-bindings/vectorio/Rectangle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) },
Expand Down
2 changes: 1 addition & 1 deletion shared-bindings/vectorio/Rectangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
62 changes: 61 additions & 1 deletion shared-module/vectorio/Circle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <math.h>


void common_hal_vectorio_circle_construct(vectorio_circle_t *self, uint16_t radius, uint16_t color_index) {
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion shared-module/vectorio/Rectangle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 05d5c2a

Please sign in to comment.