MPLABĀ® Harmony Graphics Suite  GFX v3.13.0
Legato API Documentation
legato_math.h
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2020 Microchip Technology Inc. and its subsidiaries.
3 *
4 * Subject to your compliance with these terms, you may use Microchip software
5 * and any derivatives exclusively with Microchip products. It is your
6 * responsibility to comply with third party license terms applicable to your
7 * use of third party software (including open source software) that may
8 * accompany Microchip software.
9 *
10 * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
11 * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
12 * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
13 * PARTICULAR PURPOSE.
14 *
15 * IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
16 * INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
17 * WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
18 * BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
19 * FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
20 * ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
21 * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
22 *******************************************************************************/
23 
24 /*******************************************************************************
25  Module for Microchip Graphics Library - Legato User Interface Library
26 
27  Company:
28  Microchip Technology Inc.
29 
30  File Name:
31  legato_math.h
32 
33  Summary:
34  Contains some general purpose math functions
35 
36  Description:
37  Math support functions.
38 *******************************************************************************/
39 
47 #ifndef LE_MATH_H
48 #define LE_MATH_H
49 
51 
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55 
60 typedef enum
61 {
62  LE_TRIG_SINE_TYPE,
63  LE_TRIG_COSINE_TYPE,
65 
70 typedef enum
71 {
72  LE_Q1,
73  LE_Q2,
74  LE_Q3,
75  LE_Q4
76 } LE_QUADRANT;
77 
82 typedef enum
83 {
86 } leArcDir;
87 
88 // *****************************************************************************
89 /* Function:
90  int32_t leMini(int32_t l, int32_t r);
91 
92  Summary:
93  Returns the smaller of two integers.
94 
95  Parameters:
96  l - the first number to test
97  r - the second number to test
98 
99  Returns:
100  int32_t - the smaller of the two numbers
101 */
113 int32_t leMini(int32_t l, int32_t r);
114 
115 // *****************************************************************************
116 /* Function:
117  int32_t leMaxi(int32_t l, int32_t r);
118 
119  Summary:
120  Returns the larger of two integers.
121 
122  Parameters:
123  l - the first number to test
124  r - the second number to test
125 
126  Returns:
127  int32_t - the larger of the two numbers
128 */
140 int32_t leMaxi(int32_t l, int32_t r);
141 
142 // *****************************************************************************
143 /* Function:
144  float leMinf(float l, float r);
145 
146  Summary:
147  Returns the smaller of two floats.
148 
149  Parameters:
150  l - the first float to test
151  r - the second float to test
152 
153  Returns:
154  float - the smaller of the two floats
155 */
167 float leMinf(float l, float r);
168 
169 // *****************************************************************************
170 /* Function:
171  float leMaxf(float l, float r);
172 
173  Summary:
174  Returns the larger of two floats.
175 
176  Parameters:
177  l - the first float to test
178  r - the second float to test
179 
180  Returns:
181  float - the larger of the two floats
182 */
194 float leMaxf(float l, float r);
195 
196 // *****************************************************************************
197 /* Function:
198  int32_t leClampi(int32_t min, int32_t max, int32_t i);
199 
200  Summary:
201  Clamps an integer between a min and max
202 
203  Parameters:
204  min - the minimum value
205  max - the maximum value
206  i - the number to clamp
207 
208  Returns:
209  int32_t - the clamped value
210 */
211 
225 int32_t leClampi(int32_t min, int32_t max, int32_t i);
226 
227 // *****************************************************************************
228 /* Function:
229  float leClampf(float min, float max, float i);
230 
231  Summary:
232  Clamps a float between a min and max
233 
234  Parameters:
235  min - the minimum value
236  max - the maximum value
237  i - the float to clamp
238 
239  Returns:
240  float - the clamped value
241 */
255 float leClampf(float min, float max, float f);
256 
257 // *****************************************************************************
258 /* Function:
259  uint32_t lePercent(uint32_t l, uint32_t r);
260 
261  Summary:
262  Calculates the percentage of one number when applied to another. Integer
263  based. Accuracy for higher numbers is not guaranteed.
264 
265  The result is the decimal percentage multiplied by 100.
266 
267  Parameters:
268  l - the first number of the equation
269  r - the second number of the equation
270 
271  Returns:
272  uint32_t - the percentage represented as a whole number
273 */
287 uint32_t lePercent(uint32_t l, uint32_t r);
288 
289 // *****************************************************************************
290 /* Function:
291  uint32_t lePercentWholeRounded(uint32_t l, uint32_t r);
292 
293  Summary:
294  Calculates the percentage of one number when applied to another. Integer
295  based. Accuracy for higher numbers is not guaranteed.
296 
297  The difference between this and lePercent is that the decimal portion
298  of the whole number is rounded off.
299 
300  Parameters:
301  l - the first number of the equation
302  r - the second number of the equation
303 
304  Returns:
305  uint32_t - the percentage represented as a whole number
306 */
323 uint32_t lePercentWholeRounded(uint32_t l, uint32_t r);
324 
325 // *****************************************************************************
326 /* Function:
327  uint32_t lePercentOf(uint32_t l, uint32_t percent);
328 
329  Summary:
330  Calculates the percentage of a number. Returns a whole number with no
331  decimal component.
332 
333  Parameters:
334  num - the number to consider
335  percent - the percentage to apply
336 
337  Returns:
338  uint32_t - the resultant percentage of the number
339 */
351 uint32_t lePercentOf(uint32_t num, uint32_t percent);
352 
353 // *****************************************************************************
354 /* Function:
355  void lePercentOfDec(uint32_t num, uint32_t percent, uint32_t* whl, uint32_t* dec);
356 
357  Summary:
358  Calculates the percentage of a number. Returns a whole number and the tenths digit
359  decimal component.
360 
361  Parameters:
362  num - the number to consider
363  percent - the percentage to apply
364  whl - pointer to whole number value
365  dec - pointer to tenths digit as an interger
366 
367  Returns:
368  none
369 */
383 void lePercentOfDec(uint32_t num, uint32_t percent, uint32_t* whl, uint32_t* dec);
384 
385 // *****************************************************************************
386 /* Function:
387  uint32_t leScaleInteger(uint32_t num, uint32_t oldMax, uint32_t newMax);
388 
389  Summary:
390  Scales an integer from one number range of 0 -> n0 to another range 0 -> n1
391  based on percentages.
392 
393  Parameters:
394  num - the number to consider
395  oldMax - the old range maximum
396  newMax - the new range maximum
397 
398  Returns:
399  uint32_t - the number as defined in the new number range
400 */
416 uint32_t leScaleInteger(uint32_t num, uint32_t oldMax, uint32_t newMax);
417 
418 // *****************************************************************************
419 /* Function:
420  int32_t leScaleIntegerSigned(int32_t num, int32_t oldMax, int32_t newMax);
421 
422  Summary:
423  Scales a signed integer from one number range of 0 -> n0 to another range 0 -> n1
424  based on percentages.
425 
426  Parameters:
427  num - the number to consider
428  oldMax - the old range maximum
429  newMax - the new range maximum
430 
431  Returns:
432  int32_t - the number as defined in the new number range
433 */
447 int32_t leScaleIntegerSigned(int32_t num, int32_t oldMax, int32_t newMax);
448 
449 // *****************************************************************************
450 /* Function:
451  uint32_t leAbsoluteValue(int32_t val);
452 
453  Summary:
454  Calculates the absolute value of a signed integer.
455 
456  Parameters:
457  val - the number to consider
458 
459  Returns:
460  uint32_t - the absolute value
461 */
472 uint32_t leAbsoluteValue(int32_t val);
473 
474 // *****************************************************************************
475 /* Function:
476  int32_t leLerp(int32_t x, int32_t y, uint32_t per);
477 
478  Summary:
479  Performs a linear interpolation of an integer based on a percentage between
480  two signed points.
481 
482  Parameters:
483  x - the first point to consider
484  y - the second point to consider
485  per - the percentage of interpolation
486 
487  Returns:
488  int32_t - the interpolated value
489 */
504 int32_t leLerp(int32_t x, int32_t y, uint32_t per);
505 
506 // *****************************************************************************
507 /* Function:
508  int32_t leDivideRounding(int32_t num, int32_t denom);
509 
510  Summary:
511  Calculates integer division of num/denom with rounding based on LSB/2.
512 
513  Parameters:
514  num - numerator
515  denom - denominator
516 
517  Returns:
518  int32_t - result, equivalent to int32_t( ((float)num)/((float)denom) + 0.5 ) without using floating point
519 */
533 int32_t leDivideRounding(int32_t num, int32_t denom);
534 
535 /**************************************************************************
536  Function:
537  leResult lePolarToXY(int32_t r, int32_t a, lePoint* p);
538 
539  Summary:
540  Performs a cosine lookup to determine the points in an arc at specified
541  radius and angle (polar to Cartesian conversion)
542  Parameters:
543  r - radius
544  a - angle (in degrees)
545  Returns:
546  p - the output point in Cartesian plane
547  **************************************************************************/
561 leResult lePolarToXY(int32_t r, int32_t a, lePoint* p);
562 
563 // *****************************************************************************
564 /* Function:
565  int32_t leNormalizeAngle(int32_t t)
566 
567  Summary:
568  normalize an angle between 0 - 360
569 
570  Parameters:
571  int32_t t - angle (in degrees)
572 
573  Returns:
574  int32_t - normalize an angle in degrees.
575  Example: t = -5, return value is 355
576  t = 450, return value is 90
577 */
591 int32_t leNormalizeAngle(int32_t t);
592 
593 
594 // *****************************************************************************
595 /* Function:
596  int32_t leSin(int32_t v)
597 
598  Summary:
599  Returns the sine of the value v * 256
600 
601  Parameters:
602  v - angle (in degrees)
603 
604  Returns:
605  int32_t - result of sine fixed point value (times 256), calling function needs
606  to divide by 256 to get good result
607 
608  ex. "a * sin(v)" would be "a * leSin(v) / 256";
609 */
622 int32_t leSin(int32_t v);
623 
624 // *****************************************************************************
625 /* Function:
626  int32_t leCos(int32_t v)
627 
628  Summary:
629  Returns the cosine of the value v
630 
631  Parameters:
632  v - angle (in degrees)
633 
634  Returns:
635  int32_t - result of cosine fixed point value (times 256), calling function
636  needs to divide by 256 to get good result
637 
638  ex. "a * cos(v)" would be "a * leCos(v) / 256";
639 */
652 int32_t leCos(int32_t v);
653 
654 /************************************************************************************************
655  Function:
656  leResult leEllipsePoint(int32_t t, int32_t a, int32_t b, int32_t theta, lePoint* p);
657 
658  Summary:
659  Performs a cosine lookup to determine the points in an arc at specified
660  radius and angle (polar > cartesian conversion)
661  Parameters:
662  t - angle of the point on the ellipse (in degrees)
663  a - the half\-length of 0\-180 axis of the ellipse
664  b - the half\-length of 90\-270 axis of the ellipse
665  theta - angle of the ellipse (in degrees)
666  Returns:
667  p - the output point in cartesian plane
668  ************************************************************************************************/
684 leResult leEllipsePoint(int32_t t, int32_t a, int32_t b, int32_t theta, lePoint* p);
685 
686 // *****************************************************************************
687 /* Function:
688  double leAtan(int32_t x, int32_t y);
689 
690  Summary:
691  Performs the inverse tangent operation
692 
693  Parameters:
694  val - floating point value
695 
696  Returns:
697  double - the angle in radians
698 */
710 double leAtan(double val);
711 
712 typedef struct
713 {
714  leBool q1;
715  leBool q2;
716  leBool q3;
717  leBool q4;
719 
720 leBool lePointOnLineSide(lePoint* pt,
721  lePoint* linePt,
722  lePoint* sign);
723 
724 // *****************************************************************************
725 /* Function:
726  void leSortPointsX(lePoint* p1, lePoint* p2)
727 
728  Summary:
729  Sorts two points on the X axis.
730 
731  Description:
732  Sorts two points on the X axis.
733 
734  Parameters:
735  lePoint* p1 - the first point
736  lePoint* p2 - the second point
737 
738  Returns:
739  The pointers are modified with the results of the sort
740 
741  Remarks:
742 
743 */
755 void leSortPointsX(lePoint* p1, lePoint* p2);
756 
757 // *****************************************************************************
758 /* Function:
759  void leSortPointsX(lePoint* p1, lePoint* p2)
760 
761  Summary:
762  Sorts two points on the Y axis.
763 
764  Description:
765  Sorts two points on the Y axis.
766 
767  Parameters:
768  lePoint* p1 - the first point
769  lePoint* p2 - the second point
770 
771  Returns:
772  The pointers are modified with the results of the sort
773 
774  Remarks:
775 
776 */
788 void leSortPointsY(lePoint* p1, lePoint* p2);
789 
790 // *****************************************************************************
791 /* Function:
792  int32_t leGetXGivenYOnLine(lePoint p1, lePoint p2, int32_t y)
793 
794  Summary:
795  Projects a point onto another point given a y coordinate.
796 
797  Description:
798  Projects a point onto another point given a y coordinate.
799 
800  Parameters:
801  lePoint p1 - the first point
802  lePoint p2 - the second point
803  int32_t y - the y coordinate
804 
805  Returns:
806  int32_t - the resultant x coordinate
807 
808  Remarks:
809 
810 */
827 int32_t leGetXGivenYOnLine(lePoint p1, lePoint p2, int32_t y);
828 
829 // *****************************************************************************
830 /* Function:
831  int32_t leGetYGivenXOnLine(lePoint p1, lePoint p2, int32_t x)
832 
833  Summary:
834  Projects a point onto another point given a x coordinate.
835 
836  Description:
837  Projects a point onto another point given a x coordinate.
838 
839  Parameters:
840  lePoint p1 - the first point
841  lePoint p2 - the second point
842  int32_t x - the x coordinate
843 
844  Returns:
845  int32_t - the resultant y coordinate
846 
847  Remarks:
848 
849 */
866 int32_t leGetYGivenXOnLine(lePoint p1, lePoint p2, int32_t x);
867 
868 // *****************************************************************************
869 /* Function:
870  void leRotatePoint(lePoint pos,
871  lePoint org,
872  int32_t ang,
873  lePoint* res)
874 
875  Summary:
876  Rotates a point given an origin and an angle in degrees.
877 
878  Description:
879  Rotates a point given an origin and an angle in degrees.
880 
881  Parameters:
882  lePoint pos - the point to rotate
883  lePoint org - the origin of the rotation
884  int32_t ang - the angle in degrees
885 
886  Returns:
887  lePoint - the rotated point
888 
889  Remarks:
890 
891 */
910 void leRotatePoint(lePoint pos,
911  lePoint org,
912  int32_t ang,
913  lePoint* res);
914 
915 // *****************************************************************************
916 /* Function:
917  void leRotatedRectBounds(leRect rect,
918  lePoint org,
919  int32_t ang,
920  leRect* res)
921 
922  Summary:
923  Calculates the bounding rectangle of an area rotated around
924  an anchor point.
925 
926  Description:
927  Calculates the bounding rectangle of an area rotated around
928  an anchor point.
929 
930  Parameters:
931  leRect rect - the area to rotate
932  lePoint org - the origin of the rotation
933  int32_t ang - the angle in degrees
934 
935  Returns:
936  leRect - the rectangle that completely contains the rotated area
937 
938  Remarks:
939 
940 */
956 void leRotatedRectBounds(leRect rect,
957  int32_t ang,
958  leRect* res);
959 
960 
961 // *****************************************************************************
962 /* Function:
963  float leSqrt(const float x)
964 
965  Summary:
966  Fast square root approximation function.
967 
968  Description:
969  Approximates a square root using the magic number method.
970 
971  Parameters:
972  const float x - the number to calculate the root for
973 
974  Returns:
975  float - square root of the input number
976 
977  Remarks:
978 
979 */
980 float leSqrt(const float x);
981 
982 void lePointOnCircle(uint32_t radius,
983  int32_t angle,
984  lePoint* res);
985 
986 uint32_t leDegreesFromPercent(uint32_t percent,
987  int32_t centerAngle,
988  int32_t startAngle);
989 
990 uint32_t lePercentFromDegrees(uint32_t degrees,
991  int32_t centerAngle,
992  int32_t startAngle);
993 
994 void leNormalizeAngles(int32_t startAngle,
995  int32_t spanAngle,
996  int32_t* normalizedStartAngle,
997  int32_t* normalizedEndAngle);
998 
999 typedef struct leResolvedAngleRanges
1000 {
1001  uint32_t angleCount;
1002  struct
1003  {
1004  int32_t startAngle;
1005  int32_t endAngle;
1006  leArcQuadrantQuery quadrants;
1007  } angle0;
1008  struct
1009  {
1010  int32_t startAngle;
1011  int32_t endAngle;
1012  leArcQuadrantQuery quadrants;
1013  } angle1;
1015 
1016 void leResolveAngles(int32_t startAngle,
1017  int32_t spanAngle,
1018  leResolvedAngleRanges* res);
1019 
1020 float leRound(float flt);
1021 
1022 #ifdef __cplusplus
1023 }
1024 #endif
1025 
1026 #endif /* LE_MATH_H */
int32_t leClampi(int32_t min, int32_t max, int32_t i)
Calculates clamp of an integer.
Definition: legato_math.c:171
Definition: legato_math.h:84
leResult leEllipsePoint(int32_t t, int32_t a, int32_t b, int32_t theta, lePoint *p)
Calculates points in an arc.
Definition: legato_math.c:132
Common macros and definitions used by Legato.
int32_t leScaleIntegerSigned(int32_t num, int32_t oldMax, int32_t newMax)
Calculate the scale of signed integer.
Definition: legato_math.c:245
leResult lePolarToXY(int32_t r, int32_t a, lePoint *p)
Generate points in an arc.
Definition: legato_math.c:124
void leRotatePoint(lePoint pos, lePoint org, int32_t ang, lePoint *res)
Rotates point.
Definition: legato_math.c:452
leArcDir
Used to define arc direction.
Definition: legato_math.h:82
int32_t leMaxi(int32_t l, int32_t r)
Calculate maximum of two integers.
Definition: legato_math.c:156
int32_t leGetXGivenYOnLine(lePoint p1, lePoint p2, int32_t y)
Project point.
Definition: legato_math.c:436
uint32_t lePercentOf(uint32_t num, uint32_t percent)
Calculate percent of a number.
Definition: legato_math.c:218
int32_t leDivideRounding(int32_t num, int32_t denom)
Performs a linear interpolation of an integer based on a percentage between two signed points...
Definition: legato_math.c:328
void leRotatedRectBounds(leRect rect, int32_t ang, leRect *res)
Calculate bounding rectangle.
Definition: legato_math.c:472
leResult
This enum represents function call results.
Definition: legato_common.h:123
float leMinf(float l, float r)
Calculate minimum of two floats.
Definition: legato_math.c:161
uint32_t lePercentWholeRounded(uint32_t l, uint32_t r)
Calculate percent whole rounded.
Definition: legato_math.c:204
Definition: legato_math.h:85
int32_t leLerp(int32_t x, int32_t y, uint32_t per)
Calculates a linear interpolation of an integer based on a percentage between two signed points...
Definition: legato_math.c:290
This struct represents a rectangle.
Definition: legato_common.h:394
void leSortPointsY(lePoint *p1, lePoint *p2)
Sorts two points on the Y axis.
Definition: legato_math.c:424
int32_t leMini(int32_t l, int32_t r)
Calculate minimum of two integers.
Definition: legato_math.c:151
Definition: legato_math.h:999
void lePercentOfDec(uint32_t num, uint32_t percent, uint32_t *whl, uint32_t *dec)
Calculate percent of a decimal.
Definition: legato_math.c:231
Definition: legato_math.h:712
uint32_t leAbsoluteValue(int32_t val)
Calculates the absolute value of a signed integer.
Definition: legato_math.c:280
int32_t leCos(int32_t v)
Calculate cosine of a number.
Definition: legato_math.c:93
void leSortPointsX(lePoint *p1, lePoint *p2)
Sorts two points on the X axis.
Definition: legato_math.c:412
int32_t leSin(int32_t v)
Calculate sin of a number.
Definition: legato_math.c:64
This structure represents a integer Cartesian point.
Definition: legato_common.h:346
LE_QUADRANT
Used to define the basic four quandrants of a coordinate plane.
Definition: legato_math.h:70
uint32_t leScaleInteger(uint32_t num, uint32_t oldMax, uint32_t newMax)
Calculate the scale of an integer.
Definition: legato_math.c:256
leBool
This enum represents booleans.
Definition: legato_common.h:146
uint32_t lePercent(uint32_t l, uint32_t r)
Calculate percent of number.
Definition: legato_math.c:199
int32_t leNormalizeAngle(int32_t t)
Normalize an angle between 0 - 360.
Definition: legato_math.c:49
int32_t leGetYGivenXOnLine(lePoint p1, lePoint p2, int32_t x)
Project Y give X.
Definition: legato_math.c:444
float leMaxf(float l, float r)
Calculate maximum of two floats.
Definition: legato_math.c:166
float leClampf(float min, float max, float f)
Calculate clamp of a float.
Definition: legato_math.c:185
LE_TRIG_FUNCTION_TYPE
Used to define the types of trig functions.
Definition: legato_math.h:60
double leAtan(double val)
Calculate atan of points.
Definition: legato_math.c:145