00001 //////////////////////////////////////////////////////////////////////////////// 00002 // Scorched3D (c) 2000-2009 00003 // 00004 // This file is part of Scorched3D. 00005 // 00006 // Scorched3D is free software; you can redistribute it and/or modify 00007 // it under the terms of the GNU General Public License as published by 00008 // the Free Software Foundation; either version 2 of the License, or 00009 // (at your option) any later version. 00010 // 00011 // Scorched3D is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU General Public License 00017 // along with Scorched3D; if not, write to the Free Software 00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 //////////////////////////////////////////////////////////////////////////////// 00020 00021 00022 // Line.cpp: implementation of the Line class. 00023 // 00024 ////////////////////////////////////////////////////////////////////// 00025 00026 #include <common/Line.h> 00027 00028 ////////////////////////////////////////////////////////////////////// 00029 // Construction/Destruction 00030 ////////////////////////////////////////////////////////////////////// 00031 00032 Line::Line() 00033 { 00034 00035 } 00036 00037 Line::Line(Vector &start, Vector &end) 00038 { 00039 start_ = start; 00040 end_ = end; 00041 00042 dir_ = end_ - start_; 00043 } 00044 00045 Line::~Line() 00046 { 00047 00048 } 00049 00050 void Line::setPoints(const Vector &start, const Vector &end) 00051 { 00052 start_ = start; 00053 end_ = end; 00054 dir_ = end_ - start_; 00055 } 00056 00057 void Line::setStart(const Vector &start) 00058 { 00059 start_ = start; 00060 dir_ = end_ - start_; 00061 } 00062 00063 void Line::setEnd(const Vector &end) 00064 { 00065 end_ = end; 00066 dir_ = end_ - start_; 00067 } 00068 00069 Vector Line::operator*(const Line &line) 00070 { 00071 return dir_ * line.dir_; 00072 } 00073 00074 float Line::dotP(const Line &line) 00075 { 00076 return dir_.dotP(line.dir_); 00077 } 00078 00079 Vector Line::get2DPerp() 00080 { 00081 return dir_.get2DPerp(); 00082 } 00083 00084 bool Line::intersect(const Line &line, Vector &interPt, const bool checkPtOnLine) 00085 { 00086 if (dotP(line) == 0.0f) return false; 00087 00088 Vector &dir1 = (Vector &) dir_; 00089 Vector &dir2 = (Vector &) ((Line &)line).getDirection(); 00090 00091 Vector pDir1 = Vector(dir1[1], -dir1[0], 0.0f); 00092 Vector pDir2 = Vector(dir2[1], -dir2[0], 0.0f); 00093 00094 float u1 = pDir2.dotP((Vector &)((Line &)line).getStart() - start_) / pDir2.dotP(dir1); 00095 float u2 = pDir1.dotP(start_ - ((Line &)line).getStart()) / pDir1.dotP(dir2); 00096 00097 if (checkPtOnLine) 00098 { 00099 if (u1 < 0.0f || u1 > 1.0f) return false; 00100 if (u2 < 0.0f || u2 > 1.0f) return false; 00101 } 00102 00103 interPt = start_ + dir1 * u1; 00104 00105 return true; 00106 } 00107 00108 const Vector &Line::getDirection() 00109 { 00110 return dir_; 00111 } 00112 00113 const Vector &Line::getStart() 00114 { 00115 return start_; 00116 } 00117 00118 const Vector &Line::getEnd() 00119 { 00120 return end_; 00121 }
1.5.3