how to convert Cartesian coordinates to polar with certain origin?

150 views Asked by At

cart2polar assumes 0 ,0 origin; I need to know how to change it.

My origin is the x and y

 y <- 0.23
 x <- 81.05

cart2polar <- function(x, y) {
  data.frame(theta = sqrt(x^2 + y^2), r = atan2(x,y))
 }
1

There are 1 answers

0
Allan Cameron On BEST ANSWER

You would simply subtract the origin points from the data. However, your current function is wrong. You have the theta as a Pythagorean distance and r as the angle. You also have the arguments in atan2 round the wrong way.

The corrected function should be:

cart2polar <- function(x, y, x0 = 0, y0 = 0) {
  x <- x - x0
  y <- y - y0
  data.frame(r = sqrt(x^2 + y^2), theta = atan2(y, x))
}

In your case you would use it as follows:

cart2polar(df$x, df$y, x0 = 0.23, y0 = 81.05)

For example, polar co-ordinates from the origin would look like this:

plot(df, xlim = c(0, 100), ylim = c(0, 100))
df2 <- cart2polar(df$x, df$y)
segments(0, 0, df2$r * cos(df2$theta), df2$r * sin(df2$theta))

enter image description here

But from your new origin would be:

y <- 0.23
x <- 81.05

plot(df, xlim = c(0, 100), ylim = c(0, 100))
df2 <- cart2polar(df$x, df$y, x, y)
segments(x, y, x + df2$r * cos(df2$theta), y + df2$r * sin(df2$theta))

enter image description here