2018-06-07 15:32:19 +02:00
|
|
|
/**
|
|
|
|
* @file lv_draw_triangle.c
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*********************
|
|
|
|
* INCLUDES
|
|
|
|
*********************/
|
|
|
|
#include "lv_draw_triangle.h"
|
|
|
|
#include "../lv_misc/lv_math.h"
|
|
|
|
|
|
|
|
/*********************
|
|
|
|
* DEFINES
|
|
|
|
*********************/
|
2019-05-31 07:51:18 +02:00
|
|
|
|
2018-06-07 15:32:19 +02:00
|
|
|
/**********************
|
|
|
|
* TYPEDEFS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC PROTOTYPES
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC VARIABLES
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* MACROS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* GLOBAL FUNCTIONS
|
|
|
|
**********************/
|
2019-03-18 16:16:52 +01:00
|
|
|
|
2018-06-07 15:32:19 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param points pointer to an array with 3 points
|
2019-11-13 16:44:20 +01:00
|
|
|
* @param clip_area the triangle will be drawn only in this area
|
2019-03-18 16:16:52 +01:00
|
|
|
* @param style style for of the triangle
|
2019-06-02 20:35:33 +02:00
|
|
|
* @param opa_scale scale down all opacities by the factor (0..255)
|
2018-06-07 15:32:19 +02:00
|
|
|
*/
|
2019-11-13 16:44:20 +01:00
|
|
|
void lv_draw_triangle(const lv_point_t * points, const lv_area_t * clip_area, const lv_style_t * style, lv_opa_t opa_scale)
|
2019-04-17 15:14:09 +02:00
|
|
|
{
|
2019-11-13 16:44:20 +01:00
|
|
|
lv_draw_polygon(points, 3, clip_area, style, opa_scale);
|
2019-04-17 15:14:09 +02:00
|
|
|
}
|
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
/**
|
2019-11-13 16:44:20 +01:00
|
|
|
* Draw a polygon. Only convex polygons are supported
|
2019-06-02 20:35:33 +02:00
|
|
|
* @param points an array of points
|
|
|
|
* @param point_cnt number of points
|
2019-11-13 16:44:20 +01:00
|
|
|
* @param clip_area polygon will be drawn only in this area
|
2019-06-02 20:35:33 +02:00
|
|
|
* @param style style of the polygon
|
|
|
|
* @param opa_scale scale down all opacities by the factor (0..255)
|
|
|
|
*/
|
2019-11-13 16:44:20 +01:00
|
|
|
void lv_draw_polygon(const lv_point_t * points, uint16_t point_cnt, const lv_area_t * clip_area, const lv_style_t * style,
|
2019-06-06 06:05:40 +02:00
|
|
|
lv_opa_t opa_scale)
|
2019-06-02 20:35:33 +02:00
|
|
|
{
|
2019-10-16 20:38:45 +02:00
|
|
|
if(point_cnt < 3) return;
|
|
|
|
if(points == NULL) return;
|
|
|
|
|
2019-10-28 15:03:11 +01:00
|
|
|
int16_t i;
|
|
|
|
lv_area_t poly_mask = {.x1 = LV_COORD_MAX, .y1 = LV_COORD_MAX, .x2 = LV_COORD_MIN, .y2 = LV_COORD_MIN};
|
|
|
|
|
|
|
|
for(i = 0; i < point_cnt; i++) {
|
|
|
|
poly_mask.x1 = LV_MATH_MIN(poly_mask.x1, points[i].x);
|
|
|
|
poly_mask.y1 = LV_MATH_MIN(poly_mask.y1, points[i].y);
|
|
|
|
poly_mask.x2 = LV_MATH_MAX(poly_mask.x2, points[i].x);
|
|
|
|
poly_mask.y2 = LV_MATH_MAX(poly_mask.y2, points[i].y);
|
2019-10-16 20:38:45 +02:00
|
|
|
}
|
2019-10-28 15:03:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
bool is_common;
|
2019-11-13 16:44:20 +01:00
|
|
|
is_common = lv_area_intersect(&poly_mask, &poly_mask, clip_area);
|
2019-10-28 15:03:11 +01:00
|
|
|
if(!is_common) return;
|
|
|
|
|
|
|
|
/*Find the lowest point*/
|
|
|
|
lv_coord_t y_min = points[0].y;
|
|
|
|
int16_t y_min_i = 0;
|
|
|
|
|
|
|
|
for(i = 1; i < point_cnt; i++) {
|
|
|
|
if(points[i].y < y_min) {
|
|
|
|
y_min = points[i].y;
|
|
|
|
y_min_i = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 06:25:14 +01:00
|
|
|
lv_draw_mask_line_param_t * mp = lv_mem_buf_get(sizeof(lv_draw_mask_line_param_t) * point_cnt);
|
2019-11-14 07:16:24 +01:00
|
|
|
lv_draw_mask_line_param_t * mp_next = mp;
|
2019-10-28 15:03:11 +01:00
|
|
|
|
|
|
|
int32_t i_prev_left = y_min_i;
|
|
|
|
int32_t i_prev_right = y_min_i;
|
|
|
|
int32_t i_next_left;
|
|
|
|
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`*/
|
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if(!inv) {
|
|
|
|
i_next_left = i_prev_left - 1;
|
|
|
|
if(i_next_left < 0) i_next_left = point_cnt + i_next_left;
|
|
|
|
|
|
|
|
i_next_right = i_prev_right + 1;
|
|
|
|
if(i_next_right > point_cnt - 1) i_next_right = 0;
|
|
|
|
} else {
|
|
|
|
i_next_left = i_prev_left + 1;
|
|
|
|
if(i_next_left > point_cnt - 1) i_next_left = 0;
|
|
|
|
|
|
|
|
i_next_right = i_prev_right - 1;
|
|
|
|
if(i_next_right < 0) i_next_right = point_cnt + i_next_right;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(points[i_next_left].y >= points[i_prev_left].y) {
|
|
|
|
if(points[i_next_left].y != points[i_prev_left].y &&
|
|
|
|
points[i_next_left].x != points[i_prev_left].x) {
|
|
|
|
lv_draw_mask_line_points_init(mp_next, points[i_prev_left].x, points[i_prev_left].y,
|
|
|
|
points[i_next_left].x, points[i_next_left].y,
|
|
|
|
LV_DRAW_MASK_LINE_SIDE_RIGHT);
|
|
|
|
lv_draw_mask_add(mp_next, mp);
|
|
|
|
mp_next++;
|
|
|
|
}
|
|
|
|
mask_cnt++;
|
|
|
|
i_prev_left = i_next_left;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mask_cnt == point_cnt) break;
|
|
|
|
|
|
|
|
if(points[i_next_right].y >= points[i_prev_right].y) {
|
|
|
|
if(points[i_next_right].y != points[i_prev_right].y &&
|
|
|
|
points[i_next_right].x != points[i_prev_right].x) {
|
|
|
|
|
|
|
|
lv_draw_mask_line_points_init(mp_next, points[i_prev_right].x, points[i_prev_right].y,
|
|
|
|
points[i_next_right].x, points[i_next_right].y,
|
|
|
|
LV_DRAW_MASK_LINE_SIDE_LEFT);
|
|
|
|
lv_draw_mask_add(mp_next, mp);
|
|
|
|
mp_next++;
|
|
|
|
}
|
|
|
|
mask_cnt++;
|
|
|
|
i_prev_right = i_next_right;
|
|
|
|
}
|
|
|
|
|
|
|
|
}while( mask_cnt < point_cnt);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lv_draw_rect(&poly_mask, &poly_mask, style, opa_scale);
|
|
|
|
|
|
|
|
lv_draw_mask_remove_custom(mp);
|
|
|
|
|
2019-11-19 06:25:14 +01:00
|
|
|
lv_mem_buf_release(mp);
|
2019-10-28 15:03:11 +01:00
|
|
|
|
2019-06-02 20:35:33 +02:00
|
|
|
}
|
|
|
|
|
2019-04-17 15:14:09 +02:00
|
|
|
/**********************
|
|
|
|
* STATIC FUNCTIONS
|
|
|
|
**********************/
|