Closed discoleo closed 2 years ago
An improved calculation of the scaling:
### Shift Line # d = distance to shift (translate); shiftLine = function(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); } else { if(missing(y)) { # both coordinates encoded using parameter x; y = x[2]; x = x[1]; } } ### Vertical Line if(abs(slope) == Inf) { if(length(d) == 1) { r = data.frame(x = x + d, y = y); } else { r = lapply(seq(along=d), function(id) { data.frame(x = x + d[id], y = y, id=id); }) r = do.call(rbind, r); } return(r) } ### Horizontal Line if(slope == 0) { d = d * scale; if(length(d) == 1) { r = data.frame(x = x, y = y + d); } else { r = lapply(seq(along=d), function(id) { data.frame(x = x, y = y + d[id], id=id); }) r = do.call(rbind, r); } return(r) } ### Oblique Line sl.orto = - 1 / slope; sl2 = sqrt(sl.orto^2 + 1/scale^2); sl.orto = sl.orto * scale; # shift Start- & End-points: shift.f = function(x, y, id) { delta = d[id] / sl2; x.sh = x + delta / scale; y.sh = delta*sl.orto + y; data.frame(x=x.sh, y=y.sh, id=id); } rez = lapply(seq(length(d)), function(id) shift.f(x, y, id)) rez = data.frame(do.call(rbind, rez)); return(rez); }
Modified.
[Helper] shiftLine Function
An improved calculation of the scaling: