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

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:

T2 = rbind(Origin, poly_mat[3:4, ], Origin)
A2 = t_area(T2)
C2 = t_centroid(T2)

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):