1
/***************************************************************************
4
created : Wed Feb 18 01:20:19 CET 2003
5
copyright : (C) 2003-2004 Bernhard Wymann
6
email : berniw@bluewin.ch
7
version : $Id: linalg.h,v 1.8 2005/08/05 08:54:09 berniw Exp $
9
***************************************************************************/
11
/***************************************************************************
13
* This program is free software; you can redistribute it and/or modify *
14
* it under the terms of the GNU General Public License as published by *
15
* the Free Software Foundation; either version 2 of the License, or *
16
* (at your option) any later version. *
18
***************************************************************************/
23
inline float sign(float d) {
24
return (d >= 0.0f) ? 1.0f : -1.0f;
32
v2d(const v2d &src) { this->x = src.x; this->y = src.y; }
33
v2d(float x, float y) { this->x = x; this->y = y; }
36
v2d& operator=(const v2d &src); /* assignment */
37
v2d operator+(const v2d &src) const; /* addition */
38
v2d operator-(void) const; /* negation */
39
v2d operator-(const v2d &src) const; /* subtraction */
40
v2d operator*(const float s) const; /* multiply with scalar */
41
float operator*(const v2d &src) const; /* dot product */
42
friend v2d operator*(const float s, const v2d & src);
45
float len(void) const;
47
float dist(const v2d &p) const;
48
float cosalpha(const v2d &p2, const v2d ¢er) const;
49
v2d rotate(const v2d &c, float arc) const;
58
inline v2d& v2d::operator=(const v2d &src)
60
x = src.x; y = src.y; return *this;
64
/* add *this + src (vector addition) */
65
inline v2d v2d::operator+(const v2d &src) const
67
return v2d(x + src.x, y + src.y);
71
/* negation of *this */
72
inline v2d v2d::operator-(void) const
78
/* compute *this - src (vector subtraction) */
79
inline v2d v2d::operator-(const v2d &src) const
81
return v2d(x - src.x, y - src.y);
86
inline float v2d::operator*(const v2d &src) const
88
return src.x*x + src.y*y;
92
/* multiply vector with scalar (v2d*float) */
93
inline v2d v2d::operator*(const float s) const
99
/* multiply scalar with vector (float*v2d) */
100
inline v2d operator*(const float s, const v2d & src)
102
return v2d(s*src.x, s*src.y);
106
/* compute cosine of the angle between vectors *this-c and p2-c */
107
inline float v2d::cosalpha(const v2d &p2, const v2d &c) const
111
return (l1*l2)/(l1.len()*l2.len());
115
/* rotate vector arc radians around center c */
116
inline v2d v2d::rotate(const v2d &c, float arc) const
119
float sina = (float) sin(arc), cosa = (float) cos(arc);
120
return c + v2d(d.x*cosa-d.y*sina, d.x*sina+d.y*cosa);
124
/* compute the length of the vector */
125
inline float v2d::len(void) const
127
return (float) sqrt(x*x+y*y);
131
/* distance between *this and p */
132
inline float v2d::dist(const v2d &p) const
134
return (float) sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
138
/* normalize the vector */
139
inline void v2d::normalize(void)
141
float l = this->len();
150
Straight(float x, float y, float dx, float dy)
151
{ p.x = x; p.y = y; d.x = dx; d.y = dy; d.normalize(); }
152
Straight(const v2d &anchor, const v2d &dir)
153
{ p = anchor; d = dir; d.normalize(); }
156
v2d intersect(const Straight &s) const;
157
float dist(const v2d &p) const;
160
v2d p; /* point on the straight */
161
v2d d; /* direction of the straight */
165
/* intersection point of *this and s */
166
inline v2d Straight::intersect(const Straight &s) const
168
float t = -(d.x*(s.p.y-p.y)+d.y*(p.x-s.p.x))/(d.x*s.d.y-d.y*s.d.x);
173
/* distance of point s from straight *this */
174
inline float Straight::dist(const v2d &s) const
177
v2d d3 = d1 - d*d1*d;