line_segment.h

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2011 The ClanLib Team
00004 **
00005 **  This software is provided 'as-is', without any express or implied
00006 **  warranty.  In no event will the authors be held liable for any damages
00007 **  arising from the use of this software.
00008 **
00009 **  Permission is granted to anyone to use this software for any purpose,
00010 **  including commercial applications, and to alter it and redistribute it
00011 **  freely, subject to the following restrictions:
00012 **
00013 **  1. The origin of this software must not be misrepresented; you must not
00014 **     claim that you wrote the original software. If you use this software
00015 **     in a product, an acknowledgment in the product documentation would be
00016 **     appreciated but is not required.
00017 **  2. Altered source versions must be plainly marked as such, and must not be
00018 **     misrepresented as being the original software.
00019 **  3. This notice may not be removed or altered from any source distribution.
00020 **
00021 **  Note: Some of the libraries ClanLib may link to may have additional
00022 **  requirements or restrictions.
00023 **
00024 **  File Author(s):
00025 **
00026 **    Mark Page
00027 */
00028 
00031 
00032 #pragma once
00033 
00034 #include "../api_core.h"
00035 
00036 template<typename Type>
00037 class CL_LineSegment2x;
00038 
00039 template<typename Type>
00040 class CL_LineSegment3x;
00041 
00042 template<typename Type>
00043 class CL_Vec2;
00044 
00045 template<typename Type>
00046 class CL_Vec3;
00047 
00048 template<typename Type>
00049 class CL_Rectx;
00050 
00051 class CL_Angle;
00052 
00058 template<typename Type>
00059 class CL_LineSegment3x
00060 {
00061 public:
00063         CL_Vec3<Type> p;
00064 
00065         // \brief End point on the line
00066         CL_Vec3<Type> q;
00067 
00068         CL_LineSegment3x() { }
00069         CL_LineSegment3x(const CL_LineSegment3x<Type> &copy) { p = copy.p; q = copy.q;}
00070         CL_LineSegment3x(const CL_Vec3<Type> &point_p, const CL_Vec3<Type> &point_q) { p = point_p; q = point_q; }
00071 
00074 public:
00078         CL_Vec3<Type> get_midpoint() const { return CL_Vec3<Type>( (q.x + p.x)/((Type)2), (q.y + p.y)/((Type)2), (q.z + p.z)/((Type)2) ); };
00079 
00085         Type point_distance(const CL_Vec3<Type> &point, CL_Vec3<Type> &dest_intercept) const;
00086 
00090 public:
00092         CL_LineSegment3x<Type> &operator = (const CL_LineSegment3x<Type>& copy) { p = copy.p; q = copy.q; return *this; }
00093 
00095         bool operator == (const CL_LineSegment3x<Type>& line) const {return ((p == line.p) && (q == line.q));}
00096 
00098         bool operator != (const CL_LineSegment3x<Type>& line) const {return ((p != line.p) || (q != line.q));}
00100 };
00101 
00107 template<typename Type>
00108 class CL_LineSegment2x
00109 {
00110 public:
00112         CL_Vec2<Type> p;
00113 
00114         // \brief End point on the line
00115         CL_Vec2<Type> q;
00116 
00117         CL_LineSegment2x() { }
00118         CL_LineSegment2x(const CL_LineSegment2x<Type> &copy) { p = copy.p; q = copy.q;}
00119         CL_LineSegment2x(const CL_Vec2<Type> &point_p, const CL_Vec2<Type> &point_q) { p = point_p; q = point_q; }
00120 
00123 public:
00127         CL_Vec2<Type> get_midpoint() const { return CL_Vec2<Type>( (q.x + p.x)/((Type)2), (q.y + p.y)/((Type)2) ); };
00128 
00132         Type point_distance(const CL_Vec2<Type> &point);
00133 
00138         bool collinear(const CL_LineSegment2x<Type> &second) const;
00139 
00145         bool intersects( const CL_LineSegment2x<Type> &second, bool collinear_intersect ) const;
00146 
00152         CL_Vec2<Type> get_intersection( const CL_LineSegment2x<Type> &second, bool &intersect) const;
00153 
00158         Type point_right_of_line( const CL_Vec2<Type> &point ) const {return (q.x - p.x) * (point.y - p.y) - (point.x - p.x) * (q.y - p.y);}
00159 
00165         CL_Vec2<Type> normal() const;
00166 
00170 
00171 public:
00179         CL_LineSegment2x<Type> &clip(const CL_Rectx<Type> &rect, bool &clipped);
00180 
00184 public:
00186         CL_LineSegment2x<Type> &operator = (const CL_LineSegment2x<Type>& copy) { p = copy.p; q = copy.q; return *this; }
00187 
00189         bool operator == (const CL_LineSegment2x<Type>& line) const {return ((p == line.p) && (q == line.q));}
00190 
00192         bool operator != (const CL_LineSegment2x<Type>& line) const {return ((p != line.p) || (q != line.q));}
00194 };
00195 
00200 class CL_LineSegment2 : public CL_LineSegment2x<int>
00201 {
00202 public:
00203         CL_LineSegment2() : CL_LineSegment2x<int>() {}
00204         CL_LineSegment2(const CL_LineSegment2x<int> &copy) : CL_LineSegment2x<int>(copy) {}
00205         CL_LineSegment2(const CL_Vec2<int> &point_p, const CL_Vec2<int> &point_q) : CL_LineSegment2x<int>(point_p, point_q) {}
00206 };
00207 
00212 class CL_LineSegment2f : public CL_LineSegment2x<float>
00213 {
00214 public:
00215         CL_LineSegment2f() : CL_LineSegment2x<float>() {}
00216         CL_LineSegment2f(const CL_LineSegment2x<float> &copy) : CL_LineSegment2x<float>(copy) {}
00217         CL_LineSegment2f(const CL_Vec2<float> &point_p, const CL_Vec2<float> &point_q) : CL_LineSegment2x<float>(point_p, point_q) {}
00218 };
00219 
00224 class CL_LineSegment2d : public CL_LineSegment2x<double>
00225 {
00226 public:
00227         CL_LineSegment2d() : CL_LineSegment2x<double>() {}
00228         CL_LineSegment2d(const CL_LineSegment2x<double> &copy) : CL_LineSegment2x<double>(copy) {}
00229         CL_LineSegment2d(const CL_Vec2<double> &point_p, const CL_Vec2<double> &point_q) : CL_LineSegment2x<double>(point_p, point_q) {}
00230 };
00231 
00236 class CL_LineSegment3 : public CL_LineSegment3x<int>
00237 {
00238 public:
00239         CL_LineSegment3() : CL_LineSegment3x<int>() {}
00240         CL_LineSegment3(const CL_LineSegment3x<int> &copy) : CL_LineSegment3x<int>(copy) {}
00241         CL_LineSegment3(const CL_Vec3<int> &point_p, const CL_Vec3<int> &point_q) : CL_LineSegment3x<int>(point_p, point_q) {}
00242 };
00243 
00248 class CL_LineSegment3f : public CL_LineSegment3x<float>
00249 {
00250 public:
00251         CL_LineSegment3f() : CL_LineSegment3x<float>() {}
00252         CL_LineSegment3f(const CL_LineSegment3x<float> &copy) : CL_LineSegment3x<float>(copy) {}
00253         CL_LineSegment3f(const CL_Vec3<float> &point_p, const CL_Vec3<float> &point_q) : CL_LineSegment3x<float>(point_p, point_q) {}
00254 };
00255 
00260 class CL_LineSegment3d : public CL_LineSegment3x<double>
00261 {
00262 public:
00263         CL_LineSegment3d() : CL_LineSegment3x<double>() {}
00264         CL_LineSegment3d(const CL_LineSegment3x<double> &copy) : CL_LineSegment3x<double>(copy) {}
00265         CL_LineSegment3d(const CL_Vec3<double> &point_p, const CL_Vec3<double> &point_q) : CL_LineSegment3x<double>(point_p, point_q) {}
00266 };
00267 
00269