1999-07-10 05:52:00 +08:00
|
|
|
/* vector.h: operations on vectors and points.
|
2007-04-12 23:17:30 +08:00
|
|
|
*
|
|
|
|
* Copyright (C) 1992 Free Software Foundation, Inc.
|
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
2007-04-12 23:17:30 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-18 06:28:01 +08:00
|
|
|
* the Free Software Foundation; either version 3, or (at your option)
|
2007-04-12 23:17:30 +08:00
|
|
|
* 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
|
2018-07-12 05:27:07 +08:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2007-04-12 23:17:30 +08:00
|
|
|
*/
|
1999-07-10 05:52:00 +08:00
|
|
|
|
|
|
|
#ifndef VECTOR_H
|
|
|
|
#define VECTOR_H
|
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
|
|
|
|
/* Our vectors are represented as displacements along the x and y axes. */
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
real dx, dy;
|
|
|
|
} vector_type;
|
|
|
|
|
|
|
|
|
|
|
|
/* Consider a point as a vector from the origin. */
|
2000-07-31 02:37:33 +08:00
|
|
|
extern vector_type make_vector (const real_coordinate_type);
|
1999-07-10 05:52:00 +08:00
|
|
|
|
|
|
|
/* And a vector as a point, i.e., a displacement from the origin. */
|
2000-07-31 02:37:33 +08:00
|
|
|
extern real_coordinate_type vector_to_point (const vector_type);
|
1999-07-10 05:52:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* Definitions for these common operations can be found in any decent
|
|
|
|
linear algebra book, and most calculus books. */
|
|
|
|
|
2000-07-31 02:37:33 +08:00
|
|
|
extern real magnitude (const vector_type);
|
|
|
|
extern vector_type normalize (const vector_type);
|
1999-07-10 05:52:00 +08:00
|
|
|
|
2000-07-31 02:37:33 +08:00
|
|
|
extern vector_type Vadd (const vector_type, const vector_type);
|
|
|
|
extern real Vdot (const vector_type, const vector_type);
|
|
|
|
extern vector_type Vmult_scalar (const vector_type, const real);
|
|
|
|
extern real Vangle (const vector_type in, const vector_type out);
|
1999-07-10 05:52:00 +08:00
|
|
|
|
|
|
|
/* These operations could have been named `P..._vector' just as well as
|
|
|
|
V..._point, so we may as well allow both names. */
|
|
|
|
#define Padd_vector Vadd_point
|
2000-07-31 02:37:33 +08:00
|
|
|
extern real_coordinate_type Vadd_point
|
1999-07-10 05:52:00 +08:00
|
|
|
(const real_coordinate_type, const vector_type);
|
|
|
|
|
|
|
|
#define Psubtract_vector Vsubtract_point
|
2000-07-31 02:37:33 +08:00
|
|
|
extern real_coordinate_type Vsubtract_point
|
1999-07-10 05:52:00 +08:00
|
|
|
(const real_coordinate_type, const vector_type);
|
|
|
|
|
|
|
|
/* This returns the rounded sum. */
|
|
|
|
#define IPadd_vector Vadd_int_point
|
2000-07-31 02:37:33 +08:00
|
|
|
extern coordinate_type Vadd_int_point
|
1999-07-10 05:52:00 +08:00
|
|
|
(const coordinate_type, const vector_type);
|
|
|
|
|
|
|
|
/* Take the absolute value of both components. */
|
2000-07-31 02:37:33 +08:00
|
|
|
extern vector_type Vabs (const vector_type);
|
1999-07-10 05:52:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* Operations on points with real coordinates. It is not orthogonal,
|
|
|
|
but more convenient, to have the subtraction operator return a
|
|
|
|
vector, and the addition operator return a point. */
|
2000-07-31 02:37:33 +08:00
|
|
|
extern vector_type Psubtract
|
1999-07-10 05:52:00 +08:00
|
|
|
(const real_coordinate_type, const real_coordinate_type);
|
|
|
|
|
|
|
|
/* These are heavily used in spline fitting, so we define them as macros
|
|
|
|
instead of functions. */
|
|
|
|
#define Padd(rc1, rc2) \
|
|
|
|
((real_coordinate_type) { (rc1).x + (rc2).x, (rc1).y + (rc2).y })
|
|
|
|
#define Pmult_scalar(rc, r) \
|
|
|
|
((real_coordinate_type) { (rc).x * (r), (rc).y * (r) })
|
|
|
|
|
|
|
|
/* Similarly, for points with integer coordinates; here, a subtraction
|
|
|
|
operator that does return another point is useful. */
|
2000-07-31 02:37:33 +08:00
|
|
|
extern vector_type IPsubtract
|
1999-07-10 05:52:00 +08:00
|
|
|
(const coordinate_type, const coordinate_type);
|
2000-07-31 02:37:33 +08:00
|
|
|
extern coordinate_type IPsubtractP
|
1999-07-10 05:52:00 +08:00
|
|
|
(const coordinate_type, const coordinate_type);
|
2000-07-31 02:37:33 +08:00
|
|
|
extern coordinate_type IPadd
|
1999-07-10 05:52:00 +08:00
|
|
|
(const coordinate_type, const coordinate_type);
|
2000-07-31 02:37:33 +08:00
|
|
|
extern coordinate_type IPmult_scalar (const coordinate_type, const int);
|
|
|
|
extern real_coordinate_type IPmult_real
|
1999-07-10 05:52:00 +08:00
|
|
|
(const coordinate_type, const real);
|
2000-07-31 02:37:33 +08:00
|
|
|
extern boolean IPequal (const coordinate_type, const coordinate_type);
|
1999-07-10 05:52:00 +08:00
|
|
|
|
|
|
|
#endif /* not VECTOR_H */
|