Qwt User's Guide  5.2.3
qwt_math.h
1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
2  * Qwt Widget Library
3  * Copyright (C) 1997 Josef Wilgen
4  * Copyright (C) 2002 Uwe Rathmann
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the Qwt License, Version 1.0
8  *****************************************************************************/
9 
10 #ifndef QWT_MATH_H
11 #define QWT_MATH_H
12 
13 #include <math.h>
14 #include <qpoint.h>
15 #include "qwt_global.h"
16 #include "qwt_double_rect.h"
17 
18 #if QT_VERSION < 0x040000
19 
20 #define qwtMax QMAX
21 #define qwtMin QMIN
22 #define qwtAbs QABS
23 
24 #else // QT_VERSION >= 0x040000
25 
26 #define qwtMax qMax
27 #define qwtMin qMin
28 #define qwtAbs qAbs
29 
30 #endif
31 
32 #ifndef LOG10_2
33 #define LOG10_2 0.30102999566398119802 /* log10(2) */
34 #endif
35 
36 #ifndef LOG10_3
37 #define LOG10_3 0.47712125471966243540 /* log10(3) */
38 #endif
39 
40 #ifndef LOG10_5
41 #define LOG10_5 0.69897000433601885749 /* log10(5) */
42 #endif
43 
44 #ifndef M_2PI
45 #define M_2PI 6.28318530717958623200 /* 2 pi */
46 #endif
47 
48 #ifndef LOG_MIN
49 
50 #define LOG_MIN 1.0e-100
51 #endif
52 
53 #ifndef LOG_MAX
54 
55 #define LOG_MAX 1.0e100
56 #endif
57 
58 #ifndef M_E
59 #define M_E 2.7182818284590452354 /* e */
60 #endif
61 
62 #ifndef M_LOG2E
63 #define M_LOG2E 1.4426950408889634074 /* log_2 e */
64 #endif
65 
66 #ifndef M_LOG10E
67 #define M_LOG10E 0.43429448190325182765 /* log_10 e */
68 #endif
69 
70 #ifndef M_LN2
71 #define M_LN2 0.69314718055994530942 /* log_e 2 */
72 #endif
73 
74 #ifndef M_LN10
75 #define M_LN10 2.30258509299404568402 /* log_e 10 */
76 #endif
77 
78 #ifndef M_PI
79 #define M_PI 3.14159265358979323846 /* pi */
80 #endif
81 
82 #ifndef M_PI_2
83 #define M_PI_2 1.57079632679489661923 /* pi/2 */
84 #endif
85 
86 #ifndef M_PI_4
87 #define M_PI_4 0.78539816339744830962 /* pi/4 */
88 #endif
89 
90 #ifndef M_1_PI
91 #define M_1_PI 0.31830988618379067154 /* 1/pi */
92 #endif
93 
94 #ifndef M_2_PI
95 #define M_2_PI 0.63661977236758134308 /* 2/pi */
96 #endif
97 
98 #ifndef M_2_SQRTPI
99 #define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */
100 #endif
101 
102 #ifndef M_SQRT2
103 #define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
104 #endif
105 
106 #ifndef M_SQRT1_2
107 #define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
108 #endif
109 
110 QWT_EXPORT double qwtGetMin(const double *array, int size);
111 QWT_EXPORT double qwtGetMax(const double *array, int size);
112 
113 
115 inline int qwtSign(double x)
116 {
117  if (x > 0.0)
118  return 1;
119  else if (x < 0.0)
120  return (-1);
121  else
122  return 0;
123 }
124 
126 inline double qwtSqr(const double x)
127 {
128  return x*x;
129 }
130 
137 template <class T>
138 T qwtLim(const T& x, const T& x1, const T& x2)
139 {
140  T rv;
141  T xmin, xmax;
142 
143  xmin = qwtMin(x1, x2);
144  xmax = qwtMax(x1, x2);
145 
146  if ( x < xmin )
147  rv = xmin;
148  else if ( x > xmax )
149  rv = xmax;
150  else
151  rv = x;
152 
153  return rv;
154 }
155 
156 inline QPoint qwtPolar2Pos(const QPoint &pole,
157  double radius, double angle)
158 {
159  const double x = pole.x() + radius * ::cos(angle);
160  const double y = pole.y() - radius * ::sin(angle);
161 
162  return QPoint(qRound(x), qRound(y));
163 }
164 
165 inline QPoint qwtDegree2Pos(const QPoint &pole,
166  double radius, double angle)
167 {
168  return qwtPolar2Pos(pole, radius, angle / 180.0 * M_PI);
169 }
170 
171 inline QwtDoublePoint qwtPolar2Pos(const QwtDoublePoint &pole,
172  double radius, double angle)
173 {
174  const double x = pole.x() + radius * ::cos(angle);
175  const double y = pole.y() - radius * ::sin(angle);
176 
177  return QPoint(qRound(x), qRound(y));
178 }
179 
180 inline QwtDoublePoint qwtDegree2Pos(const QwtDoublePoint &pole,
181  double radius, double angle)
182 {
183  return qwtPolar2Pos(pole, radius, angle / 180.0 * M_PI);
184 }
185 
187 inline double qwtRound(double value)
188 {
189  return ::floor(value + 0.5); // MSVC has no ::round().
190 }
191 
192 #endif