/* * 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 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