line_segment.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2013 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Mark Page
27 */
28 
29 
30 #pragma once
31 
32 #include "../api_core.h"
33 
34 namespace clan
35 {
38 
39 template<typename Type>
41 
42 template<typename Type>
44 
45 template<typename Type>
46 class Vec2;
47 
48 template<typename Type>
49 class Vec3;
50 
51 template<typename Type>
52 class Rectx;
53 
54 class Angle;
55 
60 template<typename Type>
61 class LineSegment3x
62 {
63 public:
66 
67  // \brief End point on the line
69 
71  LineSegment3x(const LineSegment3x<Type> &copy) { p = copy.p; q = copy.q;}
72  LineSegment3x(const Vec3<Type> &point_p, const Vec3<Type> &point_q) { p = point_p; q = point_q; }
73 
76 public:
80  Vec3<Type> get_midpoint() const { return Vec3<Type>( (q.x + p.x)/((Type)2), (q.y + p.y)/((Type)2), (q.z + p.z)/((Type)2) ); };
81 
87  Type point_distance(const Vec3<Type> &point, Vec3<Type> &dest_intercept) const;
88 
92 public:
94  LineSegment3x<Type> &operator = (const LineSegment3x<Type>& copy) { p = copy.p; q = copy.q; return *this; }
95 
97  bool operator == (const LineSegment3x<Type>& line) const {return ((p == line.p) && (q == line.q));}
98 
100  bool operator != (const LineSegment3x<Type>& line) const {return ((p != line.p) || (q != line.q));}
102 };
103 
108 template<typename Type>
109 class LineSegment2x
110 {
111 public:
114 
115  // \brief End point on the line
117 
119  LineSegment2x(const LineSegment2x<Type> &copy) { p = copy.p; q = copy.q;}
120  LineSegment2x(const Vec2<Type> &point_p, const Vec2<Type> &point_q) { p = point_p; q = point_q; }
121 
124 public:
128  Vec2<Type> get_midpoint() const { return Vec2<Type>( (q.x + p.x)/((Type)2), (q.y + p.y)/((Type)2) ); };
129 
133  Type point_distance(const Vec2<Type> &point);
134 
139  bool collinear(const LineSegment2x<Type> &second) const;
140 
146  bool intersects( const LineSegment2x<Type> &second, bool collinear_intersect ) const;
147 
153  Vec2<Type> get_intersection( const LineSegment2x<Type> &second, bool &intersect) const;
154 
159  Type point_right_of_line( const Vec2<Type> &point ) const {return (q.x - p.x) * (point.y - p.y) - (point.x - p.x) * (q.y - p.y);}
160 
166  Vec2<Type> normal() const;
167 
171 
172 public:
180  LineSegment2x<Type> &clip(const Rectx<Type> &rect, bool &clipped);
181 
185 public:
187  LineSegment2x<Type> &operator = (const LineSegment2x<Type>& copy) { p = copy.p; q = copy.q; return *this; }
188 
190  bool operator == (const LineSegment2x<Type>& line) const {return ((p == line.p) && (q == line.q));}
191 
193  bool operator != (const LineSegment2x<Type>& line) const {return ((p != line.p) || (q != line.q));}
195 };
196 
200 class LineSegment2 : public LineSegment2x<int>
201 {
202 public:
204  LineSegment2(const LineSegment2x<int> &copy) : LineSegment2x<int>(copy) {}
205  LineSegment2(const Vec2<int> &point_p, const Vec2<int> &point_q) : LineSegment2x<int>(point_p, point_q) {}
206 };
207 
211 class LineSegment2f : public LineSegment2x<float>
212 {
213 public:
214  LineSegment2f() : LineSegment2x<float>() {}
215  LineSegment2f(const LineSegment2x<float> &copy) : LineSegment2x<float>(copy) {}
216  LineSegment2f(const Vec2<float> &point_p, const Vec2<float> &point_q) : LineSegment2x<float>(point_p, point_q) {}
217 };
218 
222 class LineSegment2d : public LineSegment2x<double>
223 {
224 public:
225  LineSegment2d() : LineSegment2x<double>() {}
226  LineSegment2d(const LineSegment2x<double> &copy) : LineSegment2x<double>(copy) {}
227  LineSegment2d(const Vec2<double> &point_p, const Vec2<double> &point_q) : LineSegment2x<double>(point_p, point_q) {}
228 };
229 
233 class LineSegment3 : public LineSegment3x<int>
234 {
235 public:
237  LineSegment3(const LineSegment3x<int> &copy) : LineSegment3x<int>(copy) {}
238  LineSegment3(const Vec3<int> &point_p, const Vec3<int> &point_q) : LineSegment3x<int>(point_p, point_q) {}
239 };
240 
244 class LineSegment3f : public LineSegment3x<float>
245 {
246 public:
247  LineSegment3f() : LineSegment3x<float>() {}
248  LineSegment3f(const LineSegment3x<float> &copy) : LineSegment3x<float>(copy) {}
249  LineSegment3f(const Vec3<float> &point_p, const Vec3<float> &point_q) : LineSegment3x<float>(point_p, point_q) {}
250 };
251 
255 class LineSegment3d : public LineSegment3x<double>
256 {
257 public:
258  LineSegment3d() : LineSegment3x<double>() {}
259  LineSegment3d(const LineSegment3x<double> &copy) : LineSegment3x<double>(copy) {}
260  LineSegment3d(const Vec3<double> &point_p, const Vec3<double> &point_q) : LineSegment3x<double>(point_p, point_q) {}
261 };
262 
263 }
264 
266 
LineSegment2x(const LineSegment2x< Type > &copy)
Definition: line_segment.h:119
Vec2< Type > normal() const
Return the normal vector of the line from point A to point B.
Type point_distance(const Vec3< Type > &point, Vec3< Type > &dest_intercept) const
Calculate the distance from a line segment to a point.
2D line segment
Definition: line_segment.h:40
Angle class.
Definition: angle.h:63
LineSegment2(const Vec2< int > &point_p, const Vec2< int > &point_q)
Definition: line_segment.h:205
LineSegment2x()
Definition: line_segment.h:118
3D line segment - Integer
Definition: line_segment.h:233
bool intersects(const LineSegment2x< Type > &second, bool collinear_intersect) const
Return true if two line segments intersect.
LineSegment3f(const Vec3< float > &point_p, const Vec3< float > &point_q)
Definition: line_segment.h:249
Vec2< Type > get_intersection(const LineSegment2x< Type > &second, bool &intersect) const
Return the intersection point of two lines.
LineSegment3x(const LineSegment3x< Type > &copy)
Definition: line_segment.h:71
LineSegment3d(const LineSegment3x< double > &copy)
Definition: line_segment.h:259
Vec2< Type > p
Start point on the line.
Definition: line_segment.h:113
LineSegment3d()
Definition: line_segment.h:258
Vec2< Type > q
Definition: line_segment.h:116
3D line segment - Float
Definition: line_segment.h:244
LineSegment2d(const LineSegment2x< double > &copy)
Definition: line_segment.h:226
LineSegment2d(const Vec2< double > &point_p, const Vec2< double > &point_q)
Definition: line_segment.h:227
Type point_distance(const Vec2< Type > &point)
Return the distance from a point to a line.
LineSegment3x(const Vec3< Type > &point_p, const Vec3< Type > &point_q)
Definition: line_segment.h:72
Type x
Definition: vec2.h:82
2D line segment - Double
Definition: line_segment.h:222
bool operator==(const LineSegment2x< Type > &line) const
== operator.
Definition: line_segment.h:190
bool operator==(const LineSegment3x< Type > &line) const
== operator.
Definition: line_segment.h:97
bool operator!=(const LineSegment2x< Type > &line) const
!= operator.
Definition: line_segment.h:193
2D line segment - Integer
Definition: line_segment.h:200
LineSegment2x< Type > & clip(const Rectx< Type > &rect, bool &clipped)
Clip this line to a rectangle.
LineSegment3(const LineSegment3x< int > &copy)
Definition: line_segment.h:237
2D vector
Definition: line.h:49
LineSegment3d(const Vec3< double > &point_p, const Vec3< double > &point_q)
Definition: line_segment.h:260
LineSegment3f(const LineSegment3x< float > &copy)
Definition: line_segment.h:248
2D (left,top,right,bottom) rectangle structure.
Definition: line.h:46
LineSegment3x< Type > & operator=(const LineSegment3x< Type > &copy)
= operator.
Definition: line_segment.h:94
3D vector
Definition: line_ray.h:49
LineSegment2f(const Vec2< float > &point_p, const Vec2< float > &point_q)
Definition: line_segment.h:216
LineSegment2f()
Definition: line_segment.h:214
LineSegment2d()
Definition: line_segment.h:225
LineSegment2(const LineSegment2x< int > &copy)
Definition: line_segment.h:204
3D line segment
Definition: line_segment.h:43
Type point_right_of_line(const Vec2< Type > &point) const
Return [&lt;0, 0, &gt;0] if the Point P is right, on or left of the line trough A,B.
Definition: line_segment.h:159
LineSegment2()
Definition: line_segment.h:203
Vec3< Type > get_midpoint() const
Get the midpoint of this line.
Definition: line_segment.h:80
LineSegment3x()
Definition: line_segment.h:70
LineSegment2f(const LineSegment2x< float > &copy)
Definition: line_segment.h:215
LineSegment2x(const Vec2< Type > &point_p, const Vec2< Type > &point_q)
Definition: line_segment.h:120
bool collinear(const LineSegment2x< Type > &second) const
Return true if two line segments are collinear. (All points are on the same line.) ...
LineSegment3f()
Definition: line_segment.h:247
LineSegment3()
Definition: line_segment.h:236
Vec3< Type > p
Start point on the line.
Definition: line_segment.h:65
LineSegment3(const Vec3< int > &point_p, const Vec3< int > &point_q)
Definition: line_segment.h:238
LineSegment2x< Type > & operator=(const LineSegment2x< Type > &copy)
= operator.
Definition: line_segment.h:187
Vec3< Type > q
Definition: line_segment.h:68
Vec2< Type > get_midpoint() const
Get the midpoint of this line.
Definition: line_segment.h:128
2D line segment - Float
Definition: line_segment.h:211
3D line segment - Double
Definition: line_segment.h:255
Type y
Definition: vec2.h:83
bool operator!=(const LineSegment3x< Type > &line) const
!= operator.
Definition: line_segment.h:100