1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
/*
* This file is part of din.
*
* din is copyright (c) 2006 - 2012 S Jagannathan <jag@dinisnoise.org>
* For more information, please visit http://dinisnoise.org
*
* din is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* din 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 along
* with din. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef __vector2d
#define __vector2d
#include <math.h>
template <class T> inline T distance2 (const T& x1, const T& y1, const T& x2, const T& y2) {
T dx = x2 - x1;
T dy = y2 - y1;
return (dx * dx + dy * dy);
}
template<class T> inline void direction (T& dx, T& dy, const T& x1, const T& y1, const T& x2, const T& y2) {
dx = x2 - x1;
dy = y2 - y1;
}
template <class T> inline double magnitude (const T& x1, const T& y1, const T& x2, const T& y2) {
T dx, dy;
direction (dx, dy, x1, y1, x2, y2);
return sqrt ((double) (dx * dx + dy * dy));
}
template <class T> inline double magnitude (const T& vx, const T& vy) {
return sqrt ((double) (vx * vx + vy * vy));
}
template <class T> inline double magnitude2 (const T& x1, const T& y1, const T& x2, const T& y2) {
T dx, dy;
direction (dx, dy, x1, y1, x2, y2);
return (dx * dx + dy * dy);
}
template <class T> inline T magnitude2 (const T& vx, const T& vy) {
return (vx * vx + vy * vy);
}
template<class S, class T> inline double unit_vector (S& ux, S& uy, const T& x1, const T& y1, const T& x2, const T& y2) {
// unit vector joining x1,y1 & x2,y2
T dx, dy; direction (dx, dy, x1, y1, x2, y2);
double mag = magnitude (dx, dy);
if (mag > 0) {
ux = dx / mag;
uy = dy / mag;
} else {
ux = uy = 0;
}
return mag;
}
template <class T> inline double unit_vector (T& ux, T& uy, const T& vx, const T& vy) {
double mag = magnitude (vx, vy);
if (mag > 0) {
ux = vx / mag;
uy = vy / mag;
} else {
ux = uy = 0;
}
return mag;
}
#endif
|