line_segment.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2015 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 
33 namespace clan
34 {
37 
38 template<typename Type>
40 
41 template<typename Type>
43 
44 template<typename Type>
45 class Vec2;
46 
47 template<typename Type>
48 class Vec3;
49 
50 template<typename Type>
51 class Rectx;
52 
53 class Angle;
54 
59 template<typename Type>
60 class LineSegment3x
61 {
62 public:
65 
66  // \brief End point on the line
68 
69  LineSegment3x(): p(), q() {}
70  LineSegment3x(const LineSegment3x<Type> &copy): p(copy.p), q(copy.q) {}
71  LineSegment3x(const Vec3<Type> &point_p, const Vec3<Type> &point_q): p(point_p), q(point_q) {}
72 
75 public:
79  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) ); };
80 
86  Type point_distance(const Vec3<Type> &point, Vec3<Type> &dest_intercept) const;
87 
91 public:
93  LineSegment3x<Type> &operator = (const LineSegment3x<Type>& copy) { p = copy.p; q = copy.q; return *this; }
94 
96  bool operator == (const LineSegment3x<Type>& line) const {return ((p == line.p) && (q == line.q));}
97 
99  bool operator != (const LineSegment3x<Type>& line) const {return ((p != line.p) || (q != line.q));}
101 };
102 
107 template<typename Type>
108 class LineSegment2x
109 {
110 public:
113 
114  // \brief End point on the line
116 
117  LineSegment2x(): p(), q() {}
118  LineSegment2x(const LineSegment2x<Type> &copy): p(copy.p), q(copy.q) {}
119  LineSegment2x(const Vec2<Type> &point_p, const Vec2<Type> &point_q): p(point_p), q(point_q) {}
120 
123 public:
127  Vec2<Type> get_midpoint() const { return Vec2<Type>( (q.x + p.x)/((Type)2), (q.y + p.y)/((Type)2) ); };
128 
132  Type point_distance(const Vec2<Type> &point);
133 
138  bool collinear(const LineSegment2x<Type> &second) const;
139 
145  bool intersects( const LineSegment2x<Type> &second, bool collinear_intersect ) const;
146 
152  Vec2<Type> get_intersection( const LineSegment2x<Type> &second, bool &intersect) const;
153 
158  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);}
159 
166 
170 
171 public:
179  LineSegment2x<Type> &clip(const Rectx<Type> &rect, bool &clipped);
180 
184 public:
186  LineSegment2x<Type> &operator = (const LineSegment2x<Type>& copy) { p = copy.p; q = copy.q; return *this; }
187 
189  bool operator == (const LineSegment2x<Type>& line) const {return ((p == line.p) && (q == line.q));}
190 
192  bool operator != (const LineSegment2x<Type>& line) const {return ((p != line.p) || (q != line.q));}
194 };
195 
199 class LineSegment2 : public LineSegment2x<int>
200 {
201 public:
203  LineSegment2(const LineSegment2x<int> &copy) : LineSegment2x<int>(copy) {}
204  LineSegment2(const Vec2<int> &point_p, const Vec2<int> &point_q) : LineSegment2x<int>(point_p, point_q) {}
205 };
206 
210 class LineSegment2f : public LineSegment2x<float>
211 {
212 public:
213  LineSegment2f() : LineSegment2x<float>() {}
214  LineSegment2f(const LineSegment2x<float> &copy) : LineSegment2x<float>(copy) {}
215  LineSegment2f(const Vec2<float> &point_p, const Vec2<float> &point_q) : LineSegment2x<float>(point_p, point_q) {}
216 };
217 
221 class LineSegment2d : public LineSegment2x<double>
222 {
223 public:
224  LineSegment2d() : LineSegment2x<double>() {}
225  LineSegment2d(const LineSegment2x<double> &copy) : LineSegment2x<double>(copy) {}
226  LineSegment2d(const Vec2<double> &point_p, const Vec2<double> &point_q) : LineSegment2x<double>(point_p, point_q) {}
227 };
228 
232 class LineSegment3 : public LineSegment3x<int>
233 {
234 public:
236  LineSegment3(const LineSegment3x<int> &copy) : LineSegment3x<int>(copy) {}
237  LineSegment3(const Vec3<int> &point_p, const Vec3<int> &point_q) : LineSegment3x<int>(point_p, point_q) {}
238 };
239 
243 class LineSegment3f : public LineSegment3x<float>
244 {
245 public:
246  LineSegment3f() : LineSegment3x<float>() {}
247  LineSegment3f(const LineSegment3x<float> &copy) : LineSegment3x<float>(copy) {}
248  LineSegment3f(const Vec3<float> &point_p, const Vec3<float> &point_q) : LineSegment3x<float>(point_p, point_q) {}
249 };
250 
254 class LineSegment3d : public LineSegment3x<double>
255 {
256 public:
257  LineSegment3d() : LineSegment3x<double>() {}
258  LineSegment3d(const LineSegment3x<double> &copy) : LineSegment3x<double>(copy) {}
259  LineSegment3d(const Vec3<double> &point_p, const Vec3<double> &point_q) : LineSegment3x<double>(point_p, point_q) {}
260 };
261 
262 }
263 
265 
LineSegment2()
Definition: line_segment.h:202
Vec3< Type > q
Definition: line_segment.h:67
LineSegment2f(const Vec2< float > &point_p, const Vec2< float > &point_q)
Definition: line_segment.h:215
LineSegment3x< Type > & operator=(const LineSegment3x< Type > &copy)
= operator.
Definition: line_segment.h:93
bool operator==(const LineSegment3x< Type > &line) const
== operator.
Definition: line_segment.h:96
2D line segment - Float
Definition: line_segment.h:211
3D line segment - Double
Definition: line_segment.h:255
Type point_distance(const Vec3< Type > &point, Vec3< Type > &dest_intercept) const
Calculate the distance from a line segment to a point.
Type y
Definition: vec2.h:81
Type point_distance(const Vec2< Type > &point)
Return the distance from a point to a line.
2D (left,top,right,bottom) rectangle structure.
Definition: line.h:45
LineSegment2x(const LineSegment2x< Type > &copy)
Definition: line_segment.h:118
Type point_right_of_line(const Vec2< Type > &point) const
Return [<0, 0, >0] if the Point P is right, on or left of the line trough A,B.
Definition: line_segment.h:158
3D line segment - Integer
Definition: line_segment.h:233
2D line segment
Definition: line_segment.h:39
LineSegment2d(const Vec2< double > &point_p, const Vec2< double > &point_q)
Definition: line_segment.h:226
LineSegment3x(const LineSegment3x< Type > &copy)
Definition: line_segment.h:70
LineSegment3x(const Vec3< Type > &point_p, const Vec3< Type > &point_q)
Definition: line_segment.h:71
LineSegment3d()
Definition: line_segment.h:257
Type x
Definition: vec2.h:80
LineSegment3(const LineSegment3x< int > &copy)
Definition: line_segment.h:236
Vec2< Type > p
Start point on the line.
Definition: line_segment.h:112
Vec2< Type > q
Definition: line_segment.h:115
Vec2< Type > normal() const
Return the normal vector of the line from point A to point B.
Angle class.
Definition: angle.h:63
Vec2< Type > get_midpoint() const
Get the midpoint of this line.
Definition: line_segment.h:127
bool intersects(const LineSegment2x< Type > &second, bool collinear_intersect) const
Return true if two line segments intersect.
bool operator!=(const LineSegment2x< Type > &line) const
!= operator.
Definition: line_segment.h:192
2D vector
Definition: line.h:48
LineSegment3f(const LineSegment3x< float > &copy)
Definition: line_segment.h:247
Vec3< Type > p
Start point on the line.
Definition: line_segment.h:64
LineSegment3(const Vec3< int > &point_p, const Vec3< int > &point_q)
Definition: line_segment.h:237
LineSegment3x()
Definition: line_segment.h:69
Vec3< Type > get_midpoint() const
Get the midpoint of this line.
Definition: line_segment.h:79
LineSegment3f(const Vec3< float > &point_p, const Vec3< float > &point_q)
Definition: line_segment.h:248
bool operator!=(const LineSegment3x< Type > &line) const
!= operator.
Definition: line_segment.h:99
3D line segment - Float
Definition: line_segment.h:244
LineSegment2f()
Definition: line_segment.h:213
LineSegment3()
Definition: line_segment.h:235
2D line segment - Integer
Definition: line_segment.h:200
LineSegment2d(const LineSegment2x< double > &copy)
Definition: line_segment.h:225
LineSegment2f(const LineSegment2x< float > &copy)
Definition: line_segment.h:214
2D line segment - Double
Definition: line_segment.h:222
bool operator==(const LineSegment2x< Type > &line) const
== operator.
Definition: line_segment.h:189
Definition: clanapp.h:36
LineSegment2x< Type > & clip(const Rectx< Type > &rect, bool &clipped)
Clip this line to a rectangle.
LineSegment2(const LineSegment2x< int > &copy)
Definition: line_segment.h:203
LineSegment2x()
Definition: line_segment.h:117
LineSegment3f()
Definition: line_segment.h:246
LineSegment2d()
Definition: line_segment.h:224
LineSegment2(const Vec2< int > &point_p, const Vec2< int > &point_q)
Definition: line_segment.h:204
LineSegment2x(const Vec2< Type > &point_p, const Vec2< Type > &point_q)
Definition: line_segment.h:119
3D vector
Definition: line_ray.h:48
3D line segment
Definition: line_segment.h:42
LineSegment3d(const Vec3< double > &point_p, const Vec3< double > &point_q)
Definition: line_segment.h:259
Vec2< Type > get_intersection(const LineSegment2x< Type > &second, bool &intersect) const
Return the intersection point of two lines.
bool collinear(const LineSegment2x< Type > &second) const
Return true if two line segments are collinear. (All points are on the same line.)
LineSegment3d(const LineSegment3x< double > &copy)
Definition: line_segment.h:258
LineSegment2x< Type > & operator=(const LineSegment2x< Type > &copy)
= operator.
Definition: line_segment.h:186