DarianFlorianVoda / BioShapes

Bachelor Thesis R Package
0 stars 0 forks source link

[BioShapes] Helix Shape #58

Closed discoleo closed 2 years ago

discoleo commented 2 years ago

Helix Shape

Add code to draw a helix shape.

Graphic.Objects.R

Code to draw the helix.

# n = number of loops;
# N = number of points to draw curve;
# A = amplitude;
# phi = phase shift of sinusoid;
helix = function(p1, p2, n=3, 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;
    }
    sdiv = 1 / sqrt(slope^2 + 1);
    # Rotation matrix: by column
    # rotm = matrix(sdiv * c(1, s, -s, 1), ncol=2, nrow=2);
    #
    n = 2*n*pi;
    ninv = 1 / n;
    t  = seq(0, n, length.out=N);
    x  = l*t*ninv;
    y  = A*sin(t + phi);
    dx = (x - slope*y) * sdiv; # + p1[1];
    dy = (slope*x + 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);
}

Helper.R

Code to draw the shape.

### Bio-Shapes
lines.bioshape = function(x, lwd=NULL, col=1, ...) {
  lines.object.base(x, lwd=lwd, col=col, ...)
  invisible();
}

Graphic.Objects.Tests.R

Add the examples/tests.

### Helix / DNA

### Ex 1a:
lst1 = helix(c(1,1), c(8,3))
lst2 = helix(c(1,1), c(8,3), phi=-pi/2)
plot.base()
lines(lst1)
lines(lst2)

### Ex 1b:
dy = 3; n = 2.5;
lst1 = helix(c(1, 1 + dy), c(8, 3 + dy), n=n)
lst2 = helix(c(1, 1 + dy), c(8, 3 + dy), n=n, phi=-pi/2)
# plot.base()
lines(lst1)
lines(lst2)

### Ex 3:
lst1 = helix(c(1,1), c(7,9))
lst2 = helix(c(1,1), c(7,9), phi=-pi/2)
plot.base()
lines(lst1, col="red", lwd=2)
lines(lst2, col="red", lwd=2)
DarianFlorianVoda commented 2 years ago

Added.