DarianFlorianVoda / BioShapes

Bachelor Thesis R Package
0 stars 0 forks source link

[Arrows] Correct scale when shifting point #52

Closed discoleo closed 2 years ago

discoleo commented 2 years ago

Arrows: Correct Scale

Helper Functions

The shift of a point is now also scale-invariant.

shiftPoint = function(p, x, y, d=1, slope=NULL, scale=1) {
  if(is.null(slope)) {
    if(length(x) < 2 || length(y) < 2)
      stop("The base-line requires 2 points!");
    # TODO: handle if more than 2 points!
    slope = compute_slope(x,y);
  }
  if(length(p) < 2) stop("Point needs both x & y coordinates!");
  # V line: if(x[1] == x[2]) {
  if(abs(slope) == Inf) {
    r = cbind(x = p[1], y = p[2] + d);
    return(r)
  }
  slope.sqrt = 1 / sqrt(slope^2 + scale^2);
  dx = d * slope.sqrt;
  dy = dx * slope;
  xsh = p[[1]] + dx;
  ysh = p[[2]] + dy;
  return(cbind(x=xsh, y=ysh));
}

Arrow.Heads

arrowHeadSimple = function(x, y, slope, d=-1, dV=c(-d, d), scale=1) {
  p = if(d == 0) matrix(c(x, y), nrow=1, ncol=2)
  else shiftPoint(c(x, y), slope=slope, d = d, scale=scale);
  pV = shiftLine(p, slope=slope, d = dV, scale=scale);
  arrHead = list(
    x = c(pV[1,1], x, pV[2,1]),
    y = c(pV[1,2], y, pV[2,2]));
  return(arrHead);
}

Tests

### Test 3
x = c(0, 4); y = c(1, 60);
x2 = c(0, 6); y2 = c(1, 20);
d = -2; d.head = c(-1/2, 1/2);
scale = (100/12) * aspect_ratio_max;
plot.base(ylim = c(0,100))
a1 = arrowSimple(x, y, d=d, d.head=d.head, lwd=2, scale=scale);
a2 = arrowSimple(x2, y2, d=d, d.head=d.head, lwd=2, scale=scale);
a3 = arrowSimple(c(x[1], 5), c(y[1], y[1]), d=d, d.head=d.head, lwd=2, scale=scale);
a4 = arrowSimple(c(x[1], x[1]), c(y[1], 50), d=d, d.head=d.head, lwd=2, scale=scale);
# Head
h1 = a1$Head[[1]]
h2 = a2$Head[[1]]
h3 = a3$Head[[1]]
h4 = a4$Head[[1]]
# - visual aids:
lines(h1$x[c(1,3)], h1$y[c(1,3)], col="green")
lines(h2$x[c(1,3)], h2$y[c(1,3)], col="green")
lines(h3$x[c(1,3)], h3$y[c(1,3)], col="green")
lines(h4$x[c(1,3)], h4$y[c(1,3)], col="green")
DarianFlorianVoda commented 2 years ago

Added! Also added scale=scale for each arrowhead, not only simple one.