DarianFlorianVoda / BioShapes

Bachelor Thesis R Package
0 stars 0 forks source link

[Objects] New shape: spirals #61

Closed discoleo closed 2 years ago

discoleo commented 2 years ago

Spirals

Add to Objects.R the new shape: spirales.

#' @export
spirals = function(p1, p2, n=5.5, A=1, phi=0, N=128, slope=NULL) {
  if(is.null(slope)) {
    x = c(p1[1], p2[1]);
    y = c(p1[2], p2[2]);
    slope = compute_slope(x, y);
    l = sqrt((p1[1]- p2[1])^2 + (p1[2]- p2[2])^2);
  } else {
    if(length(p2) > 1) stop("Provide either: length and slope, or the 2 endpoints!")
    l = p2;
  }
  #
  n = 2*n*pi;
  ninv = 1 / n;
  v = l * ninv;
  t = seq(0, n, length.out=N);
  # Rotation matrix: by column
  # rotm = matrix(sdiv * c(1, s, -s, 1), ncol=2, nrow=2);
  if(slope == -Inf || (slope != Inf && p1[2] > p2[1])){
    v = - v;
  } else { phi = phi + pi; }
  x  = v*t;
  y  = A*sin(t + phi);
  xc = x + cos(t + phi);
  #
  if(abs(slope) == Inf) {
    sgn = sign(slope);
    dx  = y;
    dy  = xc;
    lst = list(x = p1[1] + dx, y = p1[2] + dy);
    lst = list(lst);
    class(lst) = c("bioshape", class(lst));
    return(lst);
  }
  sdiv = 1 / sqrt(slope^2 + 1);
  #
  dx = (xc - slope*y) * sdiv; # + p1[1];
  dy = (slope*xc + y) * sdiv; # + p1[2];
  lst = list(x = p1[1] + dx, y = p1[2] + dy);
  lst = list(lst);
  class(lst) = c("bioshape", class(lst));
  return(lst);
}

Tests

Add the following tests in Objects.Tests.R:

### Spirals / Coils

### Ex 1:
p1 = c(1,1); p2 = c(2,8); dx = c(2.5,0);
lst1 = spirals(p1, p2)
lst2 = spirals(p1 + dx, p2 + dx)
plot.base()
lines(lst1)
lines(lst2)

### Ex 2:
p1 = c(1,1); p2 = c(2,8); dx = c(3,0);
lst1 = spirals(p1, p2)
lst2 = spirals(p2 + dx, p1 + dx)
plot.base()
lines(lst1)
lines(lst2)

### Ex 3: Vertical
p1 = c(1,1); p2 = c(p1[1],8); dx = c(2.5,0);
lst1 = spirals(p1, p2)
lst2 = spirals(p2 + dx, p1 + dx)
plot.base()
lines(lst1)
lines(lst2)
DarianFlorianVoda commented 2 years ago

Added.