make_trig_vec: Create trigonometric vectors

View source: R/lin-algebra.R

make_trig_vecR Documentation

Create trigonometric vectors

Description

Projects point within a plane to a points based on angle and distance starting from one starting point.

Usage

make_trig_vec(start, angle, dist, prolong = NULL, prolong.opt = "m", d = Inf)

Arguments

start

Numeric vector of length two. First value sets x- and second value sets y-coordinate of the start.

angle

Angles of the trigonometric vectors.

dist

Distances from center to final points.

prolong

Numeric vector or NULL. If numeric, sets values with which the projected vector is prolonged. For that, an additional set of xend and yend variables is added to the output data.frame for each prolonging.

prolong.opt

Character value. Either 'a' or 'm'. If 'a', the prolong value is added to the distance. If 'm', the distance is multiplied with the prolong value. To reduce the length of the vector instead of prolonging it, set ⁠prolong.opt = *'m'*⁠ and prolong < 1 or ⁠prolong.opt = *'a'*⁠ and prolong < 0.)

d

Number of digits to which the output values are rounded.

Details

Length of angle and dist should be equal or at least recyclable. This means one of the two argument lengths should be a multiple of the other one.

d only affects the variables angle and dist.

Value

Data.frame with four variables x, y, xend and yend. Each row corresponds to a trigonometric vector. Values of x and y are equal to input for center. Variables xend and yend correspond to the endpoints of each projection.

Examples


library(tidyverse)

# argument input options -----

# not recyclable - fails

angle <- c(0, 90, 180)

dist <- c(10, 20)

trig_vecs <- make_trig_vec(start = c(0, 0), angle = angle, dist = dist)


# equal length - works

angle <- c(0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300)

dist <- c(5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55)

trig_vecs <- make_trig_vec(start = c(0, 0), angle = angle, dist = dist)

trig_vecs


# recyclable - works

angle <- c(0, 55, 75, 200)

dist <- c(10)

trig_vecs <- make_trig_vec(start = c(0, 0), angle = angle, dist = dist)

trig_vecs


# plot results -----

ggplot(mapping = aes(x = x, y = y)) +
  geom_segment(
    data = trig_vecs,
    mapping = aes(xend = xend, yend = yend),
    arrow = arrow()
  )


# store prolonged projections -----

angle <- c(25, 135, 245, 315)
dist <- c(20, 22.5 , 25, 27.5)

trig_vecs_prol <-
  make_trig_vec(
    start = c(0, 0),
    angle = angle,
    dist = dist,
    prolong = c(1.1, 0.5), # stores additional projections with 1.1 and 0.5 times the length
    d = 2 # round info vars to two digits
    )

trig_vecs_prol

ggplot(mapping = aes(x = x, y = y)) +
  geom_segment(
    data = trig_vecs_prol,
    mapping = aes(xend = xend, yend = yend), # segments with original projection
    arrow = arrow()
  ) +
   geom_text(
    data = trig_vecs_prol,
    mapping = aes(
      x = xend_p1,
      y = yend_p1,
      label = str_c(angle, "; ", dist)
     )
   )


kueckelj/confuns documentation built on July 4, 2024, 4:53 p.m.