Antkeeper  0.0.1
line-segment.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ANTKEEPER_GEOM_PRIMITIVES_LINE_SEGMENT_HPP
21 #define ANTKEEPER_GEOM_PRIMITIVES_LINE_SEGMENT_HPP
22 
23 #include <engine/math/vector.hpp>
24 
25 namespace geom {
26 namespace primitives {
27 
34 template <class T, std::size_t N>
36 {
39 
42 
45 
51  [[nodiscard]] inline T sqr_length() const noexcept
52  {
53  return math::sqr_distance(a, b);
54  }
55 
61  [[nodiscard]] inline T length() const noexcept
62  {
63  const T d = sqr_length();
64  return d ? std::sqrt(d) : d;
65  }
66 
74  [[nodiscard]] T sqr_distance(const vector_type& point) const noexcept
75  {
76  const vector_type ab = b - a;
77  const vector_type ap = point - a;
78 
79  const T t = math::dot(ap, ab);
80  if (t <= T{0})
81  {
82  return math::sqr_length(ap);
83  }
84 
85  const T ab_sqr_length = math::sqr_length(ab);
86  if (t >= ab_sqr_length)
87  {
88  return math::sqr_length(point - b);
89  }
90 
91  return math::sqr_length(ap) - (t * t) / ab_sqr_length;
92  }
93 
101  [[nodiscard]] inline T distance(const vector_type& point) const
102  {
103  const T d = sqr_distance(point);
104  return d ? std::sqrt(d) : d;
105  }
106 };
107 
108 } // namespace primitives
109 
110 using namespace primitives;
111 
112 } // namespace geom
113 
114 #endif // ANTKEEPER_GEOM_PRIMITIVES_LINE_SEGMENT_HPP
Geometric algorithms.
@ ab
Edge AB region.
constexpr T sqr_distance(const vector< T, N > &p0, const vector< T, N > &p1) noexcept
Calculates the square distance between two points.
Definition: vector.hpp:1514
constexpr T sqr_length(const quaternion< T > &q) noexcept
Calculates the square length of a quaternion.
Definition: quaternion.hpp:744
vector< T, N > sqrt(const vector< T, N > &x)
Takes the square root of each element.
constexpr T dot(const quaternion< T > &a, const quaternion< T > &b) noexcept
Calculates the dot product of two quaternions.
Definition: quaternion.hpp:572
n-dimensional line segment.
T distance(const vector_type &point) const
Calculates the distance from the line segment to a point.
vector_type a
First endpoint.
T sqr_length() const noexcept
Calculates the square length of the line segment.
vector_type b
Second endpoint.
T length() const noexcept
Calculates the length of the line segment.
T sqr_distance(const vector_type &point) const noexcept
Calculates the square distance from the line segment to a point.
n-dimensional vector.
Definition: vector.hpp:44