64 lines
1.7 KiB
C++
64 lines
1.7 KiB
C++
/*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*
|
|
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include "../point.hpp"
|
|
#include "../vector.hpp"
|
|
|
|
namespace util::geom {
|
|
// an line of infinite extent that passes though 'base'.
|
|
//
|
|
// direction should be normalised
|
|
template <size_t S, typename T>
|
|
struct line {
|
|
line () = default;
|
|
line (util::point<S,T> a, util::point<S,T> b):
|
|
line (a, normalised (b - a))
|
|
{ ; }
|
|
|
|
line (util::point<S,T> _base, util::vector<S,T> _direction):
|
|
base (_base),
|
|
direction (_direction)
|
|
{
|
|
CHECK (is_normalised (direction));
|
|
}
|
|
|
|
util::point<S,T> base;
|
|
util::vector<S,T> direction;
|
|
};
|
|
|
|
using line2f = line<2,float>;
|
|
using line3f = line<3,float>;
|
|
|
|
|
|
template <size_t S, typename T>
|
|
T
|
|
distance2 (line<S,T> l, point<S,T> p)
|
|
{
|
|
const auto t = dot (p - l.base, l.direction);
|
|
return distance2 (p, l.base + l.direction * t);
|
|
}
|
|
|
|
|
|
template <size_t S, typename T>
|
|
T
|
|
distance (line<S,T> l, point<S,T> p)
|
|
{
|
|
return std::sqrt (distance2 (l, p));
|
|
}
|
|
};
|