EFR32 Mighty Gecko 13 Software Documentation  efr32mg13-doc-5.1.2
glib_polygon.c
Go to the documentation of this file.
1  /*************************************************************************/
16 /* C-header files */
17 #include <stdint.h>
18 
19 /* EM types */
20 #include "em_types.h"
21 
22 /* GLIB header files */
23 #include "glib.h"
24 
25 /**************************************************************************/
46 EMSTATUS GLIB_drawPolygon(GLIB_Context_t *pContext,
47  uint32_t numPoints, const int32_t *polyPoints)
48 {
49  EMSTATUS status;
50  uint32_t drawnElements = 0;
51  uint32_t point;
52  int32_t firstX;
53  int32_t firstY;
54  int32_t startX;
55  int32_t startY;
56  int32_t endX;
57  int32_t endY;
58 
59  /* Check arguments */
60  if (pContext == NULL || polyPoints == NULL || numPoints < 2) return GLIB_ERROR_INVALID_ARGUMENT;
61 
62  startX = *polyPoints++;
63  startY = *polyPoints++;
64  firstX = startX;
65  firstY = startY;
66 
67  /* Loop through the points by moving the pointer */
68  for (point = 1; point < numPoints; point++)
69  {
70  endX = *polyPoints++;
71  endY = *polyPoints++;
72 
73  /* Draw a line between each pair of points */
74  status = GLIB_drawLine(pContext, startX, startY, endX, endY);
75  if (status > GLIB_ERROR_NOTHING_TO_DRAW) return status;
76  if (status == GLIB_OK) drawnElements++;
77 
78  startX = endX;
79  startY = endY;
80  }
81 
82  /* Draw a line from last point to first point */
83  if ((endX != firstX) || (endY != firstY))
84  {
85  status = GLIB_drawLine(pContext, firstX, firstY, endX, endY);
86  if (status > GLIB_ERROR_NOTHING_TO_DRAW) return status;
87  if (status == GLIB_OK) drawnElements++;
88  }
89  return ((drawnElements == 0) ? GLIB_ERROR_NOTHING_TO_DRAW : GLIB_OK);
90 }
EMSTATUS GLIB_drawPolygon(GLIB_Context_t *pContext, uint32_t numPoints, const int32_t *polyPoints)
Draws a polygon using Bresnham's Midpoint Line Algorithm.
Definition: glib_polygon.c:46
#define GLIB_ERROR_NOTHING_TO_DRAW
Definition: glib.h:192
#define GLIB_ERROR_INVALID_ARGUMENT
Definition: glib.h:200
Silicon Labs Graphics Library.
#define GLIB_OK
Definition: glib.h:190
EMSTATUS GLIB_drawLine(GLIB_Context_t *pContext, int32_t x1, int32_t y1, int32_t x2, int32_t y2)
Draws a line from x1,y1 to x2, y2.
Definition: glib_line.c:303
GLIB Drawing Context (Multiple instances of GLIB_Context_t can exist)
Definition: glib.h:273