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>
39 class LineSegment2x;
40
41 template<typename Type>
42 class LineSegment3x;
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:
64 Vec3<Type> p;
65
66 // \brief End point on the line
67 Vec3<Type> q;
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:
112 Vec2<Type> p;
113
114 // \brief End point on the line
115 Vec2<Type> q;
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
165 Vec2<Type> normal() const;
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:
202 LineSegment2() : LineSegment2x<int>() {}
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:
235 LineSegment3() : LineSegment3x<int>() {}
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
clan::LineSegment2x::LineSegment2x
LineSegment2x(const LineSegment2x< Type > &copy)
Definition: line_segment.h:118
clan::LineSegment2x::normal
Vec2< Type > normal() const
Return the normal vector of the line from point A to point B.
clan
Definition: clanapp.h:35
clan::Vec3::x
Type x
Definition: vec3.h:80
clan::LineSegment3x::point_distance
Type point_distance(const Vec3< Type > &point, Vec3< Type > &dest_intercept) const
Calculate the distance from a line segment to a point.
clan::LineSegment2x
2D line segment
Definition: line_segment.h:39
clan::Angle
Angle class.
Definition: angle.h:62
clan::LineSegment2::LineSegment2
LineSegment2(const Vec2< int > &point_p, const Vec2< int > &point_q)
Definition: line_segment.h:204
clan::LineSegment2x::LineSegment2x
LineSegment2x()
Definition: line_segment.h:117
clan::Vec3::y
Type y
Definition: vec3.h:81
clan::LineSegment3
3D line segment - Integer
Definition: line_segment.h:232
clan::LineSegment2x::intersects
bool intersects(const LineSegment2x< Type > &second, bool collinear_intersect) const
Return true if two line segments intersect.
clan::LineSegment3f::LineSegment3f
LineSegment3f(const Vec3< float > &point_p, const Vec3< float > &point_q)
Definition: line_segment.h:248
clan::LineSegment2x::get_intersection
Vec2< Type > get_intersection(const LineSegment2x< Type > &second, bool &intersect) const
Return the intersection point of two lines.
clan::LineSegment3x::LineSegment3x
LineSegment3x(const LineSegment3x< Type > &copy)
Definition: line_segment.h:70
clan::LineSegment3d::LineSegment3d
LineSegment3d(const LineSegment3x< double > &copy)
Definition: line_segment.h:258
clan::LineSegment2x::p
Vec2< Type > p
Start point on the line.
Definition: line_segment.h:112
clan::LineSegment3d::LineSegment3d
LineSegment3d()
Definition: line_segment.h:257
clan::LineSegment2x::q
Vec2< Type > q
Definition: line_segment.h:115
clan::LineSegment3f
3D line segment - Float
Definition: line_segment.h:243
clan::LineSegment2d::LineSegment2d
LineSegment2d(const LineSegment2x< double > &copy)
Definition: line_segment.h:225
clan::LineSegment2d::LineSegment2d
LineSegment2d(const Vec2< double > &point_p, const Vec2< double > &point_q)
Definition: line_segment.h:226
clan::LineSegment2x::point_distance
Type point_distance(const Vec2< Type > &point)
Return the distance from a point to a line.
clan::LineSegment3x::LineSegment3x
LineSegment3x(const Vec3< Type > &point_p, const Vec3< Type > &point_q)
Definition: line_segment.h:71
clan::Vec2::x
Type x
Definition: vec2.h:80
clan::LineSegment2d
2D line segment - Double
Definition: line_segment.h:221
clan::LineSegment2x::operator==
bool operator==(const LineSegment2x< Type > &line) const
== operator.
Definition: line_segment.h:189
clan::LineSegment3x::operator==
bool operator==(const LineSegment3x< Type > &line) const
== operator.
Definition: line_segment.h:96
clan::LineSegment2x::operator!=
bool operator!=(const LineSegment2x< Type > &line) const
!= operator.
Definition: line_segment.h:192
clan::LineSegment2
2D line segment - Integer
Definition: line_segment.h:199
clan::LineSegment2x::clip
LineSegment2x< Type > & clip(const Rectx< Type > &rect, bool &clipped)
Clip this line to a rectangle.
clan::LineSegment3::LineSegment3
LineSegment3(const LineSegment3x< int > &copy)
Definition: line_segment.h:236
clan::Vec2
2D vector
Definition: line.h:48
clan::LineSegment3d::LineSegment3d
LineSegment3d(const Vec3< double > &point_p, const Vec3< double > &point_q)
Definition: line_segment.h:259
clan::LineSegment3f::LineSegment3f
LineSegment3f(const LineSegment3x< float > &copy)
Definition: line_segment.h:247
clan::Rectx
2D (left,top,right,bottom) rectangle structure.
Definition: line.h:45
clan::LineSegment3x::operator=
LineSegment3x< Type > & operator=(const LineSegment3x< Type > &copy)
= operator.
Definition: line_segment.h:93
clan::Vec3
3D vector
Definition: line_ray.h:48
clan::LineSegment2f::LineSegment2f
LineSegment2f(const Vec2< float > &point_p, const Vec2< float > &point_q)
Definition: line_segment.h:215
clan::LineSegment2f::LineSegment2f
LineSegment2f()
Definition: line_segment.h:213
clan::LineSegment2d::LineSegment2d
LineSegment2d()
Definition: line_segment.h:224
clan::LineSegment2::LineSegment2
LineSegment2(const LineSegment2x< int > &copy)
Definition: line_segment.h:203
clan::LineSegment3x
3D line segment
Definition: line_segment.h:42
clan::LineSegment2x::point_right_of_line
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
clan::LineSegment2::LineSegment2
LineSegment2()
Definition: line_segment.h:202
clan::LineSegment3x::get_midpoint
Vec3< Type > get_midpoint() const
Get the midpoint of this line.
Definition: line_segment.h:79
clan::LineSegment3x::LineSegment3x
LineSegment3x()
Definition: line_segment.h:69
clan::LineSegment2f::LineSegment2f
LineSegment2f(const LineSegment2x< float > &copy)
Definition: line_segment.h:214
clan::LineSegment2x::LineSegment2x
LineSegment2x(const Vec2< Type > &point_p, const Vec2< Type > &point_q)
Definition: line_segment.h:119
clan::LineSegment2x::collinear
bool collinear(const LineSegment2x< Type > &second) const
Return true if two line segments are collinear. (All points are on the same line.) ...
clan::LineSegment3f::LineSegment3f
LineSegment3f()
Definition: line_segment.h:246
clan::LineSegment3::LineSegment3
LineSegment3()
Definition: line_segment.h:235
clan::LineSegment3x::p
Vec3< Type > p
Start point on the line.
Definition: line_segment.h:64
clan::LineSegment3::LineSegment3
LineSegment3(const Vec3< int > &point_p, const Vec3< int > &point_q)
Definition: line_segment.h:237
clan::LineSegment2x::operator=
LineSegment2x< Type > & operator=(const LineSegment2x< Type > &copy)
= operator.
Definition: line_segment.h:186
clan::LineSegment3x::q
Vec3< Type > q
Definition: line_segment.h:67
clan::LineSegment2x::get_midpoint
Vec2< Type > get_midpoint() const
Get the midpoint of this line.
Definition: line_segment.h:127
clan::LineSegment2f
2D line segment - Float
Definition: line_segment.h:210
clan::LineSegment3d
3D line segment - Double
Definition: line_segment.h:254
clan::Vec3::z
Type z
Definition: vec3.h:82
clan::Vec2::y
Type y
Definition: vec2.h:81
clan::LineSegment3x::operator!=
bool operator!=(const LineSegment3x< Type > &line) const
!= operator.
Definition: line_segment.h:99