title: R-Cran: How can I rotate a Plot?
body: <p>I'm interested in calculating the maximum distance within a Polygon along a custom direction (alpha).</p>
  
  <p>The following link solved my question.
  <a href="https://gis.stackexchange.com/questions/32552/how-to-calculate-the-maximum-distance-within-a-polygon-in-x-direction-east-west">https://gis.stackexchange.com/questions/32552/how-to-calculate-the-maximum-distance-within-a-polygon-in-x-direction-east-west</a>.
  (first answer: R code implemented to perform the calculations and create the illustrations)</p>
  
  <p>The only drawback is that I have to rotate my Polygon before running the script, as it find the maximum in the x direction.</p>
  
  <p>The script also plot the Polygon with the maximum distance in the x-axes.</p>
  
  <p>As I don't get success in editing the Plot function, is there a way to rotate the plot in order to set the same direction for the maximum distance found and the custom direction?</p>
  
  <p>Thank you</p>
  
  <p>-- reproducible  example ---</p>
  
  <p>I add the following lines with my polygon (the x,y coordinates are rotated by alpha = 30 degree in the same way the 1st answer's author suggests).</p>
  
  <pre><code># --- modified lines ------
  x &lt;- c(29,  -3,  -9, -33, -11,  -3,  30)
  y &lt;- c(13, -38, -37, -22,  32,  39,  13)
  df = data.frame(x,y)
  p.raw = list(cbind(x=df$x, y=df$y))
  
  #scale &lt;- 10
  #p.raw = list(scale * cbind(x=c(0:10,7,6,0), y=c(3,0,0,-1,-1,-1,0,-0.5,0.75,1,4,1.5,0.5,3)),
  #             scale *cbind(x=c(1,1,2.4,2,4,4,4,4,2,1), y=c(0,1,2,1,1,0,-0.5,1,1,0)),
  #             scale *cbind(x=c(6,7,6,6), y=c(.5,2,3,.5)))
  
  #p.raw = list(cbind(x=c(0,2,1,1/2,0), y=c(0,0,2,1,0)))
  #p.raw = list(cbind(x=c(0, 35, 100, 65, 0), y=c(0, 50, 100, 50, 0)))
  
  # --- modified lines ------
  </code></pre>
  
  <p>to the R script from the previous link.</p>
  
  <pre><code>#
  # Plotting functions.
  #
  points.polygon &lt;- function(p, ...) {
    points(p$v, ...)
  }
  plot.polygon &lt;- function(p, ...) {
    apply(p$e, 1, function(e) lines(matrix(e[c("x.min", "x.max", "y.min", "y.max")], ncol=2), ...))
  }
  expand &lt;- function(bb, e=1) {
    a &lt;- matrix(c(e, 0, 0, e), ncol=2)
    origin &lt;- apply(bb, 2, mean)
    delta &lt;-  origin %*% a - origin
    t(apply(bb %*% a, 1, function(x) x - delta))
  }
  #
  # Convert polygon to a better data structure.
  #
  # A polygon class has three attributes:
  #   v is an array of vertex coordinates "x" and "y" sorted by increasing y;
  #   e is an array of edges from (x.min, y.min) to (x.max, y.max) with y.max &gt;= y.min, sorted by y.min;
  #   bb is its rectangular extent (x0,y0), (x1,y1).
  #
  as.polygon &lt;- function(p) {
    #
    # p is a list of linestrings, each represented as a sequence of 2-vectors 
    # with coordinates in columns "x" and "y". 
    #
    f &lt;- function(p) {
      g &lt;- function(i) {
        v &lt;- p[(i-1):i, ]
        v[order(v[, "y"]), ]
      }
      sapply(2:nrow(p), g)
    }
    vertices &lt;- do.call(rbind, p)
    edges &lt;- t(do.call(cbind, lapply(p, f)))
    colnames(edges) &lt;- c("x.min", "x.max", "y.min", "y.max")
    #
    # Sort by y.min.
    #
    vertices &lt;- vertices[order(vertices[, "y"]), ]
    vertices &lt;- vertices[!duplicated(vertices), ]
    edges &lt;- edges[order(edges[, "y.min"]), ]
  
    # Maintaining an extent is useful.
    bb &lt;- apply(vertices &lt;- vertices[, c("x","y")], 2, function(z) c(min(z), max(z)))
  
    # Package the output.
    l &lt;- list(v=vertices, e=edges, bb=bb); class(l) &lt;- "polygon"
    l
  }
  #
  # Compute the maximal horizontal interior segments of a polygon.
  #
  fetch.x &lt;- function(p) {
    #
    # Update moves the line from the previous level to a new, higher level, changing the
    # state to represent all edges originating or strictly passing through level `y`.
    #
    update &lt;- function(y) {
      if (y &gt; state$level) {
        state$level &lt;&lt;- y
        #
        # Remove edges below the new level from state$current.
        #
        current &lt;- state$current
        current &lt;- current[current[, "y.max"] &gt; y, ]
        #
        # Adjoin edges at this level.
        #
        i &lt;- state$i
        while (i &lt;= nrow(p$e) &amp;&amp; p$e[i, "y.min"] &lt;= y) {
          current &lt;- rbind(current, p$e[i, ])
          i &lt;- i+1
        }
        state$i &lt;&lt;- i
        #
        # Sort the current edges by x-coordinate.
        #
        x.coord &lt;- function(e, y) {
          if (e["y.max"] &gt; e["y.min"]) {
            ((y - e["y.min"]) * e["x.max"] + (e["y.max"] - y) * e["x.min"]) / (e["y.max"] - e["y.min"])
          } else {
            min(e["x.min"], e["x.max"])
          }
        }
        if (length(current) &gt; 0) {
          x.array &lt;- apply(current, 1, function(e) x.coord(e, y))
          i.x &lt;- order(x.array)
          current &lt;- current[i.x, ]
          x.array &lt;- x.array[i.x]     
          #
          # Scan and mark each interval as interior or exterior.
          #
          status &lt;- FALSE
          interior &lt;- numeric(length(x.array))
          for (i in 1:length(x.array)) {
            if (current[i, "y.max"] == y) {
              interior[i] &lt;- TRUE
            } else {
              status &lt;- !status
              interior[i] &lt;- status
            }
          }
          #
          # Simplify the data structure by retaining the last value of `interior`
          # within each group of common values of `x.array`.
          #
          interior &lt;- sapply(split(interior, x.array), function(i) rev(i)[1])
          x.array &lt;- sapply(split(x.array, x.array), function(i) i[1])
  
          print(y)
          print(current)
          print(rbind(x.array, interior))
  
  
          markers &lt;- c(1, diff(interior))
          intervals &lt;- x.array[markers != 0]
          #
          # Break into a list structure.
          #
          if (length(intervals) &gt; 1) {
            if (length(intervals) %% 2 == 1) 
              intervals &lt;- intervals[-length(intervals)]
            blocks &lt;- 1:length(intervals) - 1
            blocks &lt;- blocks - (blocks %% 2)
            intervals &lt;- split(intervals, blocks)  
          } else {
            intervals &lt;- list()
          }
        } else {
          intervals &lt;- list()
        }
        #
        # Update the state.
        #
        state$current &lt;&lt;- current
      }
      list(y=y, x=intervals)
    } # Update()
  
    process &lt;- function(intervals, x, y) {
      # intervals is a list of 2-vectors. Each represents the endpoints of
      # an interior interval of a polygon.
      # x is an array of x-coordinates of vertices.
      #
      # Retains only the intervals containing at least one vertex.
      between &lt;- function(i) {
        1 == max(mapply(function(a,b) a &amp;&amp; b, i[1] &lt;= x, x &lt;= i[2]))
      }
      is.good &lt;- lapply(intervals$x, between)
      list(y=y, x=intervals$x[unlist(is.good)])
      #intervals
    }
    #
    # Group the vertices by common y-coordinate.
    #
    vertices.x &lt;- split(p$v[, "x"], p$v[, "y"])
    vertices.y &lt;- lapply(split(p$v[, "y"], p$v[, "y"]), max)
    #
    # The "state" is a collection of segments and an index into edges.
    # It will updated during the vertical line sweep.
    #
    state &lt;- list(level=-Inf, current=c(), i=1, x=c(), interior=c())
    #
    # Sweep vertically from bottom to top, processing the intersection
    # as we go.
    #
    mapply(function(x,y) process(update(y), x, y), vertices.x, vertices.y)
  }
  
  # --- modified lines ------
  x &lt;- c(29,  -3,  -9, -33, -11,  -3,  30)
  y &lt;- c(13, -38, -37, -22,  32,  39,  13)
  df = data.frame(x,y)
  p.raw = list(cbind(x=df$x, y=df$y))
  
  #scale &lt;- 10
  #p.raw = list(scale * cbind(x=c(0:10,7,6,0), y=c(3,0,0,-1,-1,-1,0,-0.5,0.75,1,4,1.5,0.5,3)),
  #             scale *cbind(x=c(1,1,2.4,2,4,4,4,4,2,1), y=c(0,1,2,1,1,0,-0.5,1,1,0)),
  #             scale *cbind(x=c(6,7,6,6), y=c(.5,2,3,.5)))
  
  #p.raw = list(cbind(x=c(0,2,1,1/2,0), y=c(0,0,2,1,0)))
  #p.raw = list(cbind(x=c(0, 35, 100, 65, 0), y=c(0, 50, 100, 50, 0)))
  
  # --- modified lines ------
  
  p &lt;- as.polygon(p.raw)
  
  results &lt;- fetch.x(p)
  #
  # Find the longest.
  #
  dx &lt;- matrix(unlist(results["x", ]), nrow=2)
  length.max &lt;- max(dx[2,] - dx[1,])
  #
  # Draw pictures.
  #
  segment.plot &lt;- function(s, length.max, colors,  ...) {
    lapply(s$x, function(x) {
      col &lt;- ifelse (diff(x) &gt;= length.max, colors[1], colors[2])
      lines(x, rep(s$y,2), col=col, ...)
    })
  }
  gray &lt;- "#f0f0f0"
  grayer &lt;- "#d0d0d0"
  plot(expand(p$bb, 1.1), type="n", xlab="x", ylab="y", main="After the Scan")
  sapply(1:length(p.raw), function(i) polygon(p.raw[[i]], col=c(gray, "White", grayer)[i]))
  apply(results, 2, function(s) segment.plot(s, length.max, colors=c("Red", "#b8b8a8"), lwd=4))
  plot(p, col="Black", lty=3)
  points(p, pch=19, col=round(2 + 2*p$v[, "y"]/scale, 0))
  points(p, cex=1.25)
  </code></pre>
  
  <p>The resulting plot show the maximum distance as a red segment in the x-axes direction. As I need it in the original direction (rotate back by alpha 30 degree), I'm looking to the the maximum distance x,y coordinate, to perform a back rotation by -alpha. </p>
  
  <p>I get the maximum distance segment x coordinate from: </p>
  
  <pre><code> dx &lt;- matrix(unlist(results["x", ]), nrow=2)
   length.max &lt;- max(dx[2,] - dx[1,])
  </code></pre>
  
  <p>I'm not able to get the y coordinate.</p>
  
  <pre><code>apply(results, 2, function(s) segment.plot(s, length.max, colors=c("Red", "#b8b8a8"), lwd=2))
  </code></pre>
  
  <p>So, I'm looking for a way to rotate the resulting plot axes by alpha.</p>