vector.c 4.02 KB
Newer Older
1
/* vector.c: vector/point operations.
Sven Neumann's avatar
Sven Neumann committed
2 3 4
 *
 * Copyright (C) 1992 Free Software Foundation, Inc.
 *
5
 * This program is free software: you can redistribute it and/or modify
Sven Neumann's avatar
Sven Neumann committed
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3, or (at your option)
Sven Neumann's avatar
Sven Neumann committed
8 9 10 11 12 13 14 15
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
Sven Neumann's avatar
Sven Neumann committed
17
 */
18

Sven Neumann's avatar
Sven Neumann committed
19
#include "config.h"
20

21 22
#include <glib.h>

23 24 25 26 27 28 29 30 31 32 33
#include <math.h>
#include <assert.h>

#include "global.h"
#include "config.h"

#include "vector.h"


/* Given the point COORD, return the corresponding vector.  */

34
vector_type
35 36 37 38 39 40 41 42 43 44 45 46 47
make_vector (const real_coordinate_type c)
{
  vector_type v;

  v.dx = c.x;
  v.dy = c.y;

  return v;
}


/* And the converse: given a vector, return the corresponding point.  */

48
real_coordinate_type
49 50 51 52 53 54 55 56 57 58 59
vector_to_point (const vector_type v)
{
  real_coordinate_type coord;

  coord.x = v.dx;
  coord.y = v.dy;

  return coord;
}


60
real
61 62 63 64 65 66
magnitude (const vector_type v)
{
  return hypot (v.dx, v.dy);
}


67
vector_type
68 69 70 71 72 73 74 75 76 77 78 79 80 81
normalize (const vector_type v)
{
  vector_type new_v;
  real m = magnitude (v);

  assert (m > 0.0);

  new_v.dx = v.dx / m;
  new_v.dy = v.dy / m;

  return new_v;
}


82
vector_type
83 84 85 86 87 88 89 90 91 92 93
Vadd (const vector_type v1, const vector_type v2)
{
  vector_type new_v;

  new_v.dx = v1.dx + v2.dx;
  new_v.dy = v1.dy + v2.dy;

  return new_v;
}


94
real
95 96 97 98 99 100
Vdot (const vector_type v1, const vector_type v2)
{
  return v1.dx * v2.dx + v1.dy * v2.dy;
}


101
vector_type
102 103 104 105 106 107 108 109 110 111 112 113 114
Vmult_scalar (const vector_type v, const real r)
{
  vector_type new_v;

  new_v.dx = v.dx * r;
  new_v.dy = v.dy * r;

  return new_v;
}


/* Given the IN_VECTOR and OUT_VECTOR, return the angle between them in
   degrees, in the range zero to 180.  */
115

116
real
117 118 119 120 121
Vangle (const vector_type in_vector, const vector_type out_vector)
{
  vector_type v1 = normalize (in_vector);
  vector_type v2 = normalize (out_vector);

122
  return my_acosd (Vdot (v2, v1));
123 124 125
}


126
real_coordinate_type
127 128 129 130 131 132 133 134 135 136
Vadd_point (const real_coordinate_type c, const vector_type v)
{
  real_coordinate_type new_c;

  new_c.x = c.x + v.dx;
  new_c.y = c.y + v.dy;
  return new_c;
}


137
real_coordinate_type
138 139 140 141 142 143 144 145 146 147
Vsubtract_point (const real_coordinate_type c, const vector_type v)
{
  real_coordinate_type new_c;

  new_c.x = c.x - v.dx;
  new_c.y = c.y - v.dy;
  return new_c;
}


148
coordinate_type
149 150 151
Vadd_int_point (const coordinate_type c, const vector_type v)
{
  coordinate_type a;
152

Sven Neumann's avatar
Sven Neumann committed
153 154
  a.x = SROUND ((real) c.x + v.dx);
  a.y = SROUND ((real) c.y + v.dy);
155 156 157 158
  return a;
}


159
vector_type
160 161 162
Vabs (const vector_type v)
{
  vector_type new_v;
163

164 165 166 167 168 169 170 171
  new_v.dx = fabs (v.dx);
  new_v.dy = fabs (v.dy);
  return new_v;
}


/* Operations on points.  */

172
vector_type
173 174 175 176 177 178 179 180 181 182 183 184
Psubtract (const real_coordinate_type c1, const real_coordinate_type c2)
{
  vector_type v;

  v.dx = c1.x - c2.x;
  v.dy = c1.y - c2.y;

  return v;
}

/* Operations on integer points.  */

185
vector_type
186 187 188 189 190 191 192 193 194 195 196
IPsubtract (const coordinate_type coord1, const coordinate_type coord2)
{
  vector_type v;

  v.dx = coord1.x - coord2.x;
  v.dy = coord1.y - coord2.y;

  return v;
}


197
coordinate_type
198 199 200
IPsubtractP (const coordinate_type c1, const coordinate_type c2)
{
  coordinate_type c;
201

202 203
  c.x = c1.x - c2.x;
  c.y = c1.y - c2.y;
204

205 206 207 208
  return c;
}


209
coordinate_type
210 211 212
IPadd (const coordinate_type c1, const coordinate_type c2)
{
  coordinate_type c;
213

214 215
  c.x = c1.x + c2.x;
  c.y = c1.y + c2.y;
216

217 218 219 220
  return c;
}


221
coordinate_type
222 223 224
IPmult_scalar (const coordinate_type c, const int i)
{
  coordinate_type a;
225

226 227
  a.x = c.x * i;
  a.y = c.y * i;
228

229 230 231 232
  return a;
}


233
real_coordinate_type
234 235 236 237 238 239 240 241 242 243 244
IPmult_real (const coordinate_type c, const real r)
{
  real_coordinate_type a;

  a.x = c.x * r;
  a.y = c.y * r;

  return a;
}


245
boolean
246 247 248 249
IPequal (const coordinate_type c1, const coordinate_type c2)
{
  return c1.x == c2.x && c1.y == c2.y;
}