line_segment.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2016 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 #pragma once
30 
31 namespace clan
32 {
35 
36  template<typename Type>
38 
39  template<typename Type>
41 
42  template<typename Type>
43  class Vec2;
44 
45  template<typename Type>
46  class Vec3;
47 
48  template<typename Type>
49  class Rectx;
50 
51  class Angle;
52 
57  template<typename Type>
58  class LineSegment3x
59  {
60  public:
63 
64  // \brief End point on the line
66 
67  LineSegment3x() : p(), q() {}
68  LineSegment3x(const LineSegment3x<Type> &copy) : p(copy.p), q(copy.q) {}
69  LineSegment3x(const Vec3<Type> &point_p, const Vec3<Type> &point_q) : p(point_p), q(point_q) {}
70 
74  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)); };
75 
81  Type point_distance(const Vec3<Type> &point, Vec3<Type> &dest_intercept) const;
82 
84  LineSegment3x<Type> &operator = (const LineSegment3x<Type>& copy) { p = copy.p; q = copy.q; return *this; }
85 
87  bool operator == (const LineSegment3x<Type>& line) const { return ((p == line.p) && (q == line.q)); }
88 
90  bool operator != (const LineSegment3x<Type>& line) const { return ((p != line.p) || (q != line.q)); }
91  };
92 
97  template<typename Type>
98  class LineSegment2x
99  {
100  public:
103 
104  // \brief End point on the line
106 
107  LineSegment2x() : p(), q() {}
108  LineSegment2x(const LineSegment2x<Type> &copy) : p(copy.p), q(copy.q) {}
109  LineSegment2x(const Vec2<Type> &point_p, const Vec2<Type> &point_q) : p(point_p), q(point_q) {}
110 
114  Vec2<Type> get_midpoint() const { return Vec2<Type>((q.x + p.x) / ((Type)2), (q.y + p.y) / ((Type)2)); };
115 
119  Type point_distance(const Vec2<Type> &point);
120 
125  bool collinear(const LineSegment2x<Type> &second) const;
126 
132  bool intersects(const LineSegment2x<Type> &second, bool collinear_intersect) const;
133 
139  Vec2<Type> get_intersection(const LineSegment2x<Type> &second, bool &intersect) const;
140 
145  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); }
146 
152  Vec2<Type> normal() const;
153 
161  LineSegment2x<Type> &clip(const Rectx<Type> &rect, bool &clipped);
162 
164  LineSegment2x<Type> &operator = (const LineSegment2x<Type>& copy) { p = copy.p; q = copy.q; return *this; }
165 
167  bool operator == (const LineSegment2x<Type>& line) const { return ((p == line.p) && (q == line.q)); }
168 
170  bool operator != (const LineSegment2x<Type>& line) const { return ((p != line.p) || (q != line.q)); }
171  };
172 
176  class LineSegment2 : public LineSegment2x<int>
177  {
178  public:
180  LineSegment2(const LineSegment2x<int> &copy) : LineSegment2x<int>(copy) {}
181  LineSegment2(const Vec2<int> &point_p, const Vec2<int> &point_q) : LineSegment2x<int>(point_p, point_q) {}
182  };
183 
187  class LineSegment2f : public LineSegment2x<float>
188  {
189  public:
190  LineSegment2f() : LineSegment2x<float>() {}
191  LineSegment2f(const LineSegment2x<float> &copy) : LineSegment2x<float>(copy) {}
192  LineSegment2f(const Vec2<float> &point_p, const Vec2<float> &point_q) : LineSegment2x<float>(point_p, point_q) {}
193  };
194 
198  class LineSegment2d : public LineSegment2x<double>
199  {
200  public:
201  LineSegment2d() : LineSegment2x<double>() {}
202  LineSegment2d(const LineSegment2x<double> &copy) : LineSegment2x<double>(copy) {}
203  LineSegment2d(const Vec2<double> &point_p, const Vec2<double> &point_q) : LineSegment2x<double>(point_p, point_q) {}
204  };
205 
209  class LineSegment3 : public LineSegment3x<int>
210  {
211  public:
213  LineSegment3(const LineSegment3x<int> &copy) : LineSegment3x<int>(copy) {}
214  LineSegment3(const Vec3<int> &point_p, const Vec3<int> &point_q) : LineSegment3x<int>(point_p, point_q) {}
215  };
216 
220  class LineSegment3f : public LineSegment3x<float>
221  {
222  public:
223  LineSegment3f() : LineSegment3x<float>() {}
224  LineSegment3f(const LineSegment3x<float> &copy) : LineSegment3x<float>(copy) {}
225  LineSegment3f(const Vec3<float> &point_p, const Vec3<float> &point_q) : LineSegment3x<float>(point_p, point_q) {}
226  };
227 
231  class LineSegment3d : public LineSegment3x<double>
232  {
233  public:
234  LineSegment3d() : LineSegment3x<double>() {}
235  LineSegment3d(const LineSegment3x<double> &copy) : LineSegment3x<double>(copy) {}
236  LineSegment3d(const Vec3<double> &point_p, const Vec3<double> &point_q) : LineSegment3x<double>(point_p, point_q) {}
237  };
238 
240 }
Definition: clanapp.h:35
Angle class.
Definition: angle.h:59
bool operator!=(const LineSegment2x< Type > &line) const
!= operator.
Definition: line_segment.h:170
LineSegment2(const LineSegment2x< int > &copy)
Definition: line_segment.h:180
bool intersects(const LineSegment2x< Type > &second, bool collinear_intersect) const
Return true if two line segments intersect.
LineSegment3f(const LineSegment3x< float > &copy)
Definition: line_segment.h:224
2D vector
Definition: line.h:46
LineSegment3x< Type > & operator=(const LineSegment3x< Type > &copy)
= operator.
Definition: line_segment.h:84
3D line segment - Integer
Definition: line_segment.h:209
LineSegment3x(const LineSegment3x< Type > &copy)
Definition: line_segment.h:68
LineSegment3()
Definition: line_segment.h:212
LineSegment3x()
Definition: line_segment.h:67
bool operator==(const LineSegment3x< Type > &line) const
== operator.
Definition: line_segment.h:87
Type point_distance(const Vec3< Type > &point, Vec3< Type > &dest_intercept) const
Calculate the distance from a line segment to a point.
LineSegment2x(const Vec2< Type > &point_p, const Vec2< Type > &point_q)
Definition: line_segment.h:109
LineSegment2x< Type > & clip(const Rectx< Type > &rect, bool &clipped)
Clip this line to a rectangle.
bool operator!=(const LineSegment3x< Type > &line) const
!= operator.
Definition: line_segment.h:90
2D line segment
Definition: line_segment.h:37
LineSegment3d(const Vec3< double > &point_p, const Vec3< double > &point_q)
Definition: line_segment.h:236
LineSegment2d(const LineSegment2x< double > &copy)
Definition: line_segment.h:202
LineSegment3d(const LineSegment3x< double > &copy)
Definition: line_segment.h:235
Type y
Definition: vec3.h:80
3D line segment - Float
Definition: line_segment.h:220
LineSegment2(const Vec2< int > &point_p, const Vec2< int > &point_q)
Definition: line_segment.h:181
bool operator==(const LineSegment2x< Type > &line) const
== operator.
Definition: line_segment.h:167
Vec3< Type > q
Definition: line_segment.h:65
LineSegment3f()
Definition: line_segment.h:223
LineSegment3d()
Definition: line_segment.h:234
2D line segment - Double
Definition: line_segment.h:198
Vec2< Type > get_intersection(const LineSegment2x< Type > &second, bool &intersect) const
Return the intersection point of two lines.
LineSegment2()
Definition: line_segment.h:179
Type y
Definition: vec2.h:81
Vec3< Type > p
Start point on the line.
Definition: line_segment.h:62
2D line segment - Integer
Definition: line_segment.h:176
Type z
Definition: vec3.h:81
LineSegment2x(const LineSegment2x< Type > &copy)
Definition: line_segment.h:108
Vec3< Type > get_midpoint() const
Get the midpoint of this line.
Definition: line_segment.h:74
LineSegment2x< Type > & operator=(const LineSegment2x< Type > &copy)
= operator.
Definition: line_segment.h:164
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:145
LineSegment2f(const Vec2< float > &point_p, const Vec2< float > &point_q)
Definition: line_segment.h:192
2D (left,top,right,bottom) rectangle structure.
Definition: line.h:43
LineSegment2f(const LineSegment2x< float > &copy)
Definition: line_segment.h:191
LineSegment3f(const Vec3< float > &point_p, const Vec3< float > &point_q)
Definition: line_segment.h:225
LineSegment2f()
Definition: line_segment.h:190
bool collinear(const LineSegment2x< Type > &second) const
Return true if two line segments are collinear. (All points are on the same line.) ...
Type x
Definition: vec3.h:79
Vec2< Type > normal() const
Return the normal vector of the line from point A to point B.
LineSegment2x()
Definition: line_segment.h:107
Type point_distance(const Vec2< Type > &point)
Return the distance from a point to a line.
Vec2< Type > get_midpoint() const
Get the midpoint of this line.
Definition: line_segment.h:114
LineSegment3(const LineSegment3x< int > &copy)
Definition: line_segment.h:213
Vec2< Type > q
Definition: line_segment.h:105
LineSegment2d()
Definition: line_segment.h:201
LineSegment3x(const Vec3< Type > &point_p, const Vec3< Type > &point_q)
Definition: line_segment.h:69
Type x
Definition: vec2.h:80
3D vector
Definition: line_ray.h:46
2D line segment - Float
Definition: line_segment.h:187
Vec2< Type > p
Start point on the line.
Definition: line_segment.h:102
3D line segment - Double
Definition: line_segment.h:231
3D line segment
Definition: line_segment.h:40
LineSegment3(const Vec3< int > &point_p, const Vec3< int > &point_q)
Definition: line_segment.h:214
LineSegment2d(const Vec2< double > &point_p, const Vec2< double > &point_q)
Definition: line_segment.h:203