[4578] | 1 | /* |
---|
[2043] | 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
[4578] | 12 | main-programmer: Christian Meyer |
---|
[2551] | 13 | co-programmer: Patrick Boenzli : Vector::scale() |
---|
| 14 | Vector::abs() |
---|
[2043] | 15 | */ |
---|
| 16 | |
---|
[3590] | 17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_MATH |
---|
[2043] | 18 | |
---|
[6617] | 19 | #include "line.h" |
---|
| 20 | #include "plane.h" |
---|
[5662] | 21 | #ifdef DEBUG |
---|
[5672] | 22 | #include "debug.h" |
---|
[5662] | 23 | #else |
---|
[5672] | 24 | #include <stdio.h> |
---|
| 25 | #define PRINT(x) printf |
---|
[5662] | 26 | #endif |
---|
[2043] | 27 | |
---|
| 28 | |
---|
[9406] | 29 | |
---|
[2043] | 30 | /** |
---|
[4836] | 31 | * calculate the distance between two lines |
---|
| 32 | * @param l: the other line |
---|
| 33 | * @return the distance between the lines |
---|
[2043] | 34 | */ |
---|
| 35 | float Line::distance (const Line& l) const |
---|
| 36 | { |
---|
| 37 | float q, d; |
---|
| 38 | Vector n = a.cross(l.a); |
---|
| 39 | q = n.dot(r-l.r); |
---|
| 40 | d = n.len(); |
---|
| 41 | if( d == 0.0) return 0.0; |
---|
| 42 | return q/d; |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | /** |
---|
[4836] | 46 | * calculate the distance between a line and a point |
---|
| 47 | * @param v: the point |
---|
| 48 | * @return the distance between the Line and the point |
---|
[2043] | 49 | */ |
---|
[3228] | 50 | float Line::distancePoint (const Vector& v) const |
---|
[2043] | 51 | { |
---|
| 52 | Vector d = v-r; |
---|
| 53 | Vector u = a * d.dot( a); |
---|
| 54 | return (d - u).len(); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | /** |
---|
[4836] | 58 | * calculate the distance between a line and a point |
---|
| 59 | * @param v: the point |
---|
| 60 | * @return the distance between the Line and the point |
---|
[4578] | 61 | */ |
---|
| 62 | float Line::distancePoint (const sVec3D& v) const |
---|
| 63 | { |
---|
| 64 | Vector s(v[0], v[1], v[2]); |
---|
| 65 | Vector d = s - r; |
---|
| 66 | Vector u = a * d.dot( a); |
---|
| 67 | return (d - u).len(); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | /** |
---|
[4836] | 71 | * calculate the two points of minimal distance of two lines |
---|
| 72 | * @param l: the other line |
---|
| 73 | * @return a Vector[2] (!has to be deleted after use!) containing the two points of minimal distance |
---|
[2043] | 74 | */ |
---|
| 75 | Vector* Line::footpoints (const Line& l) const |
---|
| 76 | { |
---|
| 77 | Vector* fp = new Vector[2]; |
---|
| 78 | Plane p = Plane (r + a.cross(l.a), r, r + a); |
---|
[3234] | 79 | fp[1] = p.intersectLine (l); |
---|
[2043] | 80 | p = Plane (fp[1], l.a); |
---|
[3234] | 81 | fp[0] = p.intersectLine (*this); |
---|
[2043] | 82 | return fp; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | /** |
---|
| 86 | \brief calculate the length of a line |
---|
[4578] | 87 | \return the lenght of the line |
---|
[2043] | 88 | */ |
---|
| 89 | float Line::len() const |
---|
| 90 | { |
---|
| 91 | return a.len(); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | /** |
---|
[4836] | 95 | * rotate the line by given rotation |
---|
| 96 | * @param rot: a rotation |
---|
[2043] | 97 | */ |
---|
| 98 | void Line::rotate (const Rotation& rot) |
---|
| 99 | { |
---|
| 100 | Vector t = a + r; |
---|
[3234] | 101 | t = rotateVector( t, rot); |
---|
| 102 | r = rotateVector( r, rot), |
---|
[2043] | 103 | a = t - r; |
---|
| 104 | } |
---|