Skip to content

Commit f2dcb67

Browse files
committed
starting polygon rectangle
1 parent b0fccc8 commit f2dcb67

File tree

3 files changed

+188
-2
lines changed

3 files changed

+188
-2
lines changed

shared-bindings/vectorio/__init__.c

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,12 +442,121 @@ static mp_obj_t vectorio_polygon_circle_intersects(size_t n_args, const mp_obj_t
442442
MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_polygon_circle_intersects_obj, 0, vectorio_polygon_circle_intersects);
443443

444444

445+
//| def line_line_intersects(
446+
//| x1: int, y1: int, x2: int, y2: int,
447+
//| x3: int, y3: int, x4: int, y4: int,
448+
//| ) -> bool:
449+
//| """Checks whether a line intersects with another line
450+
//|
451+
//| :param int x1: Line x1 coordinate
452+
//| :param int y1: Line y1 coordinate
453+
//| :param int x2: Line x2 coordinate
454+
//| :param int y2: Line y2 coordinate
455+
//| :param int x3: Other Line x3 coordinate
456+
//| :param int y3: Other Line y3 coordinate
457+
//| :param int x4: Other Line x4 coordinate
458+
//| :param int y4: Other Line y4 coordinate
459+
//| ...
460+
//|
461+
static mp_obj_t vectorio_line_line_intersects(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
462+
enum {ARG_x1, ARG_y1, ARG_x2, ARG_y2, ARG_x3, ARG_y3, ARG_x4, ARG_y4 };
463+
464+
static const mp_arg_t allowed_args[] = {
465+
{MP_QSTR_x1, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
466+
{MP_QSTR_y1, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
467+
{MP_QSTR_x2, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
468+
{MP_QSTR_y2, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
469+
{MP_QSTR_x3, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
470+
{MP_QSTR_y3, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
471+
{MP_QSTR_x4, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
472+
{MP_QSTR_y4, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
473+
};
474+
475+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
476+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
477+
478+
int16_t x1 = args[ARG_x1].u_int;
479+
int16_t y1 = args[ARG_y1].u_int;
480+
int16_t x2 = args[ARG_x2].u_int;
481+
int16_t y2 = args[ARG_y2].u_int;
482+
int16_t x3 = args[ARG_x3].u_int;
483+
int16_t y3 = args[ARG_y3].u_int;
484+
int16_t x4 = args[ARG_x4].u_int;
485+
int16_t y4 = args[ARG_y4].u_int;
486+
487+
bool result = common_hal_vectorio_line_line_intersects(x1, y1, x2, y2, x3, y3, x4, y4);
488+
489+
if (result) {
490+
return mp_const_true;
491+
} else {
492+
return mp_const_false;
493+
}
494+
}
495+
MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_line_line_intersects_obj, 0, vectorio_line_line_intersects);
496+
497+
498+
//| def line_rectangle_intersects(
499+
//| x1: int, y1: int, x2: int, y2: int,
500+
//| rx: int, ry: int, rw: int, rh: int
501+
//| ) -> bool:
502+
//| """Checks whether a line intersects with another line
503+
//|
504+
//| :param int x1: Line x1 coordinate
505+
//| :param int y1: Line y1 coordinate
506+
//| :param int x2: Line x2 coordinate
507+
//| :param int y2: Line y2 coordinate
508+
//| :param int rx: Rectangle x coordinate
509+
//| :param int ry: Rectangle y coordinate
510+
//| :param int rw: Rectangle width
511+
//| :param int rh: Rectangle height
512+
//| ...
513+
//|
514+
static mp_obj_t vectorio_line_rectangle_intersects(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
515+
enum {ARG_x1, ARG_y1, ARG_x2, ARG_y2, ARG_rx, ARG_ry, ARG_rw, ARG_rh };
516+
517+
static const mp_arg_t allowed_args[] = {
518+
{MP_QSTR_x1, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
519+
{MP_QSTR_y1, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
520+
{MP_QSTR_x2, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
521+
{MP_QSTR_y2, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
522+
{MP_QSTR_rx, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
523+
{MP_QSTR_ry, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
524+
{MP_QSTR_rw, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
525+
{MP_QSTR_rh, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
526+
527+
};
528+
529+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
530+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
531+
532+
int16_t x1 = args[ARG_x1].u_int;
533+
int16_t y1 = args[ARG_y1].u_int;
534+
int16_t x2 = args[ARG_x2].u_int;
535+
int16_t y2 = args[ARG_y2].u_int;
536+
int16_t rx = args[ARG_rx].u_int;
537+
int16_t ry = args[ARG_ry].u_int;
538+
int16_t rw = args[ARG_rw].u_int;
539+
int16_t rh = args[ARG_rh].u_int;
540+
541+
bool result = common_hal_vectorio_line_rectangle_intersects(x1, y1, x2, y2, rx, ry, rw, rh);
542+
543+
if (result) {
544+
return mp_const_true;
545+
} else {
546+
return mp_const_false;
547+
}
548+
}
549+
MP_DEFINE_CONST_FUN_OBJ_KW(vectorio_line_rectangle_intersects_obj, 0, vectorio_line_rectangle_intersects);
550+
551+
445552
static const mp_rom_map_elem_t vectorio_module_globals_table[] = {
446553
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_vectorio) },
447554
{ MP_ROM_QSTR(MP_QSTR_circle_rectangle_intersects), MP_ROM_PTR(&vectorio_circle_rectangle_intersects_obj) },
448555
{ MP_ROM_QSTR(MP_QSTR_polygon_circle_intersects), MP_ROM_PTR(&vectorio_polygon_circle_intersects_obj) },
449556
{ MP_ROM_QSTR(MP_QSTR_circle_circle_intersects), MP_ROM_PTR(&vectorio_circle_circle_intersects_obj) },
450557
{ MP_ROM_QSTR(MP_QSTR_line_circle_intersects), MP_ROM_PTR(&vectorio_line_circle_intersects_obj) },
558+
{ MP_ROM_QSTR(MP_QSTR_line_line_intersects), MP_ROM_PTR(&vectorio_line_line_intersects_obj) },
559+
{ MP_ROM_QSTR(MP_QSTR_line_rectangle_intersects), MP_ROM_PTR(&vectorio_line_rectangle_intersects_obj) },
451560
{ MP_ROM_QSTR(MP_QSTR_circle_contains_point), MP_ROM_PTR(&vectorio_circle_contains_point_obj) },
452561
{ MP_ROM_QSTR(MP_QSTR_rectangle_contains_point), MP_ROM_PTR(&vectorio_rectangle_contains_point_obj) },
453562
{ MP_ROM_QSTR(MP_QSTR_line_contains_point), MP_ROM_PTR(&vectorio_line_contains_point_obj) },

shared-bindings/vectorio/__init__.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,13 @@ bool common_hal_vectorio_line_circle_intersects(
7676
bool common_hal_vectorio_polygon_circle_intersects(
7777
mp_obj_t points_list, int16_t polygon_x, int16_t polygon_y,
7878
int16_t cx, int16_t cy, int16_t cr, mp_float_t padding);
79+
80+
bool common_hal_vectorio_line_line_intersects(
81+
int16_t x1, int16_t y1, int16_t x2, int16_t y2,
82+
int16_t x3, int16_t y3, int16_t x4, int16_t y4
83+
);
84+
85+
bool common_hal_vectorio_line_rectangle_intersects(
86+
int16_t x1, int16_t y1, int16_t x2, int16_t y2,
87+
int16_t rx, int16_t ry, int16_t rw, int16_t rh
88+
);

shared-module/vectorio/__init__.c

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ bool common_hal_vectorio_polygon_circle_intersects(
167167
size_t len = 0;
168168
mp_obj_t *points_list_items;
169169
mp_obj_list_get(points_list, &len, &points_list_items);
170-
170+
bool polygon_contains_point = false;
171171
for(uint16_t i = 0; i < len; i++){
172172
size_t cur_tuple_len = 0;
173173
mp_obj_t *cur_point_tuple;
@@ -195,8 +195,75 @@ bool common_hal_vectorio_polygon_circle_intersects(
195195
return true;
196196
}
197197

198+
if (((cur_y >= cy && cy > next_y) || (cur_y < cy && cy <= next_y)) &&
199+
(cx < (next_x - cur_x) * (cy - cur_y) / (next_y - cur_y) + cur_x)){
198200

199-
}
201+
polygon_contains_point = !polygon_contains_point;
202+
}
200203

204+
}
205+
if (polygon_contains_point == true){
206+
return true;
207+
}
201208
return false;
209+
}
210+
211+
212+
213+
bool common_hal_vectorio_line_line_intersects(
214+
int16_t x1, int16_t y1, int16_t x2, int16_t y2,
215+
int16_t x3, int16_t y3, int16_t x4, int16_t y4
216+
){
217+
218+
double denom = ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1));
219+
220+
if (denom >= 0 && denom <= 0){
221+
return false;
222+
}
223+
224+
double dx1 = x1;
225+
double dy1 = y1;
226+
double dx2 = x2;
227+
double dy2 = y2;
228+
double dx3 = x3;
229+
double dy3 = y3;
230+
double dx4 = x4;
231+
double dy4 = y4;
232+
233+
234+
double dist_a = ((dx4 - dx3) * (dy1 - dy3) - (dy4 - dy3) * (dx1 - dx3)) / denom;
235+
double dist_b = ((dx2 - dx1) * (dy1 - dy3) - (dy2 - dy1) * (dx1 - dx3)) / denom;
236+
237+
if ((0 <= dist_a) && (dist_a <= 1) && (0 <= dist_b) && (dist_b <= 1)){
238+
return true;
239+
}
240+
return false;
241+
242+
}
243+
244+
bool common_hal_vectorio_line_rectangle_intersects(
245+
int16_t x1, int16_t y1, int16_t x2, int16_t y2,
246+
int16_t rx, int16_t ry, int16_t rw, int16_t rh
247+
){
248+
249+
250+
if (common_hal_vectorio_line_line_intersects(x1, y1, x2, y2,
251+
rx, ry, rx, ry + rh)){
252+
return true;
253+
}
254+
if (common_hal_vectorio_line_line_intersects(x1, y1, x2, y2,
255+
rx + rw, ry, rx + rw, ry + rh)){
256+
return true;
257+
}
258+
if (common_hal_vectorio_line_line_intersects(x1, y1, x2, y2,
259+
rx, ry, rx + rw, ry)){
260+
return true;
261+
}
262+
if (common_hal_vectorio_line_line_intersects(x1, y1, x2, y2,
263+
rx, ry + rh, rx + rw, ry + rh)){
264+
return true;
265+
}
266+
267+
return false;
268+
202269
}

0 commit comments

Comments
 (0)