diff --git a/src/lv_draw/lv_draw_triangle.c b/src/lv_draw/lv_draw_triangle.c index 08bcf6af2..b9724f49a 100644 --- a/src/lv_draw/lv_draw_triangle.c +++ b/src/lv_draw/lv_draw_triangle.c @@ -94,16 +94,28 @@ void lv_draw_polygon(const lv_point_t points[], uint16_t point_cnt, const lv_are int32_t i_next_right; uint32_t mask_cnt = 0; - /* Check if the order of points is inverted or not. - * The normal case is when the left point is on `y_min_i - 1`*/ + /*Get the index of the left and right points*/ i_next_left = y_min_i - 1; if(i_next_left < 0) i_next_left = point_cnt + i_next_left; i_next_right = y_min_i + 1; if(i_next_right > point_cnt - 1) i_next_right = 0; + + /* Check if the order of points is inverted or not. + * The normal case is when the left point is on `y_min_i - 1` + * Explanation: + * if angle(p_left) < angle(p_right) -> inverted + * dy_left/dx_left < dy_right/dx_right + * dy_left * dx_right < dy_right * dx_left + */ + lv_coord_t dxl = points[i_next_left].x - points[y_min_i].x; + lv_coord_t dxr = points[i_next_right].x - points[y_min_i].x; + lv_coord_t dyl = points[i_next_left].y - points[y_min_i].y; + lv_coord_t dyr = points[i_next_right].y - points[y_min_i].y; + bool inv = false; - if(points[i_next_left].x > points[i_next_right].x && points[i_next_left].y < points[i_next_right].y) inv = true; + if(dyl*dxr < dyr*dxl) inv = true; do { if(!inv) {