Skip to content

Commit b0fccc8

Browse files
committed
line circle and polygon circle.
1 parent a1ac54b commit b0fccc8

File tree

4 files changed

+135
-18
lines changed

4 files changed

+135
-18
lines changed

shared-bindings/vectorio/__init__.c

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_rectangle_contains_point_obj, 0, vectorio_re
282282
//| :param int y2: Line y2 coordinate
283283
//| :param int px: Point x coordinate
284284
//| :param int py: Point y coordinate
285-
//| :param float padding: Extra padding outside of the line to consider as positive intersection
285+
//| :param float padding: Extra padding outside of the line to consider as positive intersection"""
286286
//| ...
287287
//|
288288
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_
308308
int16_t px = args[ARG_px].u_int;
309309
int16_t py = args[ARG_py].u_int;
310310

311-
// Confirm the angle value
311+
// Use default padding if None was passed
312312
mp_float_t padding = 0.0;
313313
if (args[ARG_padding].u_obj != mp_const_none) {
314314
padding = mp_obj_get_float(args[ARG_padding].u_obj);
@@ -324,37 +324,115 @@ static mp_obj_t vectorio_line_contains_point(size_t n_args, const mp_obj_t *pos_
324324
MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_line_contains_point_obj, 0, vectorio_line_contains_point);
325325

326326

327+
//| def line_circle_intersects(
328+
//| x1: int, y1: int, x2: int, y2: int,
329+
// cx: int, cy: int, cr: int
330+
//| ) -> bool:
331+
//| """Checks whether a line intersects with a circle
332+
//|
333+
//| :param int x1: Line x1 coordinate
334+
//| :param int y1: Line y1 coordinate
335+
//| :param int x2: Line x2 coordinate
336+
//| :param int y2: Line y2 coordinate
337+
//| :param int cx: Circle center x coordinate
338+
//| :param int cy: Circle center y coordinate
339+
//| :param int cr: Circle radius
340+
//| :param float padding: Extra padding outside of the line to consider as positive intersection
341+
//| ...
342+
//|
343+
static mp_obj_t vectorio_line_circle_intersects(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
344+
enum {ARG_x1, ARG_y1, ARG_x2, ARG_y2, ARG_cx, ARG_cy, ARG_cr, ARG_padding};
345+
346+
static const mp_arg_t allowed_args[] = {
347+
{MP_QSTR_x1, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
348+
{MP_QSTR_y1, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
349+
{MP_QSTR_x2, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
350+
{MP_QSTR_y2, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
351+
{MP_QSTR_cx, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
352+
{MP_QSTR_cy, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
353+
{MP_QSTR_cr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
354+
{MP_QSTR_padding, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, // None convert to 0.0
355+
};
356+
357+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
358+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
359+
360+
int16_t x1 = args[ARG_x1].u_int;
361+
int16_t y1 = args[ARG_y1].u_int;
362+
int16_t x2 = args[ARG_x2].u_int;
363+
int16_t y2 = args[ARG_y2].u_int;
364+
int16_t cx = args[ARG_cx].u_int;
365+
int16_t cy = args[ARG_cy].u_int;
366+
int16_t cr = args[ARG_cr].u_int;
367+
368+
// Use default padding if None was passed
369+
mp_float_t padding = 0.0;
370+
if (args[ARG_padding].u_obj != mp_const_none) {
371+
padding = mp_obj_get_float(args[ARG_padding].u_obj);
372+
}
373+
374+
bool result = common_hal_vectorio_line_circle_intersects(x1, y1, x2, y2, cx, cy, cr, padding);
375+
376+
if (result) {
377+
return mp_const_true;
378+
} else {
379+
return mp_const_false;
380+
}
381+
}
382+
MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_line_circle_intersects_obj, 0, vectorio_line_circle_intersects);
383+
384+
327385
//| def polygon_circle_intersects(
328-
//| points: List[Tuple[int, int]], cx: int, cy: int, cr: int
386+
//| points: List[Tuple[int, int]], polygon_x: int, polygon_y: int,
387+
//| cx: int, cy: int, cr: int, padding: float
329388
//| ) -> bool:
330389
//| """Checks for intersection between a polygon and a cricle.
331390
//|
332391
//| :param List[Tuple[int,int]] points: Vertices for the polygon
392+
//| :param int polygon_x: Polygon x coordinate. All other polygon points are relative to this
393+
//| :param int polygon_y: Polygon y coordinate. All other polygon points are relative to this
333394
//| :param int cx: Circle center x coordinate
334395
//| :param int cy: Circle center y coordinate
335-
//| :param int cr: Circle radius"""
396+
//| :param int cr: Circle radius
397+
//| :param float padding: Extra padding outside of the line to consider as positive intersection"""
336398
//| ...
337399
//|
338400
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};
401+
enum {ARG_points_list, ARG_polygon_x, ARG_polygon_y, ARG_cx, ARG_cy, ARG_cr, ARG_padding};
340402

341403
static const mp_arg_t allowed_args[] = {
342404
{MP_QSTR_points, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
405+
{MP_QSTR_polygon_x, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
406+
{MP_QSTR_polygon_y, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
343407
{MP_QSTR_cx, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
344408
{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}}
409+
{MP_QSTR_cr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
410+
{MP_QSTR_padding, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, // None convert to 0.0
346411
};
347412

348413
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
349414
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
350415

351416
mp_obj_t points_list = mp_arg_validate_type(args[ARG_points_list].u_obj, &mp_type_list, MP_QSTR_points);
352417

418+
int16_t polygon_x = args[ARG_polygon_x].u_int;
419+
int16_t polygon_y = args[ARG_polygon_y].u_int;
420+
353421
int16_t cx = args[ARG_cx].u_int;
354422
int16_t cy = args[ARG_cy].u_int;
355423
int16_t cr = args[ARG_cr].u_int;
356424

357-
bool result = common_hal_vectorio_polygon_circle_intersects(points_list, cx, cy, cr);
425+
// Use default padding if None was passed
426+
mp_float_t padding = 0.0;
427+
if (args[ARG_padding].u_obj != mp_const_none) {
428+
padding = mp_obj_get_float(args[ARG_padding].u_obj);
429+
}
430+
431+
bool result = common_hal_vectorio_polygon_circle_intersects(
432+
points_list, polygon_x, polygon_y,
433+
cx, cy, cr, padding
434+
);
435+
358436
if (result) {
359437
return mp_const_true;
360438
} else {
@@ -369,6 +447,7 @@ static const mp_rom_map_elem_t vectorio_module_globals_table[] = {
369447
{ MP_ROM_QSTR(MP_QSTR_circle_rectangle_intersects), MP_ROM_PTR(&vectorio_circle_rectangle_intersects_obj) },
370448
{ MP_ROM_QSTR(MP_QSTR_polygon_circle_intersects), MP_ROM_PTR(&vectorio_polygon_circle_intersects_obj) },
371449
{ MP_ROM_QSTR(MP_QSTR_circle_circle_intersects), MP_ROM_PTR(&vectorio_circle_circle_intersects_obj) },
450+
{ MP_ROM_QSTR(MP_QSTR_line_circle_intersects), MP_ROM_PTR(&vectorio_line_circle_intersects_obj) },
372451
{ MP_ROM_QSTR(MP_QSTR_circle_contains_point), MP_ROM_PTR(&vectorio_circle_contains_point_obj) },
373452
{ MP_ROM_QSTR(MP_QSTR_rectangle_contains_point), MP_ROM_PTR(&vectorio_rectangle_contains_point_obj) },
374453
{ MP_ROM_QSTR(MP_QSTR_line_contains_point), MP_ROM_PTR(&vectorio_line_contains_point_obj) },

shared-bindings/vectorio/__init__.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,10 @@ bool common_hal_vectorio_line_contains_point(
6969
int16_t x1, int16_t y1, int16_t x2, int16_t y2,
7070
int16_t px, int16_t py, mp_float_t padding);
7171

72+
bool common_hal_vectorio_line_circle_intersects(
73+
int16_t x1, int16_t y1, int16_t x2, int16_t y2,
74+
int16_t cx, int16_t cy, int16_t cr, mp_float_t padding);
75+
7276
bool common_hal_vectorio_polygon_circle_intersects(
73-
mp_obj_t points_list, int16_t cx, int16_t cy, int16_t cr);
77+
mp_obj_t points_list, int16_t polygon_x, int16_t polygon_y,
78+
int16_t cx, int16_t cy, int16_t cr, mp_float_t padding);

shared-module/vectorio/__init__.c

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ bool common_hal_vectorio_rectangle_contains_point(
9191
return true;
9292
}
9393

94-
float measure_distance(int x1, int y1, int x2, int y2){
95-
int dist_x = x1 - x2;
96-
int dist_y = y1 - y2;
94+
float measure_distance(float x1, float y1, float x2, float y2){
95+
float dist_x = x1 - x2;
96+
float dist_y = y1 - y2;
9797
return sqrtf((dist_x * dist_x) + (dist_y * dist_y));
9898
}
9999

@@ -113,7 +113,34 @@ bool common_hal_vectorio_line_contains_point(
113113

114114
}
115115

116+
bool common_hal_vectorio_line_circle_intersects(
117+
int16_t x1, int16_t y1, int16_t x2, int16_t y2,
118+
int16_t cx, int16_t cy, int16_t cr, mp_float_t padding
119+
){
120+
if (common_hal_vectorio_circle_contains_point(cx, cy, cr, x1, y1)){
121+
return true;
122+
}
123+
if (common_hal_vectorio_circle_contains_point(cx, cy, cr, x2, y2)){
124+
return true;
125+
}
126+
float line_length = measure_distance(x1, y1, x2, y2);
116127

128+
float dot = ( ((cx-x1)*(x2-x1)) + ((cy-y1)*(y2-y1)) ) / pow(line_length,2);
129+
130+
float closestX = x1 + (dot * (x2-x1));
131+
float closestY = y1 + (dot * (y2-y1));
132+
133+
if (!common_hal_vectorio_line_contains_point(x1,y1,x2,y2, closestX,closestY, padding)){
134+
return false;
135+
}
136+
float distance = measure_distance(closestX, closestY, cx, cy);
137+
if (distance <= cr){
138+
return true;
139+
}else{
140+
return false;
141+
}
142+
143+
}
117144

118145
bool common_hal_vectorio_rectangle_rectangle_intersects(
119146
int16_t r1x, int16_t r1y, int16_t r1w, int16_t r1h,
@@ -135,7 +162,7 @@ bool common_hal_vectorio_rectangle_rectangle_intersects(
135162
}
136163

137164
bool common_hal_vectorio_polygon_circle_intersects(
138-
mp_obj_t points_list, int16_t cx, int16_t cy, int16_t cr
165+
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
139166
) {
140167
size_t len = 0;
141168
mp_obj_t *points_list_items;
@@ -147,20 +174,26 @@ bool common_hal_vectorio_polygon_circle_intersects(
147174
mp_arg_validate_type(points_list_items[i], &mp_type_tuple, MP_QSTR_point);
148175
mp_obj_tuple_get(points_list_items[i], &cur_tuple_len, &cur_point_tuple);
149176

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);
177+
mp_int_t cur_x = mp_arg_validate_type_int(cur_point_tuple[0], MP_QSTR_x) + polygon_x;
178+
mp_int_t cur_y = mp_arg_validate_type_int(cur_point_tuple[1], MP_QSTR_y) + polygon_y;
152179

153180
size_t next_tuple_len = 0;
154181
mp_obj_t *next_point_tuple;
155182
int next_index = (i + 1) % len;
156183
mp_arg_validate_type(points_list_items[next_index], &mp_type_tuple, MP_QSTR_point);
157184
mp_obj_tuple_get(points_list_items[next_index], &next_tuple_len, &next_point_tuple);
158185

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);
186+
mp_int_t next_x = mp_arg_validate_type_int(next_point_tuple[0], MP_QSTR_x) + polygon_x;
187+
mp_int_t next_y = mp_arg_validate_type_int(next_point_tuple[1], MP_QSTR_y) + polygon_y;
161188

162-
mp_printf(&mp_plat_print, "%d,%d - %d,%d \n", cur_x, cur_y, next_x, next_y);
189+
//mp_printf(&mp_plat_print, "%d,%d - %d,%d \n", cur_x, cur_y, next_x, next_y);
163190

191+
if(common_hal_vectorio_line_circle_intersects(
192+
cur_x, cur_y, next_x, next_y,
193+
cx, cy, cr, padding
194+
)){
195+
return true;
196+
}
164197

165198

166199
}

shared-module/vectorio/__init__.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ typedef struct {
1616
} vectorio_event_t;
1717

1818
float measure_distance(
19-
int x1, int y1, int x2, int y2);
19+
float x1, float y1, float x2, float y2);

0 commit comments

Comments
 (0)