Package 'area'

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]
Maintainer: Michael Sumner <[email protected]>
License: GPL-3
Version: 0.2.0
Built: 2024-11-14 03:28:28 UTC
Source: https://github.com/hypertidy/area

Help Index


Planar straight line graphs and triangulations

Description

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.

Details

mm_tri is a triangulated form of mm in RTriangle triangulation format. The HOLE property is not yet set WIP.

Examples

str(mm)

Area of polygon

Description

Calculate polygon area from a matrix of a closed polygon. Closed means that the first coordinate is the same as the last.

Usage

polygon_area(x, signed = FALSE)

Arguments

x

polygon in xy matrix

signed

defaults to FALSE and absolute value of area is returned

Details

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"

Value

numeric vector of area

Examples

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)

Area of triangles

Description

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.

Usage

triangle_area(x, signed = FALSE)

Arguments

x

coordinates x,y triplets matrix where 'nrow(x) = ntriangles*3'

signed

defaults to FALSE and absolute value of area is returned, if TRUE negative means clockwise 'p->q->r' turns right and positive means counter-clockwise 'p->q->r' turns left

Details

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'

Value

numeric vector of area

Examples

sum(triangle_area(mm_tri$P[t(mm_tri$T), ]))