EFR32 Blue Gecko 1 Software Documentation  efr32bg1-doc-5.1.2
glib_circle.c
Go to the documentation of this file.
1  /*************************************************************************/
16 /* Standard C header files */
17 #include <stdint.h>
18 /* EM types */
19 #include "em_types.h"
20 
21 /* GLIB header files */
22 #include "glib.h"
23 
24 /* Local function prototypes */
25 static EMSTATUS GLIB_drawPartialCirclePoints(GLIB_Context_t *pContext,
26  int32_t xCenter, int32_t yCenter,
27  int32_t x, int32_t y, uint8_t bitMask);
28 
29 /**************************************************************************/
51 EMSTATUS GLIB_drawCircle(GLIB_Context_t *pContext, int32_t xCenter,
52  int32_t yCenter, uint32_t radius)
53 {
54  return GLIB_drawPartialCircle(pContext, xCenter, yCenter, radius, 0xFF);
55 }
56 
57 /**************************************************************************/
84 EMSTATUS GLIB_drawPartialCircle(GLIB_Context_t *pContext, int32_t xCenter,
85  int32_t yCenter, uint32_t radius, uint8_t bitMask)
86 {
87  EMSTATUS status;
88  int32_t x = 0;
89  int32_t y = radius;
90  int32_t d = 1 - radius;
91  uint32_t drawnElements = 0;
92 
93  /* Check arguments */
94  if (pContext == NULL) return GLIB_ERROR_INVALID_ARGUMENT;
95 
96  /* Draw initial circle points */
97  status = GLIB_drawPartialCirclePoints(pContext, xCenter, yCenter, x, y, bitMask);
98  if (status > GLIB_ERROR_NOTHING_TO_DRAW) return status;
99  if (status == GLIB_OK) drawnElements++;
100 
101  /* Loops through all points from 0 to 45 degrees (1 octant) of the circle
102  * (0 is defined straight upward) */
103  while (x < y) {
104  if (d < 0) {
105  d += 2 * x + 1;
106  } else {
107  y--;
108  d += 2 * (x - y) + 1;
109  }
110 
111  /* Draws the circle points using 8-way symmetry */
112  status = GLIB_drawPartialCirclePoints(pContext, xCenter, yCenter, x, y, bitMask);
113  x++;
114  if (status > GLIB_ERROR_NOTHING_TO_DRAW) return status;
115  if (status == GLIB_OK) drawnElements++;
116  }
117  return ((drawnElements == 0) ? GLIB_ERROR_NOTHING_TO_DRAW : GLIB_OK);
118 }
119 
120 /**************************************************************************/
142 EMSTATUS GLIB_drawCircleFilled(GLIB_Context_t *pContext, int32_t xCenter,
143  int32_t yCenter, uint32_t radius)
144 {
145  EMSTATUS status;
146  int32_t x = 0;
147  int32_t y = radius;
148  int32_t d = 1 - radius;
149  uint32_t drawnElements = 0;
150 
151  /* Check arguments */
152  if (pContext == NULL) return GLIB_ERROR_INVALID_ARGUMENT;
153 
154  /* Draws the initial circle fill line */
155  status = GLIB_drawLineH(pContext, xCenter - y, yCenter, xCenter + y);
156  if (status > GLIB_ERROR_NOTHING_TO_DRAW) return status;
157  if (status == GLIB_OK) drawnElements++;
158 
159  /* Loops through all points from 0 to 45 degrees of the circle
160  * (0 is defined straight upward) */
161  while (x < y) {
162  if (d < 0) {
163  d += 2 * x + 1;
164  } else {
165  y--;
166  d += 2 * (x - y) + 1;
167  }
168 
169  /* Draws horizontal fill lines using 4 way symmetry */
170  status = GLIB_drawLineH(pContext, xCenter - x, yCenter + y, xCenter + x);
171  if (status > GLIB_ERROR_NOTHING_TO_DRAW) return status;
172  if (status == GLIB_OK) drawnElements++;
173 
174  status = GLIB_drawLineH(pContext, xCenter - y, yCenter + x, xCenter + y);
175  if (status > GLIB_ERROR_NOTHING_TO_DRAW) return status;
176  if (status == GLIB_OK) drawnElements++;
177 
178  status = GLIB_drawLineH(pContext, xCenter - x, yCenter - y, xCenter + x);
179  if (status > GLIB_ERROR_NOTHING_TO_DRAW) return status;
180  if (status == GLIB_OK) drawnElements++;
181 
182  status = GLIB_drawLineH(pContext, xCenter - y, yCenter - x, xCenter + y);
183  if (status > GLIB_ERROR_NOTHING_TO_DRAW) return status;
184  if (status == GLIB_OK) drawnElements++;
185 
186  x++;
187  }
188  return ((drawnElements == 0) ? GLIB_ERROR_NOTHING_TO_DRAW : GLIB_OK);
189 }
190 
191 /**************************************************************************/
219  int32_t xCenter, int32_t yCenter,
220  int32_t x, int32_t y, uint8_t bitMask)
221 {
222  EMSTATUS status;
223  uint32_t drawnElements = 0;
224  uint32_t i;
225 
226  /* Draw the circle points using 8-way symmetry */
227  int32_t xOffsets[] = {y, x, -x, -y, -y, -x, x, y};
228  int32_t yOffsets[] = {-x, -y, -y, -x, x, y, y, x};
229 
230  i = 0;
231  while (bitMask)
232  {
233  /* Pixel in i+1. octant */
234  if (bitMask & 0x1)
235  {
236  status = GLIB_drawPixel(pContext, xCenter + xOffsets[i], yCenter + yOffsets[i]);
237  if (status > GLIB_ERROR_NOTHING_TO_DRAW) return status;
238  if (status == GLIB_OK) drawnElements++;
239  }
240  bitMask >>= 1;
241  i++;
242  }
243  return ((drawnElements == 0) ? GLIB_ERROR_NOTHING_TO_DRAW : GLIB_OK);
244 }
245 
EMSTATUS GLIB_drawCircle(GLIB_Context_t *pContext, int32_t xCenter, int32_t yCenter, uint32_t radius)
Draws a circle with center at x, y, and a radius.
Definition: glib_circle.c:51
#define GLIB_ERROR_NOTHING_TO_DRAW
Definition: glib.h:192
static EMSTATUS GLIB_drawPartialCirclePoints(GLIB_Context_t *pContext, int32_t xCenter, int32_t yCenter, int32_t x, int32_t y, uint8_t bitMask)
Draws circle points using 8-way symmetry.
Definition: glib_circle.c:218
EMSTATUS GLIB_drawPartialCircle(GLIB_Context_t *pContext, int32_t xCenter, int32_t yCenter, uint32_t radius, uint8_t bitMask)
Draws a partial circle with center at x, y, and a radius.
Definition: glib_circle.c:84
#define GLIB_ERROR_INVALID_ARGUMENT
Definition: glib.h:200
Silicon Labs Graphics Library.
EMSTATUS GLIB_drawCircleFilled(GLIB_Context_t *pContext, int32_t xCenter, int32_t yCenter, uint32_t radius)
Draws a filled circle with center at x, y, and a radius.
Definition: glib_circle.c:142
#define GLIB_OK
Definition: glib.h:190
EMSTATUS GLIB_drawLineH(GLIB_Context_t *pContext, int32_t x1, int32_t y1, int32_t x2)
Draws a horizontal line from x1, y1 to x2, y2.
Definition: glib_line.c:49
EMSTATUS GLIB_drawPixel(GLIB_Context_t *pContext, int32_t x, int32_t y)
Draws a pixel at x, y using foregroundColor defined in the GLIB_Context_t.
Definition: glib.c:350
GLIB Drawing Context (Multiple instances of GLIB_Context_t can exist)
Definition: glib.h:273