GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-535c39c9fc
rotate.c
Go to the documentation of this file.
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 
15 #include <math.h>
16 
17 #define RpD ((2 * M_PI) / 360.) /* radians/degree */
18 #define D2R(d) (double)(d * RpD) /* degrees->radians */
19 #define R2D(d) (double)(d / RpD) /* radians->degrees */
20 
21 /*!
22  * \brief Rotate point (double version)
23  *
24  * Given a point, angle, and origin, rotate the point around the origin
25  * by the given angle. Coordinates and results are double prec floating point.
26  *
27  * \param X0 X component of origin (center of circle)
28  * \param Y0 Y component of origin (center of circle)
29  * \param[out] X1 X component of point to be rotated (variable is modified!)
30  * \param[out] Y1 Y component of point to be rotated (variable is modified!)
31  * \param angle in degrees, measured CCW from east
32  */
33 void G_rotate_around_point(double X0, double Y0, double *X1, double *Y1,
34  double angle)
35 {
36  double dx = *X1 - X0;
37  double dy = *Y1 - Y0;
38  double c = cos(D2R(angle));
39  double s = sin(D2R(angle));
40  double dx1 = dx * c - dy * s;
41  double dy1 = dx * s + dy * c;
42 
43  *X1 = X0 + dx1;
44  *Y1 = Y0 + dy1;
45 }
46 
47 /*!
48  * \brief Rotate point (int version)
49  *
50  * Given a point, angle, and origin, rotate the point around the origin
51  * by the given angle. Coordinates are given in integer and results are rounded
52  * back to integer.
53  *
54  * \param X0 X component of origin (center of circle)
55  * \param Y0 Y component of origin (center of circle)
56  * \param[out] X1 X component of point to be rotated (variable is modified!)
57  * \param[out] Y1 Y component of point to be rotated (variable is modified!)
58  * \param angle in degrees, measured CCW from east
59  */
60 void G_rotate_around_point_int(int X0, int Y0, int *X1, int *Y1, double angle)
61 {
62  double x = (double)*X1;
63  double y = (double)*Y1;
64 
65  if (angle == 0.0)
66  return;
67 
68  G_rotate_around_point((double)X0, (double)Y0, &x, &y, angle);
69 
70  *X1 = (int)floor(x + 0.5);
71  *Y1 = (int)floor(y + 0.5);
72 }
#define D2R(d)
Definition: rotate.c:18
void G_rotate_around_point_int(int X0, int Y0, int *X1, int *Y1, double angle)
Rotate point (int version)
Definition: rotate.c:60
void G_rotate_around_point(double X0, double Y0, double *X1, double *Y1, double angle)
Rotate point (double version)
Definition: rotate.c:33
#define x