line_segment.h Source File - ClanLib SDK
Description: line_segment.h Source File

line_segment.h
1/*
2** ClanLib SDK
3** Copyright (c) 1997-2020 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
31namespace clan
32{
35
36 template<typename Type>
37 class LineSegment2x;
38
39 template<typename Type>
40 class LineSegment3x;
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:
62 Vec3<Type> p;
63
64 // \brief End point on the line
65 Vec3<Type> q;
66
67 LineSegment3x() : p(), q() {}
68 LineSegment3x(const LineSegment3x<Type> &copy) = default;
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) = default;
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:
102 Vec2<Type> p;
103
104 // \brief End point on the line
105 Vec2<Type> q;
106
107 LineSegment2x() : p(), q() {}
108 LineSegment2x(const LineSegment2x<Type> &copy) = default;
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) = default;
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:
179 LineSegment2() : LineSegment2x<int>() {}
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:
212 LineSegment3() : LineSegment3x<int>() {}
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}
clan::Angle
Angle class.
Definition angle.h:60
clan::LineSegment2::LineSegment2
LineSegment2()
Definition line_segment.h:179
clan::LineSegment2::LineSegment2
LineSegment2(const Vec2< int > &point_p, const Vec2< int > &point_q)
Definition line_segment.h:181
clan::LineSegment2::LineSegment2
LineSegment2(const LineSegment2x< int > &copy)
Definition line_segment.h:180
clan::LineSegment2d::LineSegment2d
LineSegment2d(const Vec2< double > &point_p, const Vec2< double > &point_q)
Definition line_segment.h:203
clan::LineSegment2d::LineSegment2d
LineSegment2d()
Definition line_segment.h:201
clan::LineSegment2d::LineSegment2d
LineSegment2d(const LineSegment2x< double > &copy)
Definition line_segment.h:202
clan::LineSegment2f::LineSegment2f
LineSegment2f(const Vec2< float > &point_p, const Vec2< float > &point_q)
Definition line_segment.h:192
clan::LineSegment2f::LineSegment2f
LineSegment2f()
Definition line_segment.h:190
clan::LineSegment2f::LineSegment2f
LineSegment2f(const LineSegment2x< float > &copy)
Definition line_segment.h:191
clan::LineSegment2x
2D line segment
Definition line_segment.h:99
clan::LineSegment2x::point_distance
Type point_distance(const Vec2< Type > &point)
Return the distance from a point to a line.
clan::LineSegment2x::LineSegment2x
LineSegment2x(const LineSegment2x< Type > &copy)=default
clan::LineSegment2x::p
Vec2< Type > p
Start point on the line.
Definition line_segment.h:102
clan::LineSegment2x::operator!=
bool operator!=(const LineSegment2x< Type > &line) const
!= operator.
Definition line_segment.h:170
clan::LineSegment2x::operator=
LineSegment2x< Type > & operator=(const LineSegment2x< Type > &copy)=default
= operator.
clan::LineSegment2x::LineSegment2x
LineSegment2x()
Definition line_segment.h:107
clan::LineSegment2x::LineSegment2x
LineSegment2x(const Vec2< Type > &point_p, const Vec2< Type > &point_q)
Definition line_segment.h:109
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::LineSegment2x::clip
LineSegment2x< Type > & clip(const Rectx< Type > &rect, bool &clipped)
Clip this line to a rectangle.
clan::LineSegment2x::intersects
bool intersects(const LineSegment2x< Type > &second, bool collinear_intersect) const
Return true if two line segments intersect.
clan::LineSegment2x::get_midpoint
Vec2< Type > get_midpoint() const
Get the midpoint of this line.
Definition line_segment.h:114
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:145
clan::LineSegment2x::operator==
bool operator==(const LineSegment2x< Type > &line) const
== operator.
Definition line_segment.h:167
clan::LineSegment2x::normal
Vec2< Type > normal() const
Return the normal vector of the line from point A to point B.
clan::LineSegment2x::get_intersection
Vec2< Type > get_intersection(const LineSegment2x< Type > &second, bool &intersect) const
Return the intersection point of two lines.
clan::LineSegment2x::q
Vec2< Type > q
Definition line_segment.h:105
clan::LineSegment3::LineSegment3
LineSegment3()
Definition line_segment.h:212
clan::LineSegment3::LineSegment3
LineSegment3(const LineSegment3x< int > &copy)
Definition line_segment.h:213
clan::LineSegment3::LineSegment3
LineSegment3(const Vec3< int > &point_p, const Vec3< int > &point_q)
Definition line_segment.h:214
clan::LineSegment3d::LineSegment3d
LineSegment3d(const Vec3< double > &point_p, const Vec3< double > &point_q)
Definition line_segment.h:236
clan::LineSegment3d::LineSegment3d
LineSegment3d(const LineSegment3x< double > &copy)
Definition line_segment.h:235
clan::LineSegment3d::LineSegment3d
LineSegment3d()
Definition line_segment.h:234
clan::LineSegment3f::LineSegment3f
LineSegment3f(const LineSegment3x< float > &copy)
Definition line_segment.h:224
clan::LineSegment3f::LineSegment3f
LineSegment3f(const Vec3< float > &point_p, const Vec3< float > &point_q)
Definition line_segment.h:225
clan::LineSegment3f::LineSegment3f
LineSegment3f()
Definition line_segment.h:223
clan::LineSegment3x
3D line segment
Definition line_segment.h:59
clan::LineSegment3x::get_midpoint
Vec3< Type > get_midpoint() const
Get the midpoint of this line.
Definition line_segment.h:74
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::LineSegment3x::LineSegment3x
LineSegment3x()
Definition line_segment.h:67
clan::LineSegment3x::LineSegment3x
LineSegment3x(const LineSegment3x< Type > &copy)=default
clan::LineSegment3x::operator=
LineSegment3x< Type > & operator=(const LineSegment3x< Type > &copy)=default
= operator.
clan::LineSegment3x::operator==
bool operator==(const LineSegment3x< Type > &line) const
== operator.
Definition line_segment.h:87
clan::LineSegment3x::LineSegment3x
LineSegment3x(const Vec3< Type > &point_p, const Vec3< Type > &point_q)
Definition line_segment.h:69
clan::LineSegment3x::p
Vec3< Type > p
Start point on the line.
Definition line_segment.h:62
clan::LineSegment3x::operator!=
bool operator!=(const LineSegment3x< Type > &line) const
!= operator.
Definition line_segment.h:90
clan::LineSegment3x::q
Vec3< Type > q
Definition line_segment.h:65
clan::Rectx
2D (left,top,right,bottom) rectangle structure.
Definition rect.h:51
clan::Vec2
2D vector
Definition vec2.h:76
clan::Vec3
3D vector
Definition vec3.h:75
clan::FillMode::line
@ line
Definition graphic_context.h:79
clan::FillMode::point
@ point
Definition graphic_context.h:78
clan
Definition clanapp.h:36