diff --git a/geom/tri.cpp b/geom/tri.cpp index 3ba4e338..c956668e 100644 --- a/geom/tri.cpp +++ b/geom/tri.cpp @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2015 Danny Robson + * Copyright 2016-2018 Danny Robson */ - +#include "tri.hpp" diff --git a/geom/tri.hpp b/geom/tri.hpp index 3ba4e338..e2b220c2 100644 --- a/geom/tri.hpp +++ b/geom/tri.hpp @@ -11,7 +11,68 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2015 Danny Robson + * Copyright 2016-2018 Danny Robson */ +#ifndef CRUFT_GEOM_TRI_HPP +#define CRUFT_GEOM_TRI_HPP +#include + +#include "../point.hpp" + +namespace util::geom { + /////////////////////////////////////////////////////////////////////////// + // n-dimensional triangle area + template + ValueT + area (util::point a, + util::point b, + util::point c) + { + // heron's formula + const auto ab = util::distance (a, b); + const auto bc = util::distance (b, c); + const auto ca = util::distance (c, a); + + const auto s = (ab + bc + ca) / 2; + + return std::sqrt (s * (s - ab) * (s - bc) * (s - ca)); + } + + + //------------------------------------------------------------------------- + // 2-dimension triangle area + template + T + area (util::point<2,T> a, util::point<2,T> b, util::point<2,T> c) + { + // | x1 y1 1 | + // area = 0.5 det | x2 y2 1 | + // | x3 y3 1 | + + return std::abs ( + -b.x * a.y + +c.x * a.y + +a.x * b.y + -c.x * b.y + -a.x * c.y + +b.x * c.y + ) / 2; + } + + + //------------------------------------------------------------------------- + // 3-dimension triangle area + template + T + area (util::point<3,T> a, util::point<3,T> b, util::point<3,T> c) + { + const auto ab = a - b; + const auto ac = a - c; + + return norm (cross (ab, ac)) / 2; + } +}; + +#endif