| Title: | Calculate Area of Triangles and Polygons |
|---|---|
| Description: | Calculate the area of triangles and polygons using the shoelace formula. Area may be signed, taking into account path orientation, or unsigned, ignoring path orientation. The shoelace formula is described at <https://en.wikipedia.org/wiki/Shoelace_formula>. |
| Authors: | Michael Sumner [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-2471-7511>) |
| Maintainer: | Michael Sumner <[email protected]> |
| License: | GPL-3 |
| Version: | 0.3.0 |
| Built: | 2026-05-27 07:34:40 UTC |
| Source: | https://github.com/hypertidy/area |
A minimal mesh with one hole mm and a map of Tasmania with multiple
holes in planar straight line graph format from the RTriangle package.
mm_tri is a triangulated form of mm in RTriangle triangulation format.
The HOLE property is not yet set WIP.
str(mm)str(mm)
Calculate polygon area from a matrix of a closed polygon. Closed means that the first coordinate is the same as the last.
polygon_area(x, signed = FALSE)polygon_area(x, signed = FALSE)
x |
polygon in xy matrix |
signed |
defaults to |
Only one polygon can be input. We are using the normal definition of polygon which is a plane figure described by straight line segments.
Currently inputs are not checked but are assumed to have the last coordinate as a copy of the first aka 'closed'.
If signed = FALSE the absolute value of area is returned, otherwise the
sign reflects path orientation. Positive means counter-clockwise orientation.
The algorithm used was once on the internet at "w w w .cs.tufts.edu/comp/163/OrientationTests.pdf"
numeric vector of area
x <- c(2, 10, 8, 11, 7, 2) y <- c(7, 1, 6, 7, 10, 7) polygon_area(cbind(x, y), signed = TRUE) xy <- cbind(x = c(2.3, 1.5, 2.4, 4.5, 4.6, 5.4, 7.6, 8.6, 7.4, 5.1, 2.3), y = c(-1.4, 7.3, 22.2, 22.5, 14.4, 11.8, 16.4, 5, 0.8, -1.6, -1.4)) polygon_area(xy) ## xy is clockwise so area is negative polygon_area(xy, signed = TRUE) polygon_area(xy[nrow(xy):1, ], signed = TRUE) ## Rosetta code example ## https://rosettacode.org/wiki/Shoelace_formula_for_polygonal_area m <- rbind(c(3,4), c(5,11), c(12,8), c(9,5), c(5,6)) p <- m[c(1:nrow(m), 1), ] ## close it polygon_area(p)x <- c(2, 10, 8, 11, 7, 2) y <- c(7, 1, 6, 7, 10, 7) polygon_area(cbind(x, y), signed = TRUE) xy <- cbind(x = c(2.3, 1.5, 2.4, 4.5, 4.6, 5.4, 7.6, 8.6, 7.4, 5.1, 2.3), y = c(-1.4, 7.3, 22.2, 22.5, 14.4, 11.8, 16.4, 5, 0.8, -1.6, -1.4)) polygon_area(xy) ## xy is clockwise so area is negative polygon_area(xy, signed = TRUE) polygon_area(xy[nrow(xy):1, ], signed = TRUE) ## Rosetta code example ## https://rosettacode.org/wiki/Shoelace_formula_for_polygonal_area m <- rbind(c(3,4), c(5,11), c(12,8), c(9,5), c(5,6)) p <- m[c(1:nrow(m), 1), ] ## close it polygon_area(p)
Calculate triangle area from a matrix of coordinates. Triangles are composed of three coordinates, so the matrix should have this as triplets of rows one after the other.
triangle_area(x, signed = FALSE)triangle_area(x, signed = FALSE)
x |
coordinates x,y triplets matrix where 'nrow(x) = ntriangles*3' |
signed |
defaults to |
If signed = FALSE the absolute value of area is returned, otherwise the
sign reflects path orientation. Positive means counter-clockwise orientation.
The algorithm was once documented at 'w w w cs.tufts.edu/comp/163/OrientationTests.pdf'
numeric vector of area
sum(triangle_area(mm_tri$P[t(mm_tri$T), ]))sum(triangle_area(mm_tri$P[t(mm_tri$T), ]))
Compute the unsigned surface area of triangles defined by vertex coordinates. Works with both 2D (xy) and 3D (xyz) input. For 2D input, z coordinates are assumed to be zero.
triangle_surface_area(x)triangle_surface_area(x)
x |
A matrix with 2 or 3 columns (x, y) or (x, y, z). Each set of 3 consecutive rows defines one triangle. |
The area is calculated using the cross product method:
Area = 0.5 * ||(v1 - v0) x (v2 - v0)||
For 2D input, this reduces to the absolute value of the shoelace formula.
A numeric vector of surface areas, one per triangle.
# 2D triangle (unit right triangle) tri_2d <- matrix(c(0, 0, 1, 0, 0, 1), ncol = 2, byrow = TRUE) triangle_surface_area(tri_2d) # 0.5 # 3D triangle tri_3d <- matrix(c(0, 0, 0, 1, 0, 0, 0, 1, 1), ncol = 3, byrow = TRUE) triangle_surface_area(tri_3d)# 2D triangle (unit right triangle) tri_2d <- matrix(c(0, 0, 1, 0, 0, 1), ncol = 2, byrow = TRUE) triangle_surface_area(tri_2d) # 0.5 # 3D triangle tri_3d <- matrix(c(0, 0, 0, 1, 0, 0, 0, 1, 1), ncol = 3, byrow = TRUE) triangle_surface_area(tri_3d)