1
/* vector.c: vector/point operations.
3
Copyright (C) 1992 Free Software Foundation, Inc.
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; either version 2, or (at your option)
10
This program 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
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
30
/* Given the point COORD, return the corresponding vector. */
33
make_vector (const real_coordinate_type c)
44
/* And the converse: given a vector, return the corresponding point. */
47
vector_to_point (const vector_type v)
49
real_coordinate_type coord;
59
magnitude (const vector_type v)
61
return hypot (v.dx, v.dy);
66
normalize (const vector_type v)
69
real m = magnitude (v);
81
Vadd (const vector_type v1, const vector_type v2)
85
new_v.dx = v1.dx + v2.dx;
86
new_v.dy = v1.dy + v2.dy;
93
Vdot (const vector_type v1, const vector_type v2)
95
return v1.dx * v2.dx + v1.dy * v2.dy;
100
Vmult_scalar (const vector_type v, const real r)
111
/* Given the IN_VECTOR and OUT_VECTOR, return the angle between them in
112
degrees, in the range zero to 180. */
115
Vangle (const vector_type in_vector, const vector_type out_vector)
117
vector_type v1 = normalize (in_vector);
118
vector_type v2 = normalize (out_vector);
120
return my_acosd (Vdot (v2, v1));
125
Vadd_point (const real_coordinate_type c, const vector_type v)
127
real_coordinate_type new_c;
129
new_c.x = c.x + v.dx;
130
new_c.y = c.y + v.dy;
136
Vsubtract_point (const real_coordinate_type c, const vector_type v)
138
real_coordinate_type new_c;
140
new_c.x = c.x - v.dx;
141
new_c.y = c.y - v.dy;
147
Vadd_int_point (const coordinate_type c, const vector_type v)
151
a.x = SROUND ((real) c.x + v.dx);
152
a.y = SROUND ((real) c.y + v.dy);
158
Vabs (const vector_type v)
162
new_v.dx = fabs (v.dx);
163
new_v.dy = fabs (v.dy);
168
/* Operations on points. */
171
Psubtract (const real_coordinate_type c1, const real_coordinate_type c2)
181
/* Operations on integer points. */
184
IPsubtract (const coordinate_type coord1, const coordinate_type coord2)
188
v.dx = coord1.x - coord2.x;
189
v.dy = coord1.y - coord2.y;
196
IPsubtractP (const coordinate_type c1, const coordinate_type c2)
208
IPadd (const coordinate_type c1, const coordinate_type c2)
220
IPmult_scalar (const coordinate_type c, const int i)
232
IPmult_real (const coordinate_type c, const real r)
234
real_coordinate_type a;
244
IPequal (const coordinate_type c1, const coordinate_type c2)
246
return c1.x == c2.x && c1.y == c2.y;