WMath.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
  2. /*
  3. Part of the Wiring project - http://wiring.org.co
  4. Copyright (c) 2004-06 Hernando Barragan
  5. Modified 13 August 2006, David A. Mellis for Arduino - http://www.arduino.cc/
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General
  15. Public License along with this library; if not, write to the
  16. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  17. Boston, MA 02111-1307 USA
  18. $Id$
  19. */
  20. extern "C" {
  21. #include "stdlib.h"
  22. }
  23. void randomSeed(unsigned int seed)
  24. {
  25. if (seed != 0) {
  26. srandom(seed);
  27. }
  28. }
  29. long random(long howbig)
  30. {
  31. if (howbig == 0) {
  32. return 0;
  33. }
  34. return random() % howbig;
  35. }
  36. long random(long howsmall, long howbig)
  37. {
  38. if (howsmall >= howbig) {
  39. return howsmall;
  40. }
  41. long diff = howbig - howsmall;
  42. return random(diff) + howsmall;
  43. }
  44. long map(long x, long in_min, long in_max, long out_min, long out_max)
  45. {
  46. return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  47. }
  48. unsigned int makeWord(unsigned int w) { return w; }
  49. unsigned int makeWord(unsigned char h, unsigned char l) { return (h << 8) | l; }