SplashMath.h 1.73 KB
Newer Older
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1 2 3 4 5 6 7 8 9
//========================================================================
//
// SplashMath.h
//
//========================================================================

#ifndef SPLASHMATH_H
#define SPLASHMATH_H

10
#if USE_FIXEDPOINT
11 12
#include "FixedPoint.h"
#else
Kristian Høgsberg's avatar
Kristian Høgsberg committed
13
#include <math.h>
14
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
15 16 17
#include "SplashTypes.h"

static inline SplashCoord splashAbs(SplashCoord x) {
18 19 20
#if USE_FIXEDPOINT
  return FixedPoint::abs(x);
#else
Kristian Høgsberg's avatar
Kristian Høgsberg committed
21
  return fabs(x);
22
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
23 24 25
}

static inline int splashFloor(SplashCoord x) {
26 27 28 29 30
  #if USE_FIXEDPOINT
    return FixedPoint::floor(x);
  #else
    return (int)floor(x);
  #endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
31 32 33
}

static inline int splashCeil(SplashCoord x) {
34 35 36
#if USE_FIXEDPOINT
  return FixedPoint::ceil(x);
#else
Kristian Høgsberg's avatar
Kristian Høgsberg committed
37
  return (int)ceil(x);
38
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
39 40 41
}

static inline int splashRound(SplashCoord x) {
42 43 44
#if USE_FIXEDPOINT
  return FixedPoint::round(x);
#else
Kristian Høgsberg's avatar
Kristian Høgsberg committed
45
  return (int)floor(x + 0.5);
46
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
47 48 49
}

static inline SplashCoord splashSqrt(SplashCoord x) {
50 51 52
#if USE_FIXEDPOINT
  return FixedPoint::sqrt(x);
#else
Kristian Høgsberg's avatar
Kristian Høgsberg committed
53
  return sqrt(x);
54
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
55 56 57
}

static inline SplashCoord splashPow(SplashCoord x, SplashCoord y) {
58 59 60
#if USE_FIXEDPOINT
  return FixedPoint::pow(x, y);
#else
Kristian Høgsberg's avatar
Kristian Høgsberg committed
61
  return pow(x, y);
62
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
63 64 65 66 67 68 69
}

static inline SplashCoord splashDist(SplashCoord x0, SplashCoord y0,
				     SplashCoord x1, SplashCoord y1) {
  SplashCoord dx, dy;
  dx = x1 - x0;
  dy = y1 - y0;
70
#if USE_FIXEDPOINT
71 72 73 74 75 76 77 78 79 80 81 82
  // this handles the situation where dx*dx or dy*dy is too large to
  // fit in the 16.16 fixed point format
  SplashCoord dxa, dya;
  dxa = splashAbs(dx);
  dya = splashAbs(dy);
  if (dxa == 0 && dya == 0) {
    return 0;
  } else if (dxa > dya) {
    return dxa * FixedPoint::sqrt(dya / dxa + 1);
  } else {
    return dya * FixedPoint::sqrt(dxa / dya + 1);
  }
83
#else
Kristian Høgsberg's avatar
Kristian Høgsberg committed
84
  return sqrt(dx * dx + dy * dy);
85
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
86 87 88
}

#endif