~ubuntu-branches/ubuntu/hoary/gimp/hoary

« back to all changes in this revision

Viewing changes to plug-ins/sel2path/vector.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-04-04 14:51:23 UTC
  • Revision ID: james.westby@ubuntu.com-20050404145123-9py049eeelfymur8
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* vector.c: vector/point operations.
 
2
 
 
3
Copyright (C) 1992 Free Software Foundation, Inc.
 
4
 
 
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)
 
8
any later version.
 
9
 
 
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.
 
14
 
 
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.  */
 
18
 
 
19
#include <glib.h>
 
20
 
 
21
#include <math.h>
 
22
#include <assert.h>
 
23
 
 
24
#include "global.h"
 
25
#include "config.h"
 
26
 
 
27
#include "vector.h"
 
28
 
 
29
 
 
30
/* Given the point COORD, return the corresponding vector.  */
 
31
 
 
32
vector_type
 
33
make_vector (const real_coordinate_type c)
 
34
{
 
35
  vector_type v;
 
36
 
 
37
  v.dx = c.x;
 
38
  v.dy = c.y;
 
39
 
 
40
  return v;
 
41
}
 
42
 
 
43
 
 
44
/* And the converse: given a vector, return the corresponding point.  */
 
45
 
 
46
real_coordinate_type
 
47
vector_to_point (const vector_type v)
 
48
{
 
49
  real_coordinate_type coord;
 
50
 
 
51
  coord.x = v.dx;
 
52
  coord.y = v.dy;
 
53
 
 
54
  return coord;
 
55
}
 
56
 
 
57
 
 
58
real
 
59
magnitude (const vector_type v)
 
60
{
 
61
  return hypot (v.dx, v.dy);
 
62
}
 
63
 
 
64
 
 
65
vector_type
 
66
normalize (const vector_type v)
 
67
{
 
68
  vector_type new_v;
 
69
  real m = magnitude (v);
 
70
 
 
71
  assert (m > 0.0);
 
72
 
 
73
  new_v.dx = v.dx / m;
 
74
  new_v.dy = v.dy / m;
 
75
 
 
76
  return new_v;
 
77
}
 
78
 
 
79
 
 
80
vector_type
 
81
Vadd (const vector_type v1, const vector_type v2)
 
82
{
 
83
  vector_type new_v;
 
84
 
 
85
  new_v.dx = v1.dx + v2.dx;
 
86
  new_v.dy = v1.dy + v2.dy;
 
87
 
 
88
  return new_v;
 
89
}
 
90
 
 
91
 
 
92
real
 
93
Vdot (const vector_type v1, const vector_type v2)
 
94
{
 
95
  return v1.dx * v2.dx + v1.dy * v2.dy;
 
96
}
 
97
 
 
98
 
 
99
vector_type
 
100
Vmult_scalar (const vector_type v, const real r)
 
101
{
 
102
  vector_type new_v;
 
103
 
 
104
  new_v.dx = v.dx * r;
 
105
  new_v.dy = v.dy * r;
 
106
 
 
107
  return new_v;
 
108
}
 
109
 
 
110
 
 
111
/* Given the IN_VECTOR and OUT_VECTOR, return the angle between them in
 
112
   degrees, in the range zero to 180.  */
 
113
   
 
114
real
 
115
Vangle (const vector_type in_vector, const vector_type out_vector)
 
116
{
 
117
  vector_type v1 = normalize (in_vector);
 
118
  vector_type v2 = normalize (out_vector);
 
119
 
 
120
  return my_acosd (Vdot (v2, v1));
 
121
}
 
122
 
 
123
 
 
124
real_coordinate_type
 
125
Vadd_point (const real_coordinate_type c, const vector_type v)
 
126
{
 
127
  real_coordinate_type new_c;
 
128
 
 
129
  new_c.x = c.x + v.dx;
 
130
  new_c.y = c.y + v.dy;
 
131
  return new_c;
 
132
}
 
133
 
 
134
 
 
135
real_coordinate_type
 
136
Vsubtract_point (const real_coordinate_type c, const vector_type v)
 
137
{
 
138
  real_coordinate_type new_c;
 
139
 
 
140
  new_c.x = c.x - v.dx;
 
141
  new_c.y = c.y - v.dy;
 
142
  return new_c;
 
143
}
 
144
 
 
145
 
 
146
coordinate_type
 
147
Vadd_int_point (const coordinate_type c, const vector_type v)
 
148
{
 
149
  coordinate_type a;
 
150
  
 
151
  a.x = SROUND ((real) c.x + v.dx);
 
152
  a.y = SROUND ((real) c.y + v.dy);
 
153
  return a;
 
154
}
 
155
 
 
156
 
 
157
vector_type
 
158
Vabs (const vector_type v)
 
159
{
 
160
  vector_type new_v;
 
161
  
 
162
  new_v.dx = fabs (v.dx);
 
163
  new_v.dy = fabs (v.dy);
 
164
  return new_v;
 
165
}
 
166
 
 
167
 
 
168
/* Operations on points.  */
 
169
 
 
170
vector_type
 
171
Psubtract (const real_coordinate_type c1, const real_coordinate_type c2)
 
172
{
 
173
  vector_type v;
 
174
 
 
175
  v.dx = c1.x - c2.x;
 
176
  v.dy = c1.y - c2.y;
 
177
 
 
178
  return v;
 
179
}
 
180
 
 
181
/* Operations on integer points.  */
 
182
 
 
183
vector_type
 
184
IPsubtract (const coordinate_type coord1, const coordinate_type coord2)
 
185
{
 
186
  vector_type v;
 
187
 
 
188
  v.dx = coord1.x - coord2.x;
 
189
  v.dy = coord1.y - coord2.y;
 
190
 
 
191
  return v;
 
192
}
 
193
 
 
194
 
 
195
coordinate_type
 
196
IPsubtractP (const coordinate_type c1, const coordinate_type c2)
 
197
{
 
198
  coordinate_type c;
 
199
  
 
200
  c.x = c1.x - c2.x;
 
201
  c.y = c1.y - c2.y;
 
202
  
 
203
  return c;
 
204
}
 
205
 
 
206
 
 
207
coordinate_type
 
208
IPadd (const coordinate_type c1, const coordinate_type c2)
 
209
{
 
210
  coordinate_type c;
 
211
  
 
212
  c.x = c1.x + c2.x;
 
213
  c.y = c1.y + c2.y;
 
214
  
 
215
  return c;
 
216
}
 
217
 
 
218
 
 
219
coordinate_type
 
220
IPmult_scalar (const coordinate_type c, const int i)
 
221
{
 
222
  coordinate_type a;
 
223
  
 
224
  a.x = c.x * i;
 
225
  a.y = c.y * i;
 
226
  
 
227
  return a;
 
228
}
 
229
 
 
230
 
 
231
real_coordinate_type
 
232
IPmult_real (const coordinate_type c, const real r)
 
233
{
 
234
  real_coordinate_type a;
 
235
 
 
236
  a.x = c.x * r;
 
237
  a.y = c.y * r;
 
238
 
 
239
  return a;
 
240
}
 
241
 
 
242
 
 
243
boolean
 
244
IPequal (const coordinate_type c1, const coordinate_type c2)
 
245
{
 
246
  return c1.x == c2.x && c1.y == c2.y;
 
247
}