rotate.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*!
  2. * \file lib/gis/rotate.c
  3. *
  4. * \brief GIS Library - rotate
  5. *
  6. * (C) 2001-2014 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public
  9. * License (>=v2). Read the file COPYING that comes with GRASS
  10. * for details.
  11. *
  12. * \author Hamish Bowman, Glynn Clements
  13. */
  14. #include <math.h>
  15. # define RpD ((2 * M_PI) / 360.) /* radians/degree */
  16. # define D2R(d) (double)(d * RpD) /* degrees->radians */
  17. # define R2D(d) (double)(d / RpD) /* radians->degrees */
  18. /*!
  19. * \brief Rotate point (double version)
  20. *
  21. * Given a point, angle, and origin, rotate the point around the origin
  22. * by the given angle. Coordinates and results are double prec floating point.
  23. *
  24. * \param X0 X component of origin (center of circle)
  25. * \param Y0 Y component of origin (center of circle)
  26. * \param[out] X1 X component of point to be rotated (variable is modified!)
  27. * \param[out] Y1 Y component of point to be rotated (variable is modified!)
  28. * \param angle in degrees, measured CCW from east
  29. */
  30. void G_rotate_around_point(double X0, double Y0, double *X1, double *Y1,
  31. double angle)
  32. {
  33. double dx = *X1 - X0;
  34. double dy = *Y1 - Y0;
  35. double c = cos(D2R(angle));
  36. double s = sin(D2R(angle));
  37. double dx1 = dx * c - dy * s;
  38. double dy1 = dx * s + dy * c;
  39. *X1 = X0 + dx1;
  40. *Y1 = Y0 + dy1;
  41. }
  42. /*!
  43. * \brief Rotate point (int version)
  44. *
  45. * Given a point, angle, and origin, rotate the point around the origin
  46. * by the given angle. Coordinates are given in integer and results are rounded
  47. * back to integer.
  48. *
  49. * \param X0 X component of origin (center of circle)
  50. * \param Y0 Y component of origin (center of circle)
  51. * \param[out] X1 X component of point to be rotated (variable is modified!)
  52. * \param[out] Y1 Y component of point to be rotated (variable is modified!)
  53. * \param angle in degrees, measured CCW from east
  54. */
  55. void G_rotate_around_point_int(int X0, int Y0, int *X1, int *Y1, double angle)
  56. {
  57. double x = (double)*X1;
  58. double y = (double)*Y1;
  59. if (angle == 0.0)
  60. return;
  61. G_rotate_around_point((double)X0, (double)Y0, &x, &y, angle);
  62. *X1 = (int)floor(x + 0.5);
  63. *Y1 = (int)floor(y + 0.5);
  64. }