Nebula
Toggle main menu visibility
Loading...
Searching...
No Matches
line.h
Go to the documentation of this file.
1
#pragma once
2
#ifndef MATH_LINE_H
3
#define MATH_LINE_H
4
//------------------------------------------------------------------------------
14
15
#include "
math/point.h
"
16
#include "
math/scalar.h
"
17
18
//------------------------------------------------------------------------------
19
namespace
Math
20
{
21
class
line
22
{
23
public
:
25
line
();
27
line
(
const
point
& startPoint,
const
point
& endPoint);
29
line
(
const
line
& rhs);
31
void
set
(
const
point
& startPoint,
const
point
& endPoint);
33
void
set_point_dir
(
const
point
& startPoint,
const
vector
& direction);
35
const
point
&
start
()
const
;
37
point
end
()
const
;
39
const
vector
&
vec
()
const
;
41
scalar
length
()
const
;
43
scalar
lengthsq
()
const
;
45
scalar
distance
(
const
point
& p)
const
;
47
bool
intersect
(
const
line
& l,
point
& pa,
point
& pb)
const
;
49
scalar
distance
(
const
line
& l,
point
& pa,
point
& pb)
const
;
51
scalar
closestpoint
(
const
point
& p)
const
;
53
point
pointat
(
scalar
t)
const
;
54
55
point
b
;
56
vector
m
;
57
};
58
59
//------------------------------------------------------------------------------
62
inline
63
line::line
()
64
{
65
// empty
66
}
67
68
//------------------------------------------------------------------------------
71
inline
72
line::line
(
const
point
& startPoint,
const
point
& endPoint) :
73
b
(startPoint),
74
m
(endPoint - startPoint)
75
{
76
// empty
77
}
78
79
//------------------------------------------------------------------------------
82
inline
83
line::line
(
const
line
& rhs) :
84
b
(rhs.
b
),
85
m
(rhs.
m
)
86
{
87
// empty
88
}
89
90
//------------------------------------------------------------------------------
93
inline
void
94
line::set
(
const
point
& startPoint,
const
point
& endPoint)
95
{
96
this->
b
= startPoint;
97
this->
m
= endPoint - startPoint;
98
}
99
100
//------------------------------------------------------------------------------
103
inline
void
104
line::set_point_dir
(
const
point
& startPoint,
const
vector
& dir)
105
{
106
this->
b
= startPoint;
107
this->
m
= dir;
108
}
109
110
//------------------------------------------------------------------------------
113
inline
const
point
&
114
line::start
()
const
115
{
116
return
this->
b
;
117
}
118
119
//------------------------------------------------------------------------------
122
inline
point
123
line::end
()
const
124
{
125
return
this->
b
+ this->
m
;
126
}
127
128
//------------------------------------------------------------------------------
131
inline
const
vector
&
132
line::vec
()
const
133
{
134
return
this->
m
;
135
}
136
137
//------------------------------------------------------------------------------
140
inline
scalar
141
line::length
()
const
142
{
143
return
Math::length
(this->
m
);
144
}
145
146
//------------------------------------------------------------------------------
149
inline
scalar
150
line::lengthsq
()
const
151
{
152
return
Math::lengthsq
(this->
m
);
153
}
154
155
//------------------------------------------------------------------------------
164
inline
scalar
165
line::closestpoint
(
const
point
& p)
const
166
{
167
vector
diff(p - this->
b
);
168
scalar
l =
dot
(this->
m
, this->
m
);
169
if
(l > 0.0f)
170
{
171
scalar
t =
dot
(this->
m
, diff) / l;
172
return
t;
173
}
174
else
175
{
176
return
0.0f;
177
}
178
}
179
180
//------------------------------------------------------------------------------
183
inline
scalar
184
line::distance
(
const
point
& p)
const
185
{
186
vector
diff(p - this->
b
);
187
scalar
l =
dot
(this->
m
, this->
m
);
188
if
(l > 0.0f)
189
{
190
scalar
t =
dot
(this->
m
, diff) / l;
191
diff = diff - this->
m
* t;
192
return
Math::length
(diff);
193
}
194
else
195
{
196
// line is really a point...
197
vector
v(p - this->
b
);
198
return
Math::length
(v);
199
}
200
}
201
202
//------------------------------------------------------------------------------
207
inline
point
208
line::pointat
(
scalar
t)
const
209
{
210
return
this->
b
+ this->
m
* t;
211
}
212
213
}
// namespace Math
214
//------------------------------------------------------------------------------
215
#endif
Math::line::line
line()
default constructor
Definition
line.h:63
Math::line::length
scalar length() const
get length
Definition
line.h:141
Math::line::distance
scalar distance(const point &p) const
minimal distance of point to line
Definition
line.h:184
Math::line::end
point end() const
get end point
Definition
line.h:123
Math::line::intersect
bool intersect(const line &l, point &pa, point &pb) const
intersect with line
Definition
line.cc:20
Math::line::vec
const vector & vec() const
get vector
Definition
line.h:132
Math::line::set
void set(const point &startPoint, const point &endPoint)
set start and end point
Definition
line.h:94
Math::line::lengthsq
scalar lengthsq() const
get squared length
Definition
line.h:150
Math::line::b
point b
Definition
line.h:55
Math::line::closestpoint
scalar closestpoint(const point &p) const
return t of the closest point on the line
Definition
line.h:165
Math::line::set_point_dir
void set_point_dir(const point &startPoint, const vector &direction)
set start point and direction
Definition
line.h:104
Math::line::start
const point & start() const
get start point
Definition
line.h:114
Math::line::pointat
point pointat(scalar t) const
return p = b + m*t
Definition
line.h:208
Math::line::m
vector m
Definition
line.h:56
Math
Different curves.
Definition
angularpfeedbackloop.h:17
Math::length
__forceinline scalar length(const quat &q)
Definition
quat.h:260
Math::lengthsq
__forceinline scalar lengthsq(const quat &q)
Definition
quat.h:269
Math::dot
__forceinline scalar dot(const plane &p, const vec4 &v1)
Definition
plane.h:252
Math::scalar
float scalar
Definition
scalar.h:45
point.h
scalar.h
Nebula's scalar datatype.
Math::point
Represents a 3D point in space.
Definition
point.h:22
Math::vector
A vector is a 3D direction in space.
Definition
vector.h:22
code
foundation
math
line.h
Generated on
for Nebula. Dark theme by
Tilen Majerle
. All rights reserved.