R/resistor.R

resistor <- function(x0, y0, x1, y1,
                     points=FALSE,
                     length=0.08, width=length/(3 * 2 * sqrt(2)),
                     label, pos,
                     col=par('col'), lwd=par('lwd'), cex=par('cex'))
{
    if (missing(x0)) stop("must provide x0")
    if (missing(y0)) stop("must provide y0")
    if (missing(x1)) stop("must provide x1")
    if (missing(y1)) stop("must provide y1")
    horizontal <- y0 == y1         # FIXME rounding issues?
    wirelength <- if (horizontal) x1 - x0 else y1 - y0
    if (horizontal) {
        lhs <- x0 + wirelength / 2 - length / 2
        rhs <- x0 + wirelength / 2 + length / 2
        segments(x0, y0, lhs, y0, col=col, lwd=lwd) # wire to component
        segments(rhs, y0, x0+wirelength, y0, col=col, lwd=lwd) # component to wire
        nzigzag <- round(length / width / (2 * sqrt(2)))
        zigzagx <- seq(0, length, length.out=5*nzigzag)
        zigzagy <- rep(c(0, width, 0, -width), length.out=5*nzigzag)
        lines(lhs + zigzagx, y0 + zigzagy, lwd=2*lwd)
        if (points) {
            points(x0, y0, pch=20, col=col)
            points(x0+wirelength, y0, pch=20, col=col)
        }
        if (!missing(label)) {
            if (missing(pos)) pos <- 1
            if (pos == 1) label(x0 + wirelength / 2, y0 - width/2, label, pos=pos, col=col, cex=cex)
            else if (pos == 3) label(x0 + wirelength / 2, y0 + width/4, label, pos=pos, col=col, cex=cex)
            else label(x0 + wirelength / 2, y0 - width/2, label, pos=pos, col=col, cex=cex)
        }
    } else {
        bot <- y0 + wirelength / 2 - length / 2
        top <- y0 + wirelength / 2 + length / 2
        segments(x0, y0, x0, bot, col=col, lwd=lwd) # wire to component
        segments(x0, top, x0, y0+wirelength, col=col, lwd=lwd) # comp to wire
        nzigzag <- round(length / width / (2 * sqrt(2)))
        zigzagx <- rep(c(0, width, 0, -width), length.out=5*nzigzag)
        zigzagy <- seq(0, length, length.out=5*nzigzag)
        lines(x0 + zigzagx, bot + zigzagy, lwd=2*lwd)
        if (points) {
            points(x0, y0, pch=20, col=col, cex=cex)
            points(x0, y0+wirelength, pch=20, col=col, cex=cex)
        }
        if (!missing(label)) {
            if (missing(pos)) pos <- 2
            dy <- par('cin')[2] / par('fin')[2] / 4
            if (pos == 2)
                label(x0 - width/4, y0 + wirelength/2 - dy,
                      label, pos=pos, col=col, cex=cex)
            else if (pos == 4)
                label(x0 + width/2, y0 + wirelength/2 - dy,
                      label, pos=pos, col=col, cex=cex)
            else
                label(x0 - width / 2, y0 + wirelength/2,
                      label, pos=pos, col=col, cex=cex)
        }
    }
    invisible(c(x0, y0, x1, y1))
}
dankelley/circuit documentation built on May 14, 2019, 4:09 p.m.