Sensorian  1.0
C API Reference Guide Library
GFX.c
Go to the documentation of this file.
1 /****************************************************************************
2  * Copyright (C) 2015 Sensorian
3  * *
4  * This file is part of Sensorian. *
5  * *
6  * Sensorian is free software: you can redistribute it and/or modify it *
7  * under the terms of the GNU Lesser General Public License as published *
8  * by the Free Software Foundation, either version 3 of the License, or *
9  * (at your option) any later version. *
10  * *
11  * Sensorian is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with Sensorian. *
18  * If not, see <http://www.gnu.org/licenses/>. *
19  ****************************************************************************/
20 
28 #include "GFX.h"
29 #include <stdio.h>
30 #include <math.h>
31 
40 void TFT_HorizontalLine(unsigned int Xaxis, unsigned int Yaxis,unsigned int width ,unsigned int color)
41 {
42  TFT_FullRectangle(Xaxis,Yaxis,Xaxis+width,Yaxis,color);
43 }
44 
53 void TFT_VerticalLine(unsigned int Xaxis,unsigned int Yaxis, unsigned int height ,unsigned int color)
54 {
55  TFT_FullRectangle(Xaxis,Yaxis,Xaxis,Yaxis+height,color);
56 }
57 
67 void TFT_Line(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, unsigned int color)
68 {
69  unsigned int deltax, deltay, x,y;
70  unsigned char steep;
71  int lerror, ystep;
72 
73  steep = absDiff(y1,y2) > absDiff(x1,x2); //check slope
74 
75  if (steep)
76  {
77  swap(x1, y1);
78  swap(x2, y2);
79  }
80 
81  if (x1 > x2)
82  {
83  swap(x1, x2);
84  swap(y1, y2);
85  }
86 
87  deltax = x2 - x1;
88  deltay = absDiff(y2,y1);
89  lerror = deltax / 2;
90  y = y1;
91  if(y1 < y2) ystep = 1; else ystep = -1;
92 
93  for(x = x1; x <= x2; x++)
94  {
95  if (steep) TFT_SetPixel(y,x, color); else TFT_SetPixel(x,y, color);
96  lerror -= deltay;
97  if (lerror < 0){
98  y = y + ystep;
99  lerror += deltax;
100  }
101  }
102 }
103 
109 double inline TFT_dfloor( double value )
110 {
111  if (value < 0.0)
112  return ceil( value );
113  else
114  return floor( value );
115 }
116 
126 void TFT_SlantyLine(unsigned int lX1, unsigned int lY1, unsigned int lX2,unsigned int lY2,unsigned int color)
127 {
128  long lError, lDeltaX, lDeltaY, lYStep, bSteep;
129 
130  // A steep line has a bigger ordinate.
131 
132  if(((lY2 > lY1) ? (lY2 - lY1) : (lY1 - lY2)) > ((lX2 > lX1) ? (lX2 - lX1) : (lX1 - lX2))){
133  bSteep = 1;
134  }else {
135  bSteep = 0;
136  }
137 
138  // If line is steep, swap the X and Y coordinates.
139  if(bSteep){
140  lError = lX1;
141  lX1 = lY1;
142  lY1 = lError;
143  lError = lX2;
144  lX2 = lY2;
145  lY2 = lError;
146  }
147 
148  // If the starting X coordinate is larger than the ending X coordinate,
149  // swap the start and end coordinates.
150  if(lX1 > lX2){
151  lError = lX1;
152  lX1 = lX2;
153  lX2 = lError;
154  lError = lY1;
155  lY1 = lY2;
156  lY2 = lError;
157  }
158 
159  lDeltaX = lX2 - lX1; // Compute the difference between the start and end coordinates.
160  lDeltaY = (lY2 > lY1) ? (lY2 - lY1) : (lY1 - lY2);
161 
162  lError = -lDeltaX / 2; // Initialize the error term to negative half the X delta.
163 
164  if(lY1 < lY2){ // Determine the direction to step in the Y axis when required.
165  lYStep = 1;
166  }else{
167  lYStep = -1;
168  }
169 
170  for(; lX1 <= lX2; lX1++)
171  { // Loop through all the points along the X axis of the line.
172 
173  // See if this is a steep line.
174 
175  if(bSteep){
176  // Plot this point of the line, swapping the X and Y coordinates.
177  TFT_SetPixel(lY1, lX1,color);
178  }
179  else { // Plot this point of the line, using the coordinates as is.
180  TFT_SetPixel(lX1, lY1,color);
181  }
182 
183  // Increment the error term by the Y delta.
184 
185  lError += lDeltaY;
186 
187  if(lError > 0){ // See if the error term is now greater than zero.
188 
189  lY1 += lYStep; // Take a step in the Y axis.
190 
191  lError -= lDeltaX; // Decrement the error term by the X delta.
192  }
193  }
194 }
195 
205 void TFT_FullRectangle(unsigned int Xaxis1, unsigned int Yaxis1, unsigned int Xaxis2 ,unsigned int Yaxis2,unsigned int color)
206 {
207  unsigned int i = 0;
208  unsigned int j = 0;
209 
210  for(i=Xaxis1;i<= Xaxis2;i++)
211  {
212  for(j=Yaxis1;j<=Yaxis2;j++)
213  {
214  TFT_SetPixel(i,j,color);
215  }
216  }
217 }
218 
227 void TFT_EmptyRectangle(unsigned int Xaxis1,unsigned int Yaxis1, unsigned int Xaxis2,unsigned int Yaxis2,unsigned int color)
228 {
229  unsigned int CurrentValue;
230 
231  /* Draw the two horizontal lines */
232  for (CurrentValue = 0; CurrentValue < Xaxis2 - Xaxis1+ 1; CurrentValue++)
233  {
234  TFT_SetPixel(Xaxis1 + CurrentValue, Yaxis1,color);
235  TFT_SetPixel(Xaxis1 + CurrentValue, Yaxis2,color);
236  }
237 
238  /* draw the two vertical lines */
239  for (CurrentValue = 0; CurrentValue < Yaxis2 - Yaxis1 + 1; CurrentValue++)
240  {
241  TFT_SetPixel(Xaxis1, Yaxis1 + CurrentValue,color);
242  TFT_SetPixel(Xaxis2, Yaxis1 + CurrentValue,color);
243  }
244 }
245 
256 void TFT_RoundRectangle(unsigned int x, unsigned int y, unsigned int width, unsigned int height, unsigned int radius, unsigned int color)
257 {
258  int tSwitch, x1 = 0, y1 = radius;
259  tSwitch = 3 - 2 * radius;
260 
261  while (x1 <= y1) {
262  TFT_SetPixel(x+radius - x1, y+radius - y1, color);
263  TFT_SetPixel(x+radius - y1, y+radius - x1, color);
264 
265  TFT_SetPixel(x+width-radius + x1, y+radius - y1, color);
266  TFT_SetPixel(x+width-radius + y1, y+radius - x1, color);
267 
268  TFT_SetPixel(x+width-radius + x1, y+height-radius + y1, color);
269  TFT_SetPixel(x+width-radius + y1, y+height-radius + x1, color);
270 
271  TFT_SetPixel(x+radius - x1, y+height-radius + y1, color);
272  TFT_SetPixel(x+radius - y1, y+height-radius + x1, color);
273 
274  if (tSwitch < 0) {
275  tSwitch += (4 * x1 + 6);
276  } else {
277  tSwitch += (4 * (x1 - y1) + 10);
278  y1--;
279  }
280  x1++;
281  }
282 
283  TFT_HorizontalLine(x+radius,y, width-(2*radius), color); // top
284  TFT_HorizontalLine(x+radius,y+height, width-(2*radius), color); // bottom
285  TFT_VerticalLine(x,y+radius,height-(2*radius), color); // left
286  TFT_VerticalLine(x+width, y+radius,height-(2*radius), color); // right
287 }
288 
297 void TFT_Circle(unsigned int CenterX, unsigned int CenterY, unsigned int Radius,unsigned int color)
298 {
299  unsigned int y=0, x=0, d = 0;
300  int part;
301  d = CenterY - CenterX;
302  y = Radius;
303  part = 3 - 2 * Radius;
304 
305  while (x <= y) {
306  TFT_SetPixel(CenterX + x, CenterY + y,color);
307  TFT_SetPixel(CenterX + x, CenterY - y,color);
308  TFT_SetPixel(CenterX - x, CenterY + y,color);
309  TFT_SetPixel(CenterX - x, CenterY - y,color);
310  TFT_SetPixel(CenterY + y - d, CenterY + x,color);
311  TFT_SetPixel(CenterY + y - d, CenterY - x,color);
312  TFT_SetPixel(CenterY - y - d, CenterY + x,color);
313  TFT_SetPixel(CenterY - y - d, CenterY - x,color);
314 
315  if (part < 0) part += (4 * x + 6);
316  else {
317  part += (4 * (x - y) + 10);
318  y--;
319  }
320  x++;
321  }
322 }
323 
332  void TFT_Disk(unsigned int CenterX, unsigned int CenterY, unsigned int Radius,unsigned int color)
333  {
334  unsigned int i, x, yL, yU;
335 
336  //start at Xmiddle-Radius and end at Xmiddle+Radius
337  for (x = CenterX - Radius; x <= CenterX + Radius; x++)
338  {
339  yL = TFT_dfloor(CenterY - sqrt(Radius * Radius - (x - CenterX) * (x - CenterX)));
340  yU = TFT_dfloor(CenterY + sqrt(Radius * Radius - (x - CenterX) * (x - CenterX)));
341  //draw vertical lines between X|Yl and X|Yu
342  for (i = 0; i < (yU - yL); i++)
343  {
344  if (i <= yU && yL >= 0)
345  {
346  TFT_SetPixel(x, yL + i, color);
347  }
348  }
349  //draw the upper halve's pixel
350  if (yU < 80 && yU > 0)
351  {
352  TFT_SetPixel(x, yU, color);
353  }
354  //the lower pixel
355  if (yL < 80 && yL > 0)
356  {
357  TFT_SetPixel(x, yL, color);
358  }
359  }
360 }
361 
370 void TFT_DrawCircle(unsigned int CenterX, unsigned int CenterY, unsigned int Radius,unsigned int color)
371 {
372  unsigned int x, yL, yU;
373 
374  for (x = (CenterX - Radius); x <= CenterX + Radius; x++)
375  {
376  yL = CenterY - sqrt(Radius * Radius - (x - CenterX) * (x - CenterX));
377  yU = CenterY + sqrt(Radius * Radius - (x - CenterX) * (x - CenterX));
378 
379  if (yL < 80 && yL > 0)
380  {
381  TFT_SetPixel(x, yL, color);
382  }
383  if (yU < 80 && yU > 0)
384  {
385  TFT_SetPixel(x, yU, color);
386  }
387  }
388 }
389 
399 void TFT_Ellipse(long CX, long CY, long XRadius,long YRadius, unsigned int color)
400 {
401 
402  long X, Y;
403  long XChange, YChange;
404  long EllipseError;
405  long TwoASquare,TwoBSquare;
406  long StoppingX, StoppingY;
407  TwoASquare = 2*XRadius*XRadius;
408  TwoBSquare = 2*YRadius*YRadius;
409  X = XRadius;
410  Y = 0;
411  XChange = YRadius*YRadius*(1-2*XRadius);
412  YChange = XRadius*XRadius;
413  EllipseError = 0;
414  StoppingX = TwoBSquare*XRadius;
415  StoppingY = 0;
416 
417  while ( StoppingX >=StoppingY ) //first set of points,y'>-1
418  {
419  Plot4EllipsePoints(CX,CY,X,Y,color);
420  Y++;
421  StoppingY=StoppingY+ TwoASquare;
422  EllipseError = EllipseError+ YChange;
423  YChange=YChange+TwoASquare;
424  if ((2*EllipseError + XChange) > 0 ) {
425  X--;
426  StoppingX=StoppingX- TwoBSquare;
427  EllipseError=EllipseError+ XChange;
428  XChange=XChange+TwoBSquare;
429  }
430  }
431 
432  Y = YRadius;
433  X = 0;
434  YChange = XRadius*XRadius*(1-2*YRadius);
435  XChange = YRadius*YRadius;
436  EllipseError = 0;
437  StoppingY = TwoASquare*YRadius;
438  StoppingX = 0;
439 
440  while ( StoppingY >=StoppingX ) //{2nd set of points, y'< -1}
441  {
442  Plot4EllipsePoints(CX,CY,X,Y,color);
443  X++;
444  StoppingX=StoppingX + TwoBSquare;
445  EllipseError=EllipseError+ XChange;
446  XChange=XChange+TwoBSquare;
447  if ((2*EllipseError + YChange) > 0 ) {
448  Y--;
449  StoppingY=StoppingY- TwoASquare;
450  EllipseError=EllipseError+ YChange;
451  YChange=YChange+TwoASquare;
452  }
453  }
454 }
455 
465 void inline Plot4EllipsePoints(long CX,long CY, long X, long Y, unsigned int color)
466 {
467  TFT_SetPixel(CX+X, CY+Y, color); //{point in quadrant 1}
468  TFT_SetPixel(CX-X, CY+Y, color); //{point in quadrant 2}
469  TFT_SetPixel(CX-X, CY-Y, color); //{point in quadrant 3}
470  TFT_SetPixel(CX+X, CY-Y, color); //{point in quadrant 4}
471 }
472 
482 void TFT_RightTriangle ( int topx, int topy, int rightx, int righty,unsigned int color)
483 {
484  //draw rectangle one line at a time
485  TFT_Line( topx,topy, rightx,righty,color ); //draw hypotenuse
486  TFT_Line ( topx,righty,topx,topy,color); //draw perpendicular
487  TFT_Line (topx,righty, rightx,righty,color); // draw base
488 }
489 
void TFT_RoundRectangle(unsigned int x, unsigned int y, unsigned int width, unsigned int height, unsigned int radius, unsigned int color)
This function draws a rectangle with round corners.
Definition: GFX.c:256
void TFT_VerticalLine(unsigned int Xaxis, unsigned int Yaxis, unsigned int height, unsigned int color)
Draw a vertical line on the display.
Definition: GFX.c:53
double TFT_dfloor(double value)
Helper funrtion for truncating the double values.
Definition: GFX.c:109
void TFT_SlantyLine(unsigned int lX1, unsigned int lY1, unsigned int lX2, unsigned int lY2, unsigned int color)
Plot a slanty line from x1,y1 to x2,y2 on the display.
Definition: GFX.c:126
void TFT_FullRectangle(unsigned int Xaxis1, unsigned int Yaxis1, unsigned int Xaxis2, unsigned int Yaxis2, unsigned int color)
This function paints a filled rectangle on the screen.
Definition: GFX.c:205
#define absDiff(x, y)
Definition: GFX.h:36
void TFT_SetPixel(unsigned char x_start, unsigned char y_start, unsigned int color)
This functions sets a specific pixel on the TFT display.
Definition: TFT.c:206
void TFT_EmptyRectangle(unsigned int Xaxis1, unsigned int Yaxis1, unsigned int Xaxis2, unsigned int Yaxis2, unsigned int color)
This function paints an empty rectangle on the screen. The width of the contour is 1 pixel...
Definition: GFX.c:227
GFX library header.
void TFT_RightTriangle(int topx, int topy, int rightx, int righty, unsigned int color)
This function paints a right angled triangle.
Definition: GFX.c:482
#define swap(a, b)
Definition: GFX.h:37
void Plot4EllipsePoints(long CX, long CY, long X, long Y, unsigned int color)
Helper function for plotting an ellipse.
Definition: GFX.c:465
void TFT_Disk(unsigned int CenterX, unsigned int CenterY, unsigned int Radius, unsigned int color)
This function plots a full disk on the display.
Definition: GFX.c:332
void TFT_Ellipse(long CX, long CY, long XRadius, long YRadius, unsigned int color)
This function paints a.
Definition: GFX.c:399
void TFT_HorizontalLine(unsigned int Xaxis, unsigned int Yaxis, unsigned int width, unsigned int color)
Draw a horizontal line on the display.
Definition: GFX.c:40
void TFT_Circle(unsigned int CenterX, unsigned int CenterY, unsigned int Radius, unsigned int color)
This function draws a circle centered a x,y with radius R and specific color.
Definition: GFX.c:297
void TFT_DrawCircle(unsigned int CenterX, unsigned int CenterY, unsigned int Radius, unsigned int color)
This function draws a circle centered a x,y with radius R and specific color.
Definition: GFX.c:370
void TFT_Line(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, unsigned int color)
This function paints a.
Definition: GFX.c:67