This vignette provides some further detail on the chapter Scripts, algorithms and functions (see ). It starts with the same input data:

x_coords = c(10, 0, 0, 12, 20, 10)
y_coords = c(0, 0, 10, 20, 15, 0)
poly_mat = cbind(x_coords, y_coords)
O = poly_mat[1, ] # create a point representing the origin
T1 = rbind(O, poly_mat[2:3, ], O) # create 'triangle matrix'
C1 = (T1[1, ] + T1[2, ] + T1[3, ]) / 3 # find centroid

The function t_area generalizes the solution by taking any object x, assumed to have the same dimensions as the triangle represented in T1. We can verify it works on the triangle matrix T1 as follows:

## [1] 50

Likewise we can create a function that finds the triangle’s centroid:

## x_coords y_coords 
## 3.333333 3.333333

The next stage is to create the second triangle and calculate its area and centroid. This is done in the code chunk below, which binds the 3rd and 4th coordinates onto the 1st, and uses the same functions we created above to calculate its area and width:

From this point it is not a big leap to see how to create the 3rd and final triangle that constitutes the polygon represented by poly_mat.

Note: it would also be possible to create the polygons with the decido package (code not run):