Skip to content

Commit a1ac54b

Browse files
committed
starting polygon circle.
1 parent 5db420a commit a1ac54b

File tree

4 files changed

+294
-0
lines changed

4 files changed

+294
-0
lines changed

shared-bindings/vectorio/__init__.c

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,193 @@ static mp_obj_t vectorio_circle_circle_intersects(size_t n_args, const mp_obj_t
185185
}
186186
MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_circle_circle_intersects_obj, 0, vectorio_circle_circle_intersects);
187187

188+
//| def circle_contains_point(
189+
//| cx: int, cy: int, cr: int, px: int, py: int
190+
//| ) -> bool:
191+
//| """Checks whether a circle contains the given point
192+
//|
193+
//| :param int cx: Circle center x coordinate
194+
//| :param int cy: Circle center y coordinate
195+
//| :param int cr: Circle radius
196+
//| :param int px: Point x coordinate
197+
//| :param int py: Point y coordinate
198+
//| ...
199+
//|
200+
static mp_obj_t vectorio_circle_contains_point(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
201+
enum {ARG_cx, ARG_cy, ARG_cr, ARG_px, ARG_py};
202+
203+
static const mp_arg_t allowed_args[] = {
204+
{MP_QSTR_cx, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
205+
{MP_QSTR_cy, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
206+
{MP_QSTR_cr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
207+
{MP_QSTR_px, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
208+
{MP_QSTR_py, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
209+
};
210+
211+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
212+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
213+
214+
int16_t cx = args[ARG_cx].u_int;
215+
int16_t cy = args[ARG_cy].u_int;
216+
int16_t cr = args[ARG_cr].u_int;
217+
int16_t px = args[ARG_px].u_int;
218+
int16_t py = args[ARG_py].u_int;
219+
220+
bool result = common_hal_vectorio_circle_contains_point(cx, cy, cr, px, py);
221+
if (result) {
222+
return mp_const_true;
223+
} else {
224+
return mp_const_false;
225+
}
226+
}
227+
MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_circle_contains_point_obj, 0, vectorio_circle_contains_point);
228+
229+
//| def rectangle_contains_point(
230+
//| rx: int, ry: int, rw: int, rh: int, px: int, py: int
231+
//| ) -> bool:
232+
//| """Checks whether a rectangle contains the given point
233+
//|
234+
//| :param int rx: Rectangle x coordinate
235+
//| :param int ry: Rectangle y coordinate
236+
//| :param int rw: Rectangle width
237+
//| :param int rh: Rectangle height
238+
//| :param int px: Point x coordinate
239+
//| :param int py: Point y coordinate
240+
//| ...
241+
//|
242+
static mp_obj_t vectorio_rectangle_contains_point(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
243+
enum {ARG_rx, ARG_ry, ARG_rw, ARG_rh, ARG_px, ARG_py};
244+
245+
static const mp_arg_t allowed_args[] = {
246+
{MP_QSTR_rx, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
247+
{MP_QSTR_ry, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
248+
{MP_QSTR_rw, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
249+
{MP_QSTR_rh, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
250+
{MP_QSTR_px, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
251+
{MP_QSTR_py, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
252+
};
253+
254+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
255+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
256+
257+
int16_t rx = args[ARG_rx].u_int;
258+
int16_t ry = args[ARG_ry].u_int;
259+
int16_t rw = args[ARG_rw].u_int;
260+
int16_t rh = args[ARG_rh].u_int;
261+
int16_t px = args[ARG_px].u_int;
262+
int16_t py = args[ARG_py].u_int;
263+
264+
bool result = common_hal_vectorio_rectangle_contains_point(rx, ry, rw, rh, px, py);
265+
if (result) {
266+
return mp_const_true;
267+
} else {
268+
return mp_const_false;
269+
}
270+
}
271+
MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_rectangle_contains_point_obj, 0, vectorio_rectangle_contains_point);
272+
273+
274+
//| def line_contains_point(
275+
//| x1: int, y1: int, x2: int, y2: int, px: int, py: int
276+
//| ) -> bool:
277+
//| """Checks whether a line contains the given point
278+
//|
279+
//| :param int x1: Line x1 coordinate
280+
//| :param int y1: Line y1 coordinate
281+
//| :param int x2: Line x2 coordinate
282+
//| :param int y2: Line y2 coordinate
283+
//| :param int px: Point x coordinate
284+
//| :param int py: Point y coordinate
285+
//| :param float padding: Extra padding outside of the line to consider as positive intersection
286+
//| ...
287+
//|
288+
static mp_obj_t vectorio_line_contains_point(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
289+
enum {ARG_x1, ARG_y1, ARG_x2, ARG_y2, ARG_px, ARG_py, ARG_padding};
290+
291+
static const mp_arg_t allowed_args[] = {
292+
{MP_QSTR_x1, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
293+
{MP_QSTR_y1, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
294+
{MP_QSTR_x2, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
295+
{MP_QSTR_y2, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
296+
{MP_QSTR_px, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
297+
{MP_QSTR_py, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
298+
{MP_QSTR_padding, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, // None convert to 0.0
299+
};
300+
301+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
302+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
303+
304+
int16_t x1 = args[ARG_x1].u_int;
305+
int16_t y1 = args[ARG_y1].u_int;
306+
int16_t x2 = args[ARG_x2].u_int;
307+
int16_t y2 = args[ARG_y2].u_int;
308+
int16_t px = args[ARG_px].u_int;
309+
int16_t py = args[ARG_py].u_int;
310+
311+
// Confirm the angle value
312+
mp_float_t padding = 0.0;
313+
if (args[ARG_padding].u_obj != mp_const_none) {
314+
padding = mp_obj_get_float(args[ARG_padding].u_obj);
315+
}
316+
317+
bool result = common_hal_vectorio_line_contains_point(x1, y1, x2, y2, px, py, padding);
318+
if (result) {
319+
return mp_const_true;
320+
} else {
321+
return mp_const_false;
322+
}
323+
}
324+
MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_line_contains_point_obj, 0, vectorio_line_contains_point);
325+
326+
327+
//| def polygon_circle_intersects(
328+
//| points: List[Tuple[int, int]], cx: int, cy: int, cr: int
329+
//| ) -> bool:
330+
//| """Checks for intersection between a polygon and a cricle.
331+
//|
332+
//| :param List[Tuple[int,int]] points: Vertices for the polygon
333+
//| :param int cx: Circle center x coordinate
334+
//| :param int cy: Circle center y coordinate
335+
//| :param int cr: Circle radius"""
336+
//| ...
337+
//|
338+
static mp_obj_t vectorio_polygon_circle_intersects(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
339+
enum {ARG_points_list, ARG_cx, ARG_cy, ARG_cr};
340+
341+
static const mp_arg_t allowed_args[] = {
342+
{MP_QSTR_points, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
343+
{MP_QSTR_cx, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
344+
{MP_QSTR_cy, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
345+
{MP_QSTR_cr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}
346+
};
347+
348+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
349+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
350+
351+
mp_obj_t points_list = mp_arg_validate_type(args[ARG_points_list].u_obj, &mp_type_list, MP_QSTR_points);
352+
353+
int16_t cx = args[ARG_cx].u_int;
354+
int16_t cy = args[ARG_cy].u_int;
355+
int16_t cr = args[ARG_cr].u_int;
356+
357+
bool result = common_hal_vectorio_polygon_circle_intersects(points_list, cx, cy, cr);
358+
if (result) {
359+
return mp_const_true;
360+
} else {
361+
return mp_const_false;
362+
}
363+
}
364+
MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_polygon_circle_intersects_obj, 0, vectorio_polygon_circle_intersects);
365+
366+
188367
static const mp_rom_map_elem_t vectorio_module_globals_table[] = {
189368
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_vectorio) },
190369
{ MP_ROM_QSTR(MP_QSTR_circle_rectangle_intersects), MP_ROM_PTR(&vectorio_circle_rectangle_intersects_obj) },
370+
{ MP_ROM_QSTR(MP_QSTR_polygon_circle_intersects), MP_ROM_PTR(&vectorio_polygon_circle_intersects_obj) },
191371
{ MP_ROM_QSTR(MP_QSTR_circle_circle_intersects), MP_ROM_PTR(&vectorio_circle_circle_intersects_obj) },
372+
{ MP_ROM_QSTR(MP_QSTR_circle_contains_point), MP_ROM_PTR(&vectorio_circle_contains_point_obj) },
373+
{ MP_ROM_QSTR(MP_QSTR_rectangle_contains_point), MP_ROM_PTR(&vectorio_rectangle_contains_point_obj) },
374+
{ MP_ROM_QSTR(MP_QSTR_line_contains_point), MP_ROM_PTR(&vectorio_line_contains_point_obj) },
192375
{ MP_ROM_QSTR(MP_QSTR_rectangle_rectangle_intersects), MP_ROM_PTR(&vectorio_rectangle_rectangle_intersects_obj) },
193376
{ MP_ROM_QSTR(MP_QSTR_Circle), MP_ROM_PTR(&vectorio_circle_type) },
194377
{ MP_ROM_QSTR(MP_QSTR_Polygon), MP_ROM_PTR(&vectorio_polygon_type) },

shared-bindings/vectorio/__init__.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,18 @@ bool common_hal_vectorio_rectangle_rectangle_intersects(
5656
bool common_hal_vectorio_circle_circle_intersects(
5757
int16_t c1x, int16_t c1y, int16_t c1r,
5858
int16_t c2x, int16_t c2y, int16_t c2r);
59+
60+
bool common_hal_vectorio_circle_contains_point(
61+
int16_t cx, int16_t cy, int16_t cr,
62+
int16_t px, int16_t py);
63+
64+
bool common_hal_vectorio_rectangle_contains_point(
65+
int16_t rx, int16_t ry, int16_t rw, int16_t rh,
66+
int16_t px, int16_t py);
67+
68+
bool common_hal_vectorio_line_contains_point(
69+
int16_t x1, int16_t y1, int16_t x2, int16_t y2,
70+
int16_t px, int16_t py, mp_float_t padding);
71+
72+
bool common_hal_vectorio_polygon_circle_intersects(
73+
mp_obj_t points_list, int16_t cx, int16_t cy, int16_t cr);

shared-module/vectorio/__init__.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
// Don't need anything in here yet
88

99
#include "shared-bindings/vectorio/__init__.h"
10+
#include "shared-module/vectorio/__init__.h"
11+
1012
#include "py/runtime.h"
1113
#include "stdlib.h"
1214
#include <math.h>
@@ -56,6 +58,63 @@ bool common_hal_vectorio_circle_circle_intersects(
5658

5759
}
5860

61+
bool common_hal_vectorio_circle_contains_point(
62+
int16_t cx, int16_t cy, int16_t cr,
63+
int16_t px, int16_t py
64+
) {
65+
66+
mp_int_t dist_x = px - cx;
67+
mp_int_t dist_y = py - cy;
68+
69+
mp_int_t dist_sq = (dist_x * dist_x) + (dist_y * dist_y);
70+
71+
return dist_sq <= cr * cr;
72+
}
73+
74+
bool common_hal_vectorio_rectangle_contains_point(
75+
int16_t rx, int16_t ry, int16_t rw, int16_t rh,
76+
int16_t px, int16_t py
77+
) {
78+
79+
if (rx > px){
80+
return false;
81+
}
82+
if (px > rx + rw){
83+
return false;
84+
}
85+
if (ry > py){
86+
return false;
87+
}
88+
if (py > ry + rh){
89+
return false;
90+
}
91+
return true;
92+
}
93+
94+
float measure_distance(int x1, int y1, int x2, int y2){
95+
int dist_x = x1 - x2;
96+
int dist_y = y1 - y2;
97+
return sqrtf((dist_x * dist_x) + (dist_y * dist_y));
98+
}
99+
100+
bool common_hal_vectorio_line_contains_point(
101+
int16_t x1, int16_t y1, int16_t x2, int16_t y2,
102+
int16_t px, int16_t py, mp_float_t padding
103+
) {
104+
105+
float line_length = measure_distance(x1, y1, x2, y2);
106+
float d1 = measure_distance(x1, y1, px, py);
107+
float d2 = measure_distance(x2, y2, px, py);
108+
109+
if (d1+d2 >= line_length-padding && d1+d2 <= line_length+padding) {
110+
return true;
111+
}
112+
return false;
113+
114+
}
115+
116+
117+
59118
bool common_hal_vectorio_rectangle_rectangle_intersects(
60119
int16_t r1x, int16_t r1y, int16_t r1w, int16_t r1h,
61120
int16_t r2x, int16_t r2y, int16_t r2w, int16_t r2h) {
@@ -74,3 +133,37 @@ bool common_hal_vectorio_rectangle_rectangle_intersects(
74133
return r1_left < r2_right && r1_right > r2_left &&
75134
r1_top < r2_bottom && r1_bottom > r2_top;
76135
}
136+
137+
bool common_hal_vectorio_polygon_circle_intersects(
138+
mp_obj_t points_list, int16_t cx, int16_t cy, int16_t cr
139+
) {
140+
size_t len = 0;
141+
mp_obj_t *points_list_items;
142+
mp_obj_list_get(points_list, &len, &points_list_items);
143+
144+
for(uint16_t i = 0; i < len; i++){
145+
size_t cur_tuple_len = 0;
146+
mp_obj_t *cur_point_tuple;
147+
mp_arg_validate_type(points_list_items[i], &mp_type_tuple, MP_QSTR_point);
148+
mp_obj_tuple_get(points_list_items[i], &cur_tuple_len, &cur_point_tuple);
149+
150+
mp_int_t cur_x = mp_arg_validate_type_int(cur_point_tuple[0], MP_QSTR_x);
151+
mp_int_t cur_y = mp_arg_validate_type_int(cur_point_tuple[1], MP_QSTR_y);
152+
153+
size_t next_tuple_len = 0;
154+
mp_obj_t *next_point_tuple;
155+
int next_index = (i + 1) % len;
156+
mp_arg_validate_type(points_list_items[next_index], &mp_type_tuple, MP_QSTR_point);
157+
mp_obj_tuple_get(points_list_items[next_index], &next_tuple_len, &next_point_tuple);
158+
159+
mp_int_t next_x = mp_arg_validate_type_int(next_point_tuple[0], MP_QSTR_x);
160+
mp_int_t next_y = mp_arg_validate_type_int(next_point_tuple[1], MP_QSTR_y);
161+
162+
mp_printf(&mp_plat_print, "%d,%d - %d,%d \n", cur_x, cur_y, next_x, next_y);
163+
164+
165+
166+
}
167+
168+
return false;
169+
}

shared-module/vectorio/__init__.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ typedef struct {
1414
mp_obj_t obj;
1515
event_function *event;
1616
} vectorio_event_t;
17+
18+
float measure_distance(
19+
int x1, int y1, int x2, int y2);

0 commit comments

Comments
 (0)