/Shapes

R Utilities used to generate geometric shapes

Primary LanguageROtherNOASSERTION

License: CC BY-NC-SA 4.0

Shapes R-Package

This package is used to generates x, y coordinate pairs for basic shapes:

  1. Line.vertical, Line.horizontal
  2. Rectangle
  3. Triangle
  4. Circle
  5. Trapezoid
  6. Segment

Note: Structural validation of input specifications is done before generating the coordinates.

This package also contains a set of utility functions that make working with shapes easier. Utilities include:

  1. get.width: Get the width of a shape.
  2. get.height: Get the height of a shape.
  3. translate: Translate a shape by using an offset.
  4. translate.x: Translate a shape along the x-axis.
  5. translate.y: Translate a shape along the y-axis.
  6. shrink.height: Shrink a shape height by an amount.
  7. join: Join two shapes together.

Installation

This package is not available on CRAN. Use devtools to either install from GitHub or build from source and install locally.

Install from GitHub

  1. Use the following command to install the package from GitHub:
devtools::install_github("https://github.com/FlippieCoetser/Shapes")

Build from Source

  1. Clone the repository to your local machine.
git clone https://github.com/FlippieCoetser/Shapes.git
  1. Navigate to the directory where the repository was cloned.

  2. Build the package.

devtools::build()
  1. Install .tar.gz file.
install.packages("path_to_file/tar_gz_file", repos = NULL, type = "source")

Load the Package

There are two ways to load the package: using the library function or the package namespace.

  1. Using the library function.
library(Shapes)
generate <- Generator()
validate <- Validator()
  1. Using the package namespace.
generate <- Shapes::Generator()
validate <- Shapes::Validator()

Shape Generator Usage

Generate Shape Coordinates for the following shapes:

  1. Line (vertical and horizontal)
specifications <- list()
specifications[['length']] <- 1

line <- specifications |> generate[['Line.horizontal']]()
  1. Rectangle
specifications <- list()
specifications[['width']]  <- 1
specifications[['height']] <- 0.5

rectangle <- specifications |> generate[['Rectangle']]()
  1. Triangle
specifications <- list()
specifications[['base']]   <- 1
specifications[['height']] <- 1.5

triangle <- specifications |> generate[['Triangle']]()
  1. Circle
specifications <- list()
specifications[['radius']] <- 1

circle <- specifications |> generate[['Circle']]()
  1. Trapezoid
specifications <- list()
specifications[['bottom']] <- 1
specifications[['top']]    <- 0.5
specifications[['height']] <- 1

trapezoid <- specifications |> generate[['Trapezoid']]()
  1. Segment
specifications <- list()
specifications[['radius']] <- 1

segment <- specifications |> generate[['Segment']]()

Note: An optional align parameter can be passed to the generator function.

The align parameter can be one of the following:

  1. corner: The Left Bottom Corner of shapes will be at the origin (0,0) and also the default value
  2. center: The center of shapes will be at the origin (0,0)
  3. horizontal: The shapes will be centred horizontally with the bottom at y = 0
  4. vertical: The shapes will be centred vertically with the left side at x = 0

Generate Shape Coordinates for the following shapes:

  1. Centered Aligned Rectangle
specifications <- list()
specifications[['width']]  <- 1
specifications[['height']] <- 0.5

rectangle <- specifications |> generate[['Rectangle']](align = 'center')

Shape Validator Usage

Validate specifications of shapes and throw exception with meaningful message, for example:

  1. Define a specification with missing parameters.
specifications <- list()
specifications[['width']] <- 1
  1. Validate the specifications.
specifications |> validate[['Rectangle']]()
  1. The following exception will be thrown:
"Error: Attribute.NULL: 'height' does not exist."