MPLABĀ® Harmony Graphics Suite
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 
56 typedef enum
57 {
58  LE_TRIG_SINE_TYPE,
59  LE_TRIG_COSINE_TYPE,
61 
66 typedef enum
67 {
68  LE_Q1,
69  LE_Q2,
70  LE_Q3,
71  LE_Q4
72 } LE_QUADRANT;
73 
78 typedef enum
79 {
82 } leArcDir;
83 
84 // *****************************************************************************
85 /* Function:
86  int32_t leMini(int32_t l, int32_t r);
87 
88  Summary:
89  Returns the smaller of two integers.
90 
91  Parameters:
92  l - the first number to test
93  r - the second number to test
94 
95  Returns:
96  int32_t - the smaller of the two numbers
97 */
109 LIB_EXPORT int32_t leMini(int32_t l, int32_t r);
110 
111 // *****************************************************************************
112 /* Function:
113  int32_t leMaxi(int32_t l, int32_t r);
114 
115  Summary:
116  Returns the larger of two integers.
117 
118  Parameters:
119  l - the first number to test
120  r - the second number to test
121 
122  Returns:
123  int32_t - the larger of the two numbers
124 */
136 LIB_EXPORT int32_t leMaxi(int32_t l, int32_t r);
137 
138 // *****************************************************************************
139 /* Function:
140  float leMinf(float l, float r);
141 
142  Summary:
143  Returns the smaller of two floats.
144 
145  Parameters:
146  l - the first float to test
147  r - the second float to test
148 
149  Returns:
150  float - the smaller of the two floats
151 */
163 LIB_EXPORT float leMinf(float l, float r);
164 
165 // *****************************************************************************
166 /* Function:
167  float leMaxf(float l, float r);
168 
169  Summary:
170  Returns the larger of two floats.
171 
172  Parameters:
173  l - the first float to test
174  r - the second float to test
175 
176  Returns:
177  float - the larger of the two floats
178 */
190 LIB_EXPORT float leMaxf(float l, float r);
191 
192 // *****************************************************************************
193 /* Function:
194  int32_t leClampi(int32_t min, int32_t max, int32_t i);
195 
196  Summary:
197  Clamps an integer between a min and max
198 
199  Parameters:
200  min - the minimum value
201  max - the maximum value
202  i - the number to clamp
203 
204  Returns:
205  int32_t - the clamped value
206 */
207 
221 LIB_EXPORT int32_t leClampi(int32_t min, int32_t max, int32_t i);
222 
223 // *****************************************************************************
224 /* Function:
225  float leClampf(float min, float max, float i);
226 
227  Summary:
228  Clamps a float between a min and max
229 
230  Parameters:
231  min - the minimum value
232  max - the maximum value
233  i - the float to clamp
234 
235  Returns:
236  float - the clamped value
237 */
251 LIB_EXPORT float leClampf(float min, float max, float f);
252 
253 // *****************************************************************************
254 /* Function:
255  uint32_t lePercent(uint32_t l, uint32_t r);
256 
257  Summary:
258  Calculates the percentage of one number when applied to another. Integer
259  based. Accuracy for higher numbers is not guaranteed.
260 
261  The result is the decimal percentage multiplied by 100.
262 
263  Parameters:
264  l - the first number of the equation
265  r - the second number of the equation
266 
267  Returns:
268  uint32_t - the percentage represented as a whole number
269 */
283 LIB_EXPORT uint32_t lePercent(uint32_t l, uint32_t r);
284 
285 // *****************************************************************************
286 /* Function:
287  uint32_t lePercentWholeRounded(uint32_t l, uint32_t r);
288 
289  Summary:
290  Calculates the percentage of one number when applied to another. Integer
291  based. Accuracy for higher numbers is not guaranteed.
292 
293  The difference between this and lePercent is that the decimal portion
294  of the whole number is rounded off.
295 
296  Parameters:
297  l - the first number of the equation
298  r - the second number of the equation
299 
300  Returns:
301  uint32_t - the percentage represented as a whole number
302 */
319 LIB_EXPORT uint32_t lePercentWholeRounded(uint32_t l, uint32_t r);
320 
321 // *****************************************************************************
322 /* Function:
323  uint32_t lePercentOf(uint32_t l, uint32_t percent);
324 
325  Summary:
326  Calculates the percentage of a number. Returns a whole number with no
327  decimal component.
328 
329  Parameters:
330  num - the number to consider
331  percent - the percentage to apply
332 
333  Returns:
334  uint32_t - the resultant percentage of the number
335 */
347 LIB_EXPORT uint32_t lePercentOf(uint32_t num, uint32_t percent);
348 
349 // *****************************************************************************
350 /* Function:
351  void lePercentOfDec(uint32_t num, uint32_t percent, uint32_t* whl, uint32_t* dec);
352 
353  Summary:
354  Calculates the percentage of a number. Returns a whole number and the tenths digit
355  decimal component.
356 
357  Parameters:
358  num - the number to consider
359  percent - the percentage to apply
360  whl - pointer to whole number value
361  dec - pointer to tenths digit as an interger
362 
363  Returns:
364  none
365 */
379 LIB_EXPORT void lePercentOfDec(uint32_t num, uint32_t percent, uint32_t* whl, uint32_t* dec);
380 
381 // *****************************************************************************
382 /* Function:
383  uint32_t leScaleInteger(uint32_t num, uint32_t oldMax, uint32_t newMax);
384 
385  Summary:
386  Scales an integer from one number range of 0 -> n0 to another range 0 -> n1
387  based on percentages.
388 
389  Parameters:
390  num - the number to consider
391  oldMax - the old range maximum
392  newMax - the new range maximum
393 
394  Returns:
395  uint32_t - the number as defined in the new number range
396 */
412 LIB_EXPORT uint32_t leScaleInteger(uint32_t num, uint32_t oldMax, uint32_t newMax);
413 
414 // *****************************************************************************
415 /* Function:
416  int32_t leScaleIntegerSigned(int32_t num, int32_t oldMax, int32_t newMax);
417 
418  Summary:
419  Scales a signed integer from one number range of 0 -> n0 to another range 0 -> n1
420  based on percentages.
421 
422  Parameters:
423  num - the number to consider
424  oldMax - the old range maximum
425  newMax - the new range maximum
426 
427  Returns:
428  int32_t - the number as defined in the new number range
429 */
443 LIB_EXPORT int32_t leScaleIntegerSigned(int32_t num, int32_t oldMax, int32_t newMax);
444 
445 // *****************************************************************************
446 /* Function:
447  uint32_t leAbsoluteValue(int32_t val);
448 
449  Summary:
450  Calculates the absolute value of a signed integer.
451 
452  Parameters:
453  val - the number to consider
454 
455  Returns:
456  uint32_t - the absolute value
457 */
468 LIB_EXPORT uint32_t leAbsoluteValue(int32_t val);
469 
470 // *****************************************************************************
471 /* Function:
472  int32_t leLerp(int32_t x, int32_t y, uint32_t per);
473 
474  Summary:
475  Performs a linear interpolation of an integer based on a percentage between
476  two signed points.
477 
478  Parameters:
479  x - the first point to consider
480  y - the second point to consider
481  per - the percentage of interpolation
482 
483  Returns:
484  int32_t - the interpolated value
485 */
500 LIB_EXPORT int32_t leLerp(int32_t x, int32_t y, uint32_t per);
501 
502 // *****************************************************************************
503 /* Function:
504  int32_t leDivideRounding(int32_t num, int32_t denom);
505 
506  Summary:
507  Calculates integer division of num/denom with rounding based on LSB/2.
508 
509  Parameters:
510  num - numerator
511  denom - denominator
512 
513  Returns:
514  int32_t - result, equivalent to int32_t( ((float)num)/((float)denom) + 0.5 ) without using floating point
515 */
529 LIB_EXPORT int32_t leDivideRounding(int32_t num, int32_t denom);
530 
531 /**************************************************************************
532  Function:
533  leResult lePolarToXY(int32_t r, int32_t a, lePoint* p);
534 
535  Summary:
536  Performs a cosine lookup to determine the points in an arc at specified
537  radius and angle (polar to Cartesian conversion)
538  Parameters:
539  r - radius
540  a - angle (in degrees)
541  Returns:
542  p - the output point in Cartesian plane
543  **************************************************************************/
557 LIB_EXPORT leResult lePolarToXY(int32_t r, int32_t a, lePoint* p);
558 
559 // *****************************************************************************
560 /* Function:
561  int32_t leNormalizeAngle(int32_t t)
562 
563  Summary:
564  normalize an angle between 0 - 360
565 
566  Parameters:
567  int32_t t - angle (in degrees)
568 
569  Returns:
570  int32_t - normalize an angle in degrees.
571  Example: t = -5, return value is 355
572  t = 450, return value is 90
573 */
587 LIB_EXPORT int32_t leNormalizeAngle(int32_t t);
588 
589 
590 // *****************************************************************************
591 /* Function:
592  int32_t leSin(int32_t v)
593 
594  Summary:
595  Returns the sine of the value v * 256
596 
597  Parameters:
598  v - angle (in degrees)
599 
600  Returns:
601  int32_t - result of sine fixed point value (times 256), calling function needs
602  to divide by 256 to get good result
603 
604  ex. "a * sin(v)" would be "a * leSin(v) / 256";
605 */
618 LIB_EXPORT int32_t leSin(int32_t v);
619 
620 // *****************************************************************************
621 /* Function:
622  int32_t leCos(int32_t v)
623 
624  Summary:
625  Returns the cosine of the value v
626 
627  Parameters:
628  v - angle (in degrees)
629 
630  Returns:
631  int32_t - result of cosine fixed point value (times 256), calling function
632  needs to divide by 256 to get good result
633 
634  ex. "a * cos(v)" would be "a * leCos(v) / 256";
635 */
648 LIB_EXPORT int32_t leCos(int32_t v);
649 
650 /************************************************************************************************
651  Function:
652  leResult leEllipsePoint(int32_t t, int32_t a, int32_t b, int32_t theta, lePoint* p);
653 
654  Summary:
655  Performs a cosine lookup to determine the points in an arc at specified
656  radius and angle (polar > cartesian conversion)
657  Parameters:
658  t - angle of the point on the ellipse (in degrees)
659  a - the half\-length of 0\-180 axis of the ellipse
660  b - the half\-length of 90\-270 axis of the ellipse
661  theta - angle of the ellipse (in degrees)
662  Returns:
663  p - the output point in cartesian plane
664  ************************************************************************************************/
680 LIB_EXPORT leResult leEllipsePoint(int32_t t, int32_t a, int32_t b, int32_t theta, lePoint* p);
681 
682 // *****************************************************************************
683 /* Function:
684  double leAtan(int32_t x, int32_t y);
685 
686  Summary:
687  Performs the inverse tangent operation
688 
689  Parameters:
690  val - floating point value
691 
692  Returns:
693  double - the angle in radians
694 */
706 LIB_EXPORT double leAtan(double val);
707 
708 typedef struct
709 {
710  leBool q1;
711  leBool q2;
712  leBool q3;
713  leBool q4;
715 
716 leBool lePointOnLineSide(lePoint* pt,
717  lePoint* linePt,
718  lePoint* sign);
719 
720 // *****************************************************************************
721 /* Function:
722  void leSortPointsX(lePoint* p1, lePoint* p2)
723 
724  Summary:
725  Sorts two points on the X axis.
726 
727  Description:
728  Sorts two points on the X axis.
729 
730  Parameters:
731  lePoint* p1 - the first point
732  lePoint* p2 - the second point
733 
734  Returns:
735  The pointers are modified with the results of the sort
736 
737  Remarks:
738 
739 */
751 void leSortPointsX(lePoint* p1, lePoint* p2);
752 
753 // *****************************************************************************
754 /* Function:
755  void leSortPointsX(lePoint* p1, lePoint* p2)
756 
757  Summary:
758  Sorts two points on the Y axis.
759 
760  Description:
761  Sorts two points on the Y axis.
762 
763  Parameters:
764  lePoint* p1 - the first point
765  lePoint* p2 - the second point
766 
767  Returns:
768  The pointers are modified with the results of the sort
769 
770  Remarks:
771 
772 */
784 void leSortPointsY(lePoint* p1, lePoint* p2);
785 
786 // *****************************************************************************
787 /* Function:
788  int32_t leGetXGivenYOnLine(lePoint p1, lePoint p2, int32_t y)
789 
790  Summary:
791  Projects a point onto another point given a y coordinate.
792 
793  Description:
794  Projects a point onto another point given a y coordinate.
795 
796  Parameters:
797  lePoint p1 - the first point
798  lePoint p2 - the second point
799  int32_t y - the y coordinate
800 
801  Returns:
802  int32_t - the resultant x coordinate
803 
804  Remarks:
805 
806 */
823 int32_t leGetXGivenYOnLine(lePoint p1, lePoint p2, int32_t y);
824 
825 // *****************************************************************************
826 /* Function:
827  int32_t leGetYGivenXOnLine(lePoint p1, lePoint p2, int32_t x)
828 
829  Summary:
830  Projects a point onto another point given a x coordinate.
831 
832  Description:
833  Projects a point onto another point given a x coordinate.
834 
835  Parameters:
836  lePoint p1 - the first point
837  lePoint p2 - the second point
838  int32_t x - the x coordinate
839 
840  Returns:
841  int32_t - the resultant y coordinate
842 
843  Remarks:
844 
845 */
862 int32_t leGetYGivenXOnLine(lePoint p1, lePoint p2, int32_t x);
863 
864 // *****************************************************************************
865 /* Function:
866  lePoint leRotatePoint(lePoint pos,
867  lePoint org,
868  int32_t ang)
869 
870  Summary:
871  Rotates a point given an origin and an angle in degrees.
872 
873  Description:
874  Rotates a point given an origin and an angle in degrees.
875 
876  Parameters:
877  lePoint pos - the point to rotate
878  lePoint org - the origin of the rotation
879  int32_t ang - the angle in degrees
880 
881  Returns:
882  lePoint - the rotated point
883 
884  Remarks:
885 
886 */
904  lePoint org,
905  int32_t ang);
906 
907 // *****************************************************************************
908 /* Function:
909  leRect leRotatedRectBounds(leRect rect,
910  lePoint org,
911  int32_t ang)
912 
913  Summary:
914  Calculates the bounding rectangle of an area rotated around
915  an anchor point.
916 
917  Description:
918  Calculates the bounding rectangle of an area rotated around
919  an anchor point.
920 
921  Parameters:
922  leRect rect - the area to rotate
923  lePoint org - the origin of the rotation
924  int32_t ang - the angle in degrees
925 
926  Returns:
927  leRect - the rectangle that completely contains the rotated area
928 
929  Remarks:
930 
931 */
947  int32_t ang);
948 
949 
950 // *****************************************************************************
951 /* Function:
952  float leSqrt(const float x)
953 
954  Summary:
955  Fast square root approximation function.
956 
957  Description:
958  Approximates a square root using the magic number method.
959 
960  Parameters:
961  const float x - the number to calculate the root for
962 
963  Returns:
964  float - square root of the input number
965 
966  Remarks:
967 
968 */
969 float leSqrt(const float x);
970 
971 lePoint lePointOnCircle(uint32_t radius,
972  int32_t angle);
973 
974 uint32_t leDegreesFromPercent(uint32_t percent,
975  int32_t centerAngle,
976  int32_t startAngle);
977 
978 uint32_t lePercentFromDegrees(uint32_t degrees,
979  int32_t centerAngle,
980  int32_t startAngle);
981 
982 void leNormalizeAngles(int32_t startAngle,
983  int32_t spanAngle,
984  int32_t* normalizedStartAngle,
985  int32_t* normalizedEndAngle);
986 
987 typedef struct leResolvedAngleRanges
988 {
989  uint32_t angleCount;
990  struct
991  {
992  int32_t startAngle;
993  int32_t endAngle;
994  leArcQuadrantQuery quadrants;
995  } angle0;
996  struct
997  {
998  int32_t startAngle;
999  int32_t endAngle;
1000  leArcQuadrantQuery quadrants;
1001  } angle1;
1003 
1004 leResolvedAngleRanges leResolveAngles(int32_t startAngle,
1005  int32_t spanAngle);
1006 
1007 float leRound(float flt);
1008 
1009 #endif /* LE_MATH_H */
leNormalizeAngle
LIB_EXPORT int32_t leNormalizeAngle(int32_t t)
Normalize an angle between 0 - 360.
Definition: legato_math.c:49
legato_math.h
Defines common math functions for general use.
leClampf
LIB_EXPORT float leClampf(float min, float max, float f)
Calculate clamp of a float.
Definition: legato_math.c:185
leResult
leResult
This enum represents function call results.
Definition: legato_common.h:134
lePercentWholeRounded
LIB_EXPORT uint32_t lePercentWholeRounded(uint32_t l, uint32_t r)
Calculate percent whole rounded.
Definition: legato_math.c:204
leLerp
LIB_EXPORT 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
leEllipsePoint
LIB_EXPORT 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
leRect
This struct represents a rectangle.
Definition: legato_common.h:405
leMaxf
LIB_EXPORT float leMaxf(float l, float r)
Calculate maximum of two floats.
Definition: legato_math.c:166
LE_QUADRANT
LE_QUADRANT
Used to define the basic four quandrants of a coordinate plane.
Definition: legato_math.h:67
leCos
LIB_EXPORT int32_t leCos(int32_t v)
Calculate cosine of a number.
Definition: legato_math.c:93
lePercentOfDec
LIB_EXPORT void lePercentOfDec(uint32_t num, uint32_t percent, uint32_t *whl, uint32_t *dec)
Calculate percent of a decimal.
Definition: legato_math.c:231
leArcDir
leArcDir
Used to define arc direction.
Definition: legato_math.h:79
lePercentOf
LIB_EXPORT uint32_t lePercentOf(uint32_t num, uint32_t percent)
Calculate percent of a number.
Definition: legato_math.c:218
leSin
LIB_EXPORT int32_t leSin(int32_t v)
Calculate sin of a number.
Definition: legato_math.c:64
lePercent
LIB_EXPORT uint32_t lePercent(uint32_t l, uint32_t r)
Calculate percent of number.
Definition: legato_math.c:199
leArcQuadrantQuery
Definition: legato_math.h:709
leGetXGivenYOnLine
int32_t leGetXGivenYOnLine(lePoint p1, lePoint p2, int32_t y)
Project point.
Definition: legato_math.c:438
LE_TRIG_FUNCTION_TYPE
LE_TRIG_FUNCTION_TYPE
Used to define the types of trig functions.
Definition: legato_math.h:57
leMaxi
LIB_EXPORT int32_t leMaxi(int32_t l, int32_t r)
Calculate maximum of two integers.
Definition: legato_math.c:156
LE_CCW
@ LE_CCW
Definition: legato_math.h:80
leBool
leBool
This enum represents booleans.
Definition: legato_common.h:157
leAtan
LIB_EXPORT double leAtan(double val)
Calculate atan of points.
Definition: legato_math.c:145
LE_FALSE
@ LE_FALSE
Definition: legato_common.h:158
leRotatedRectBounds
leRect leRotatedRectBounds(leRect rect, int32_t ang)
Calculate bounding rectangle.
Definition: legato_math.c:475
leMinf
LIB_EXPORT float leMinf(float l, float r)
Calculate minimum of two floats.
Definition: legato_math.c:161
leScaleIntegerSigned
LIB_EXPORT int32_t leScaleIntegerSigned(int32_t num, int32_t oldMax, int32_t newMax)
Calculate the scale of signed integer.
Definition: legato_math.c:245
leMini
LIB_EXPORT int32_t leMini(int32_t l, int32_t r)
Calculate minimum of two integers.
Definition: legato_math.c:151
leClampi
LIB_EXPORT int32_t leClampi(int32_t min, int32_t max, int32_t i)
Calculates clamp of an integer.
Definition: legato_math.c:171
lePolarToXY
LIB_EXPORT leResult lePolarToXY(int32_t r, int32_t a, lePoint *p)
Generate points in an arc.
Definition: legato_math.c:124
leRotatePoint
lePoint leRotatePoint(lePoint pos, lePoint org, int32_t ang)
Rotates point.
Definition: legato_math.c:454
leAbsoluteValue
LIB_EXPORT uint32_t leAbsoluteValue(int32_t val)
Calculates the absolute value of a signed integer.
Definition: legato_math.c:280
leSortPointsY
void leSortPointsY(lePoint *p1, lePoint *p2)
Sorts two points on the Y axis.
Definition: legato_math.c:426
LE_CW
@ LE_CW
Definition: legato_math.h:81
leScaleInteger
LIB_EXPORT uint32_t leScaleInteger(uint32_t num, uint32_t oldMax, uint32_t newMax)
Calculate the scale of an integer.
Definition: legato_math.c:256
leResolvedAngleRanges
Definition: legato_math.h:988
LE_TRUE
@ LE_TRUE
Definition: legato_common.h:159
legato_common.h
Common macros and definitions used by Legato.
leDivideRounding
LIB_EXPORT 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
leSortPointsX
void leSortPointsX(lePoint *p1, lePoint *p2)
Sorts two points on the X axis.
Definition: legato_math.c:414
leGetYGivenXOnLine
int32_t leGetYGivenXOnLine(lePoint p1, lePoint p2, int32_t x)
Project Y give X.
Definition: legato_math.c:446
lePoint
This structure represents a integer Cartesian point.
Definition: legato_common.h:357