Skip to content

Commit

Permalink
polygon rectangle function. cleanup uneeded changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
FoamyGuy committed Oct 23, 2024
1 parent f2dcb67 commit 5093988
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 139 deletions.
2 changes: 1 addition & 1 deletion lib/tinyusb
Submodule tinyusb updated 123 files
2 changes: 1 addition & 1 deletion ports/espressif/esp-idf
Submodule esp-idf updated 716 files
1 change: 0 additions & 1 deletion shared-bindings/vectorio/Rectangle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
123 changes: 97 additions & 26 deletions shared-bindings/vectorio/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -226,17 +224,15 @@ 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
//| :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
//| :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) {
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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.
//|
Expand All @@ -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}},
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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
//|
Expand All @@ -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) {
Expand Down Expand Up @@ -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
//|
Expand All @@ -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) {
Expand All @@ -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)];
Expand All @@ -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) },
Expand All @@ -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) },
Expand Down
20 changes: 12 additions & 8 deletions shared-bindings/vectorio/__init__.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
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);
5 changes: 1 addition & 4 deletions shared-module/vectorio/Circle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <math.h>


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


void common_hal_vectorio_rectangle_construct(vectorio_rectangle_t *self, uint32_t width, uint32_t height, uint16_t color_index) {
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 5093988

Please sign in to comment.