Nebula
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//------------------------------------------------------------------------------
15#include "math/point.h"
16#include "math/scalar.h"
17
18//------------------------------------------------------------------------------
19namespace Math
20{
21class line
22{
23public:
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
57};
58
59//------------------------------------------------------------------------------
62inline
64{
65 // empty
66}
67
68//------------------------------------------------------------------------------
71inline
72line::line(const point& startPoint, const point& endPoint) :
73 b(startPoint),
74 m(endPoint - startPoint)
75{
76 // empty
77}
78
79//------------------------------------------------------------------------------
82inline
83line::line(const line& rhs) :
84 b(rhs.b),
85 m(rhs.m)
86{
87 // empty
88}
89
90//------------------------------------------------------------------------------
93inline void
94line::set(const point& startPoint, const point& endPoint)
95{
96 this->b = startPoint;
97 this->m = endPoint - startPoint;
98}
99
100//------------------------------------------------------------------------------
103inline void
104line::set_point_dir(const point& startPoint, const vector& dir)
105{
106 this->b = startPoint;
107 this->m = dir;
108}
109
110//------------------------------------------------------------------------------
113inline const point&
115{
116 return this->b;
117}
118
119//------------------------------------------------------------------------------
122inline point
124{
125 return this->b + this->m;
126}
127
128//------------------------------------------------------------------------------
131inline const vector&
133{
134 return this->m;
135}
136
137//------------------------------------------------------------------------------
140inline scalar
142{
143 return Math::length(this->m);
144}
145
146//------------------------------------------------------------------------------
149inline scalar
151{
152 return Math::lengthsq(this->m);
153}
154
155//------------------------------------------------------------------------------
164inline scalar
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//------------------------------------------------------------------------------
183inline scalar
184line::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//------------------------------------------------------------------------------
207inline point
209{
210 return this->b + this->m * t;
211}
212
213} // namespace Math
214//------------------------------------------------------------------------------
215#endif
A line in 3d space.
Definition line.h:22
line()
default constructor
Definition line.h:63
scalar length() const
get length
Definition line.h:141
scalar distance(const point &p) const
minimal distance of point to line
Definition line.h:184
point end() const
get end point
Definition line.h:123
bool intersect(const line &l, point &pa, point &pb) const
intersect with line
Definition line.cc:20
const vector & vec() const
get vector
Definition line.h:132
void set(const point &startPoint, const point &endPoint)
set start and end point
Definition line.h:94
scalar lengthsq() const
get squared length
Definition line.h:150
point b
Definition line.h:55
scalar closestpoint(const point &p) const
return t of the closest point on the line
Definition line.h:165
void set_point_dir(const point &startPoint, const vector &direction)
set start point and direction
Definition line.h:104
const point & start() const
get start point
Definition line.h:114
point pointat(scalar t) const
return p = b + m*t
Definition line.h:208
vector m
Definition line.h:56
Different curves.
Definition angularpfeedbackloop.h:17
__forceinline scalar length(const quat &q)
Definition quat.h:259
__forceinline scalar lengthsq(const quat &q)
Definition quat.h:268
__forceinline scalar dot(const plane &p, const vec4 &v1)
Definition plane.h:246
float scalar
Definition scalar.h:45
Nebula's scalar datatype.
Represents a 3D point in space.
Definition point.h:22
A vector is a 3D direction in space.
Definition vector.h:22