Skip to content

Commit c44edcb

Browse files
committed
starting refactor to module functions instead of class functions
1 parent c9979ae commit c44edcb

File tree

3 files changed

+222
-0
lines changed

3 files changed

+222
-0
lines changed

shared-bindings/vectorio/__init__.c

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
#include "py/obj.h"
1010
#include "py/runtime.h"
1111

12+
1213
#include "shared-bindings/vectorio/Circle.h"
1314
#include "shared-bindings/vectorio/Polygon.h"
1415
#include "shared-bindings/vectorio/Rectangle.h"
16+
#include "shared-bindings/vectorio/__init__.h"
1517

1618
//| """Lightweight 2D shapes for displays
1719
//|
@@ -37,8 +39,147 @@
3739
//|
3840
//| """
3941

42+
43+
//| def circle_rectangle_intersects(
44+
//| cx: int, cy: int, cr: int, rx: int, ry: int, rw: int, rh: int
45+
//| :param int cx: Circle center x coordinate
46+
//| :param int cy: Circle center y coordinate
47+
//| :param int cr: Circle radius
48+
//| :param int rx: Rectangle x coordinate
49+
//| :param int ry: Rectangle y coordinate
50+
//| :param int rw: Rectangle width
51+
//| :param int rh: Rectangle height
52+
//| ) -> None:
53+
static mp_obj_t vectorio_circle_rectangle_intersects(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
54+
enum {ARG_cx, ARG_cy, ARG_cr, ARG_rx, ARG_ry, ARG_rw, ARG_rh};
55+
56+
static const mp_arg_t allowed_args[] = {
57+
{MP_QSTR_cx, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
58+
{MP_QSTR_cy, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
59+
{MP_QSTR_cr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
60+
{MP_QSTR_rx, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
61+
{MP_QSTR_ry, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
62+
{MP_QSTR_rw, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
63+
{MP_QSTR_rh, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
64+
};
65+
66+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
67+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
68+
69+
int16_t cx = args[ARG_cx].u_int;
70+
int16_t cy = args[ARG_cy].u_int;
71+
int16_t cr = args[ARG_cr].u_int;
72+
int16_t rx = args[ARG_rx].u_int;
73+
int16_t ry = args[ARG_ry].u_int;
74+
int16_t rw = args[ARG_rw].u_int;
75+
int16_t rh = args[ARG_rh].u_int;
76+
77+
bool result = common_hal_vectorio_circle_rectangle_intersects(cx, cy, cr, rx, ry, rw, rh);
78+
if (result){
79+
return mp_const_true;
80+
}else{
81+
return mp_const_false;
82+
}
83+
}
84+
MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_circle_rectangle_intersects_obj, 0, vectorio_circle_rectangle_intersects);
85+
86+
87+
//| def rectangle_rectangle_intersects(
88+
//| r1x: int, r1y: int, r1w: int, r1h: int,
89+
// r2x: int, r2y: int, r2w: int, r2h: int
90+
//|
91+
//| :param int r1x: Rectangle x coordinate
92+
//| :param int r1y: Rectangle y coordinate
93+
//| :param int r1w: Rectangle width
94+
//| :param int r1h: Rectangle height
95+
//| :param int r2x: Other Rectangle x coordinate
96+
//| :param int r2y: Other Rectangle y coordinate
97+
//| :param int r2w: Other Rectangle width
98+
//| :param int r2h: Other Rectangle height
99+
//| ) -> None:
100+
static mp_obj_t vectorio_rectangle_rectangle_intersects(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
101+
enum {ARG_r1x, ARG_r1y, ARG_r1w, ARG_r1h, ARG_r2x, ARG_r2y, ARG_r2w, ARG_r2h};
102+
103+
static const mp_arg_t allowed_args[] = {
104+
{MP_QSTR_r1x, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
105+
{MP_QSTR_r1y, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
106+
{MP_QSTR_r1w, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
107+
{MP_QSTR_r1h, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
108+
{MP_QSTR_r2x, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
109+
{MP_QSTR_r2y, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
110+
{MP_QSTR_r2w, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
111+
{MP_QSTR_r2h, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
112+
};
113+
114+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
115+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
116+
117+
int16_t r1x = args[ARG_r1x].u_int;
118+
int16_t r1y = args[ARG_r1y].u_int;
119+
int16_t r1w = args[ARG_r1w].u_int;
120+
int16_t r1h = args[ARG_r1h].u_int;
121+
122+
int16_t r2x = args[ARG_r2x].u_int;
123+
int16_t r2y = args[ARG_r2y].u_int;
124+
int16_t r2w = args[ARG_r2w].u_int;
125+
int16_t r2h = args[ARG_r2h].u_int;
126+
127+
128+
bool result = common_hal_vectorio_rectangle_rectangle_intersects(r1x, r1y, r1w, r1h, r2x, r2y, r2w, r2h);
129+
if (result){
130+
return mp_const_true;
131+
}else{
132+
return mp_const_false;
133+
}
134+
}
135+
MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_rectangle_rectangle_intersects_obj, 0, vectorio_rectangle_rectangle_intersects);
136+
137+
138+
//| def circle_circle_intersects(
139+
//| cx: int, cy: int, cr: int, rx: int, ry: int, rw: int, rh: int
140+
//| :param int c1x: Circle center x coordinate
141+
//| :param int c1y: Circle center y coordinate
142+
//| :param int c1r: Circle radius
143+
//| :param int c2x: Other Circle center x coordinate
144+
//| :param int c2y: Other Circle center y coordinate
145+
//| :param int c2r: Other Circle radius
146+
//| ) -> None:
147+
static mp_obj_t vectorio_circle_circle_intersects(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
148+
enum {ARG_c1x, ARG_c1y, ARG_c1r, ARG_c2x, ARG_c2y, ARG_c2r};
149+
150+
static const mp_arg_t allowed_args[] = {
151+
{MP_QSTR_c1x, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
152+
{MP_QSTR_c1y, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
153+
{MP_QSTR_c1r, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
154+
{MP_QSTR_c2x, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
155+
{MP_QSTR_c2y, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
156+
{MP_QSTR_c2r, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
157+
};
158+
159+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
160+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
161+
162+
int16_t c1x = args[ARG_c1x].u_int;
163+
int16_t c1y = args[ARG_c1y].u_int;
164+
int16_t c1r = args[ARG_c1r].u_int;
165+
int16_t c2x = args[ARG_c2x].u_int;
166+
int16_t c2y = args[ARG_c2y].u_int;
167+
int16_t c2r = args[ARG_c2r].u_int;
168+
169+
bool result = common_hal_vectorio_circle_circle_intersects(c1x, c1y, c1r, c2x, c2y, c2r);
170+
if (result){
171+
return mp_const_true;
172+
}else{
173+
return mp_const_false;
174+
}
175+
}
176+
MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_circle_circle_intersects_obj, 0, vectorio_circle_circle_intersects);
177+
40178
static const mp_rom_map_elem_t vectorio_module_globals_table[] = {
41179
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_vectorio) },
180+
{ MP_ROM_QSTR(MP_QSTR_circle_rectangle_intersects), MP_ROM_PTR(&vectorio_circle_rectangle_intersects_obj) },
181+
{ MP_ROM_QSTR(MP_QSTR_circle_circle_intersects), MP_ROM_PTR(&vectorio_circle_circle_intersects_obj) },
182+
{ MP_ROM_QSTR(MP_QSTR_rectangle_rectangle_intersects), MP_ROM_PTR(&vectorio_rectangle_rectangle_intersects_obj) },
42183
{ MP_ROM_QSTR(MP_QSTR_Circle), MP_ROM_PTR(&vectorio_circle_type) },
43184
{ MP_ROM_QSTR(MP_QSTR_Polygon), MP_ROM_PTR(&vectorio_polygon_type) },
44185
{ MP_ROM_QSTR(MP_QSTR_Rectangle), MP_ROM_PTR(&vectorio_rectangle_type) },

shared-bindings/vectorio/__init__.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,15 @@ typedef struct _vectorio_draw_protocol_t {
4444
// Implementation functions for the draw protocol
4545
vectorio_draw_protocol_impl_t *draw_protocol_impl;
4646
} vectorio_draw_protocol_t;
47+
48+
bool common_hal_vectorio_circle_rectangle_intersects(
49+
int16_t cx, int16_t cy, int16_t cr,
50+
int16_t rx, int16_t ry, int16_t rw, int16_t rh);
51+
52+
bool common_hal_vectorio_rectangle_rectangle_intersects(
53+
int16_t r1x, int16_t r1y, int16_t r1w, int16_t r1h,
54+
int16_t r2x, int16_t r2y, int16_t r2w, int16_t r2h);
55+
56+
bool common_hal_vectorio_circle_circle_intersects(
57+
int16_t c1x, int16_t c1y, int16_t c1r,
58+
int16_t c2x, int16_t c2y, int16_t c2r);

shared-module/vectorio/__init__.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,72 @@
55
// SPDX-License-Identifier: MIT
66

77
// Don't need anything in here yet
8+
9+
#include "shared-bindings/vectorio/__init__.h"
10+
#include "py/runtime.h"
11+
#include "stdlib.h"
12+
#include <math.h>
13+
14+
bool common_hal_vectorio_circle_rectangle_intersects(
15+
int16_t cx, int16_t cy, int16_t cr,
16+
int16_t rx, int16_t ry, int16_t rw, int16_t rh){
17+
18+
mp_int_t rect_left = rx;
19+
mp_int_t rect_right = rect_left + rw;
20+
mp_int_t rect_top = ry;
21+
mp_int_t rect_bottom = rect_top + rh;
22+
23+
mp_int_t test_x = cx;
24+
mp_int_t test_y = cy;
25+
26+
if (cx < rect_left) {
27+
test_x = rect_left;
28+
} else if (cx > rect_right) {
29+
test_x = rect_right;
30+
}
31+
32+
if (cy < rect_top) {
33+
test_y = rect_top;
34+
} else if (cy > rect_bottom) {
35+
test_y = rect_bottom;
36+
}
37+
38+
mp_int_t dist_x = cx - test_x;
39+
mp_int_t dist_y = cy - test_y;
40+
mp_int_t dist = (dist_x * dist_x) + (dist_y * dist_y);
41+
42+
return dist <= cr*cr;
43+
}
44+
45+
bool common_hal_vectorio_circle_circle_intersects(
46+
int16_t c1x, int16_t c1y, int16_t c1r,
47+
int16_t c2x, int16_t c2y, int16_t c2r
48+
){
49+
50+
mp_int_t dist_x = c1x - c2x;
51+
mp_int_t dist_y = c1y - c2y;
52+
53+
mp_int_t dist = (dist_x * dist_x) + (dist_y * dist_y);
54+
55+
return dist <= (c1r + c2r) * (c1r + c2r);
56+
57+
}
58+
59+
bool common_hal_vectorio_rectangle_rectangle_intersects(
60+
int16_t r1x, int16_t r1y, int16_t r1w, int16_t r1h,
61+
int16_t r2x, int16_t r2y, int16_t r2w, int16_t r2h){
62+
63+
64+
mp_int_t r1_left = r1x;
65+
mp_int_t r1_right = r1_left + r1w;
66+
mp_int_t r1_top = r1y;
67+
mp_int_t r1_bottom = r1_top + r1h;
68+
69+
mp_int_t r2_left = r2x;
70+
mp_int_t r2_right = r2_left + r2w;
71+
mp_int_t r2_top = r2y;
72+
mp_int_t r2_bottom = r2_top + r2h;
73+
74+
return r1_left < r2_right && r1_right > r2_left &&
75+
r1_top < r2_bottom && r1_bottom > r2_top;
76+
}

0 commit comments

Comments
 (0)