Spiral arc calculations

Overview

The problem is to find a good algorithm for deciding how many straight line segments to use to simulate a curve, when using coord_polar or coord_map.

Demonstration

We'll create some data points for a straight line.

dat <- data.frame(x = -5:5, y = seq(2, 8, length.out = 11))
dat
##     x   y
## 1  -5 2.0
## 2  -4 2.6
## 3  -3 3.2
## 4  -2 3.8
## 5  -1 4.4
## 6   0 5.0
## 7   1 5.6
## 8   2 6.2
## 9   3 6.8
## 10  4 7.4
## 11  5 8.0

Now we can plot the line in cartesian coordinates, and in polar coordinates, by simply using the x value for \( \theta \) and the y value for \( r \).

library(grid)
library(ggplot2)

# Plot the line in cartesian coordinates
p1 <-
ggplot(dat, aes(x, y)) + geom_point() + geom_line() + 
  ylim(0, 9) + xlim(-8, 8)

# Use polar coordinates
p2 <-
ggplot(dat, aes(x, y)) + geom_point() + geom_line() +
  ylim(0, 9) + xlim(-8, 8) + coord_polar()


# Plot them together
grid.newpage()
pushViewport(viewport(layout = grid.layout(1, 2)))
print(p1, vp = viewport(layout.pos.row = 1, layout.pos.col = 1))
print(p2, vp = viewport(layout.pos.row = 1, layout.pos.col = 2))

plot of chunk arc

The points that are equally-spaced in cartesian coordinates become unequally spaced in polar coordinates.
If you draw a straight line segment between each of the consecutive points, that the angle between each segment will be the same (I think).
If this is right, it makes one part of the problem easy: the line can broken up into pieces that are equally spaced in cartesian coordinates.

The next question is, how many pieces should it be broken into?

I think that the number of pieces should be proportional to the angle between the beginning and end of the arc.
That is, if you take a tangent vector at the beginning of the arc, and one at the end of the arc, what is the angle between them?
So now the problem is to find this angle.