For code maintainability, it is better to have a single definition of the
constant than many ones dispatched in many places. In addition, we try to
count on the constant M_PI that can be defined by the header <math.h> if
possible because it may have the best accuracy for the platform.

Signed-off-by: Christophe CURIS <christophe.cu...@free.fr>
---
 WINGs/wcolorpanel.c | 15 ++++++---------
 m4/wm_libmath.m4    | 19 +++++++++++++++++++
 src/wconfig.h.in    |  1 -
 wrlib/rotate.c      | 19 ++++++++-----------
 wrlib/scale.c       |  7 +------
 5 files changed, 34 insertions(+), 27 deletions(-)

diff --git a/WINGs/wcolorpanel.c b/WINGs/wcolorpanel.c
index 12d5448..f23c91d 100644
--- a/WINGs/wcolorpanel.c
+++ b/WINGs/wcolorpanel.c
@@ -265,9 +265,6 @@ enum {
 #define        COLORWHEEL_PART         1
 #define        CUSTOMPALETTE_PART      2
 
-#ifndef M_PI
-#define M_PI 3.14159265358979323846
-#endif
 
 static char *generateNewFilename(const char *curName);
 static void convertCPColor(CPColor * color);
@@ -1951,7 +1948,7 @@ static void wheelInitMatrix(W_ColorPanel * panel)
                        if (sat < 256) {
                                if (xcor != 0)
                                        dhue[0] = rint(atan((double)ycor / 
(double)xcor) *
-                                                      (180.0 / M_PI)) + (xcor 
< 0 ? 180.0 : 0.0);
+                                                      (180.0 / WM_PI)) + (xcor 
< 0 ? 180.0 : 0.0);
                                else
                                        dhue[0] = 270;
 
@@ -2237,7 +2234,7 @@ static void wheelPositionSelectionOutBounds(W_ColorPanel 
* panel, int x, int y)
        panel->color.hsv.value = 255 - 
WMGetSliderValue(panel->wheelBrightnessS);
 
        if (xcor != 0)
-               hue = rint(atan(-(double)ycor / (double)xcor) * (180.0 / M_PI));
+               hue = rint(atan(-(double)ycor / (double)xcor) * (180.0 / 
WM_PI));
        else {
                if (ycor < 0)
                        hue = 90;
@@ -2257,9 +2254,9 @@ static void wheelPositionSelectionOutBounds(W_ColorPanel 
* panel, int x, int y)
 
        wheelUndrawSelection(panel);
 
-       panel->colx = 2 + rint((colorWheelSize * (1.0 + 
cos(panel->color.hsv.hue * (M_PI / 180.0)))) / 2.0);
+       panel->colx = 2 + rint((colorWheelSize * (1.0 + 
cos(panel->color.hsv.hue * (WM_PI / 180.0)))) / 2.0);
        /* "+2" because of "colorWheelSize + 4" */
-       panel->coly = 2 + rint((colorWheelSize * (1.0 + 
sin(-panel->color.hsv.hue * (M_PI / 180.0)))) / 2.0);
+       panel->coly = 2 + rint((colorWheelSize * (1.0 + 
sin(-panel->color.hsv.hue * (WM_PI / 180.0)))) / 2.0);
 
        wheelUpdateSelection(panel);
        cpColor = panel->color;
@@ -3357,10 +3354,10 @@ static void wheelInit(W_ColorPanel * panel)
 
        panel->colx = 2 + rint((colorWheelSize / 2.0) *
                               (1 + (panel->color.hsv.saturation / 255.0) *
-                               cos(panel->color.hsv.hue * M_PI / 180.0)));
+                               cos(panel->color.hsv.hue * WM_PI / 180.0)));
        panel->coly = 2 + rint((colorWheelSize / 2.0) *
                               (1 + (panel->color.hsv.saturation / 255.0) *
-                               sin(-panel->color.hsv.hue * M_PI / 180.0)));
+                               sin(-panel->color.hsv.hue * WM_PI / 180.0)));
 
        wheelCalculateValues(panel, panel->color.hsv.value);
 
diff --git a/m4/wm_libmath.m4 b/m4/wm_libmath.m4
index 8d056a2..d21493f 100644
--- a/m4/wm_libmath.m4
+++ b/m4/wm_libmath.m4
@@ -30,5 +30,24 @@ AC_CHECK_FUNC(atan,
         [LIBM=-lm],
         [AC_MSG_WARN(Could not find Math library, you may experience problems)
          LIBM=] )] ) dnl
+AC_CACHE_CHECK([if M_PI is defined in math.h], [wm_cv_libm_pi],
+    [wm_cv_libm_pi="no"
+     wm_save_CFLAGS="$CFLAGS"
+     for wm_arg in dnl
+       "% yes" dnl natively available (Unix98 compliant?)
+       "-D_XOPEN_SOURCE=500" ; dnl Explicit request
+     do
+       AS_IF([wm_fn_lib_try_compile "math.h" "double val;" "val = M_PI" dnl
+                  "`echo "$wm_arg" | sed -e 's, *%.*$,,' `"],
+           [wm_cv_libm_pi="`echo "$wm_arg" | sed -e 's,^.*% *,,' `" ; break])
+     done
+     CFLAGS="$wm_save_CFLAGS"])
+AS_IF([test "x$wm_cv_libm_pi" = "xno"],
+    [AC_DEFINE([WM_PI], [(3.14159265358979323846)],
+        [Defines how to access the value of Pi])],
+    [AC_DEFINE([WM_PI], [(M_PI)],
+        [Defines how to access the value of Pi])
+     AS_IF([test "x$wm_cv_libm_pi" != "xyes"],
+        [CFLAGS="$CFLAGS $wm_cv_libm_pi"]) ])
 AC_SUBST(LIBM) dnl
 ])
diff --git a/src/wconfig.h.in b/src/wconfig.h.in
index c1548ee..2930bcb 100644
--- a/src/wconfig.h.in
+++ b/src/wconfig.h.in
@@ -334,7 +334,6 @@
 
 /* number of window shortcuts */
 #define MAX_WINDOW_SHORTCUTS      10
-#define WM_PI                     3.14159265358979323846
 #define MIN_TITLEFONT_HEIGHT(h)   ((h)>14 ? (h) : 14)
 #define TITLEBAR_HEIGHT           18  /* window's titlebar height */
 #define RESIZEBAR_HEIGHT          8   /* height of the resizebar */
diff --git a/wrlib/rotate.c b/wrlib/rotate.c
index 84b34b7..482b317 100644
--- a/wrlib/rotate.c
+++ b/wrlib/rotate.c
@@ -33,9 +33,6 @@
 
 #include <math.h>
 
-#ifndef PI
-#define PI 3.14159265358979323846
-#endif
 
 static RImage *rotate_image_90(RImage *source);
 static RImage *rotate_image_270(RImage *source);
@@ -347,13 +344,13 @@ static RImage *rotate_image_any(RImage *source, float 
angle)
        if (angle > 180.0)
                angle -= 180.0;
 
-       angle = (angle * PI) / 180.0;
+       angle = (angle * WM_PI) / 180.0;
 
        nwidth = ceil(abs(cos(angle) * image->width))
-           + ceil(abs(cos(PI / 2 - angle) * image->width));
+           + ceil(abs(cos(WM_PI / 2 - angle) * image->width));
 
        nheight = ceil(abs(sin(angle) * image->height))
-           + ceil(abs(cos(PI / 2 - angle) * image->height));
+           + ceil(abs(cos(WM_PI / 2 - angle) * image->height));
 
        img = RCreateImage(nwidth, nheight, True);
        if (!img)
@@ -362,16 +359,16 @@ static RImage *rotate_image_any(RImage *source, float 
angle)
        src = image->data;
        dst = img->data;
 
-       x1 = floor(abs(cos(PI / 2 - angle) * image->width));
+       x1 = floor(abs(cos(WM_PI / 2 - angle) * image->width));
        y1 = 0;
 
        x2 = 0;
-       y2 = floor(abs(sin(PI / 2 - angle) * image->width));
+       y2 = floor(abs(sin(WM_PI / 2 - angle) * image->width));
 
        xx = floor(abs(cos(angle) * image->height)) - 1;
        yy = nheight - 1;
 
-       printf("%ix%i, %i %i     %i %i %i\n", nwidth, nheight, x1, y1, x2, y2, 
(int)((angle * 180.0) / PI));
+       printf("%ix%i, %i %i     %i %i %i\n", nwidth, nheight, x1, y1, x2, y2, 
(int)((angle * 180.0) / WM_PI));
 
        dx = abs(x2 - x1);
        dy = abs(y2 - y1);
@@ -414,8 +411,8 @@ static RImage *rotate_image_any(RImage *source, float angle)
                p = dpr - dy;
 
                while (dy-- >= 0) {
-                       xx = abs(x1 * sin(angle * PI / 180.0));
-                       yy = abs(y1 * cos(angle * PI / 180.0));
+                       xx = abs(x1 * sin(angle * WM_PI / 180.0));
+                       yy = abs(y1 * cos(angle * WM_PI / 180.0));
 
                        copyLine(x1, y1, xx, yy, nwidth, image->format, dst, 
&src);
 
diff --git a/wrlib/scale.c b/wrlib/scale.c
index eafe40f..0f5952c 100644
--- a/wrlib/scale.c
+++ b/wrlib/scale.c
@@ -27,11 +27,6 @@
 #include <string.h>
 #include <X11/Xlib.h>
 #include <math.h>
-
-#ifndef PI
-#define PI 3.14159265358979323846
-#endif
-
 #include <assert.h>
 
 #include "wraster.h"
@@ -200,7 +195,7 @@ static double sinc(double x)
         * The goal was to avoid a Divide-by-0 error, now we also
         * avoid a +/-inf result too
         */
-       x *= PI;
+       x *= WM_PI;
        if (x > 1.0E-9)
                return (sin(x) / x);
        return (1.0);
-- 
2.1.3


-- 
To unsubscribe, send mail to wmaker-dev-unsubscr...@lists.windowmaker.org.

Reply via email to