1

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 Answers1

3

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

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • do you have any idea what this means here: atan2 (y,x) is defined as theta = arctan(y,x) + pi if x<0, theta =pi/2 if x=0, and theta = arctan(y/x) if x > 0, and it ensures that theta falls within the range (0,pi). I am trying to follow someone approach – user22178063 Sep 02 '23 at 12:37