R/get_tree_matrix.R

Defines functions get_tree_matrix

Documented in get_tree_matrix

#' Extract the Fractal Tree Matrix from a Tree Data Frame
#'
#' @param tree_df A data frame containing the tree data, typically resulting from the `build_tree` function. The data frame should include columns `from_x`, `from_y`, `to_x`, and `to_y`, which represent the coordinates of the tree segments.
#'
#' @return A matrix of unique coordinates (x, y) representing the positions of the tree's segments.
#' @details This function extracts the start and end coordinates of the tree segments and returns the result as a matrix.
#'
#' @export
#'
get_tree_matrix <- function(tree_df){

  tree_df %>%
    dplyr::select(x = from_x, y = from_y) %>%
    dplyr::bind_rows(
      tree_df %>%
        dplyr::select(x = to_x, y = to_y)
    ) %>%
    dplyr::distinct() %>%
    as.matrix() %>%
    unname()

}

Try the fractalforest package in your browser

Any scripts or data that you put into this service are public.

fractalforest documentation built on July 3, 2025, 1:09 a.m.