Lagrange
triangle_area.h
1/*
2 * Copyright 2023 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13#include <lagrange/utils/span.h>
14
15#include <cmath>
16
17namespace lagrange {
18
28template <typename Scalar>
30{
31 const Scalar n0 = (a[1] - b[1]) * (a[2] - c[2]) - (a[1] - c[1]) * (a[2] - b[2]);
32 const Scalar n1 = -(a[0] - b[0]) * (a[2] - c[2]) + (a[0] - c[0]) * (a[2] - b[2]);
33 const Scalar n2 = (a[0] - b[0]) * (a[1] - c[1]) - (a[0] - c[0]) * (a[1] - b[1]);
34
35 return std::sqrt(n0 * n0 + n1 * n1 + n2 * n2) / 2;
36}
37
47template <typename Scalar>
49{
50 return ((a[0] - b[0]) * (a[1] - c[1]) - (a[0] - c[0]) * (a[1] - b[1])) / 2;
51}
52
53} // namespace lagrange
::nonstd::span< T, Extent > span
A bounds-safe view for sequences of objects.
Definition: span.h:27
Main namespace for Lagrange.
Definition: AABBIGL.h:30
Scalar triangle_area_2d(span< const Scalar, 2 > a, span< const Scalar, 2 > b, span< const Scalar, 2 > c)
Compute 2D triangle signed area.
Definition: triangle_area.h:48
Scalar triangle_area_3d(span< const Scalar, 3 > a, span< const Scalar, 3 > b, span< const Scalar, 3 > c)
Compute 3D triangle area.
Definition: triangle_area.h:29