sGUI  V1.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
geometry.c
Go to the documentation of this file.
1 
75 #ifdef __cplusplus
76 extern "C" {
77 #endif /* __cplusplus */
78 
79 /*----- Header-Files -------------------------------------------------------*/
80 #include <stdlib.h> /* Common types and functions */
81 #include <stdint.h> /* Standard integer formats */
82 #include "lcd.h" /* Main functionality and lld */
83 
84 /*----- Macros -------------------------------------------------------------*/
85 
86 /*----- Data types ---------------------------------------------------------*/
87 
88 /*----- Function prototypes ------------------------------------------------*/
89 
90 /*----- Data ---------------------------------------------------------------*/
91 
92 /*----- Implementation -----------------------------------------------------*/
103 void LCD_DrawLine(uint16_t Xstart, uint16_t Ystart, uint16_t Xstop,
104  uint16_t Ystop, LCDCOLOR Color) {
105 
106  int16_t dX = 0, dY = 0;
107  int16_t sX, sY, err, e2;
108 
109  if (Xstop > Xstart) {
110  dX = Xstop - Xstart;
111  }
112  else {
113  dX = Xstart - Xstop;
114  }
115  if (Ystop > Ystart) {
116  dY = Ystop - Ystart;
117  }
118  else {
119  dY = Ystart - Ystop;
120  }
121 
122  if (Xstart < Xstop) {
123  sX = 1;
124  }
125  else {
126  sX = -1;
127  }
128  if (Ystart < Ystop) {
129  sY = 1;
130  }
131  else {
132  sY = -1;
133  }
134  err = dX - dY;
135 
136  while (1) {
137  LCD_WritePixel(Xstart, Ystart, Color);
138  if (Xstart == Xstop && Ystart == Ystop) {
139  break;
140  }
141  e2 = 2 * err;
142  if (e2 > -dY) {
143  err = err - dY;
144  Xstart = Xstart + sX;
145  }
146  if (e2 < dX) {
147  err = err + dX;
148  Ystart = Ystart + sY;
149  }
150  }
151 }
152 
161 void LCD_DrawRect(uint16_t Xpos, uint16_t Ypos, uint16_t width,
162  uint16_t height, LCDCOLOR Color) {
163 
164  LCD_DrawLine(Xpos, Ypos, Xpos + width, Ypos, Color);
165  LCD_DrawLine(Xpos, Ypos, Xpos, Ypos + height, Color);
166  LCD_DrawLine(Xpos + width, Ypos, Xpos + width, Ypos + height, Color);
167  LCD_DrawLine(Xpos, Ypos + height, Xpos + width, Ypos + height, Color);
168 }
169 
178 void LCD_DrawRectF(uint16_t Xpos, uint16_t Ypos, uint16_t width,
179  uint16_t height, LCDCOLOR Color) {
180 
181  LCD_FillArea(Xpos, Ypos, Xpos + width, Ypos + height, Color);
182 }
183 
191 void LCD_DrawCircle(uint16_t Xpos, uint16_t Ypos, uint16_t Radius,
192  LCDCOLOR Color) {
193 
194  int16_t x, y;
195 
196  for (x = -Radius; x <= Radius; x++) {
197  for (y = -Radius; y <= Radius; y++) {
198  if (abs(x * x + y * y - Radius * Radius) <= Radius) {
199  LCD_WritePixel(x + Xpos, y + Ypos, Color);
200 }
201  }
202  }
203 }
204 
212 void LCD_DrawCircleF(uint8_t Xpos, uint16_t Ypos, uint16_t Radius,
213  LCDCOLOR Color) {
214 
215  int16_t x, y;
216  int16_t radError;
217 
218  x = Radius;
219  y = 0;
220  radError = 1 - x;
221 
222  while (x >= y) {
223 
224  LCD_DrawLine(x + Xpos, y + Ypos, -x + Xpos, y + Ypos, Color);
225  LCD_DrawLine(y + Xpos, x + Ypos, -y + Xpos, x + Ypos, Color);
226  LCD_DrawLine(-x + Xpos, -y + Ypos, x + Xpos, -y + Ypos, Color);
227  LCD_DrawLine(-y + Xpos, -x + Ypos, y + Xpos, -x + Ypos, Color);
228 
229  y++;
230  if (radError < 0) {
231  radError += 2 * y + 1;
232  }
233  else {
234  x--;
235  radError += 2 * (y - x + 1);
236  }
237  }
238 }
239 
240 /*----- EOF ----------------------------------------------------------------*/
241 
242 #ifdef __cplusplus
243 }
244 #endif /* __cplusplus */
245 
static void LCD_WritePixel(uint16_t x, uint16_t y, uint16_t pixel)
Write one pixel to the display.
Definition: lcd_lld.h:129
void LCD_DrawRect(uint16_t Xpos, uint16_t Ypos, uint16_t width, uint16_t height, LCDCOLOR Color)
Displays a rectangle.
Definition: geometry.c:161
void LCD_DrawCircleF(uint8_t Xpos, uint16_t Ypos, uint16_t Radius, LCDCOLOR Color)
Displays a filled circle.
Definition: geometry.c:212
void LCD_DrawRectF(uint16_t Xpos, uint16_t Ypos, uint16_t width, uint16_t height, LCDCOLOR Color)
Displays a filled rectangle.
Definition: geometry.c:178
Simple graphic library main functionality.
uint16_t LCDCOLOR
Data type for a color definition.
Definition: color.h:98
void LCD_DrawCircle(uint16_t Xpos, uint16_t Ypos, uint16_t Radius, LCDCOLOR Color)
Displays a circle.
Definition: geometry.c:191
static void LCD_FillArea(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color)
Fill a specified area on the display with the same color.
Definition: lcd_lld.h:104
void LCD_DrawLine(uint16_t Xstart, uint16_t Ystart, uint16_t Xstop, uint16_t Ystop, LCDCOLOR Color)
Draw a line on the display starting at (Xstart/Ystart) and ending at (Xstop/Ystop). This function draws the line with the Bresenham's line algorithm.
Definition: geometry.c:103