Transform between coordinate systems

xy_to_cartesian(x, y, track_index = current_track_index())

xy_to_polar(x, y, track_index = current_track_index(), flip = TRUE)

polar_to_cartesian(theta, r)

cartesian_to_polar(x, y)

cartesian_to_xy(x, y, track_index = current_track_index())

Arguments

x

X-locations of the data points.

y

Y-locations of the data points.

track_index

Index of the track.

flip

If it is FALSE, it returns theta for the original spiral (before flipping).

theta

Angles, in radians.

r

Radius.

Value

xy_to_cartesian() returns A data frame with two columns: x and y.

xy_to_polar() returns a data frame with two columns: theta (in radians) and r (the radius).

polar_to_cartesian() returns a data frame with two columns: x and y.

cartesian_to_polar() returns a data frame with two columns: theta (in radians) and r (the radius).

cartesian_to_xy() returns a data frame with two columns: x and y.

Details

There are three coordinate systems: the data coordinate system (xy), the polar coordinate system (polar) and the canvas coordinate system (cartesian). The canvas coordinates correspond to the "native" coordinates of the viewport where the graphics are drawn.

Note different settings of flip and reverse in spiral_initialize() affect the conversion.

xy_to_cartesian() converts from the data coordinate system to the canvas coordinate system.

xy_to_polar() converts from the data coordinate system to the polar coordinate system.

polar_to_cartesian() converts from the polar coordinate system to the canvas coordinate system.

cartesian_to_polar() converts from the canvas coordinate system to the polar coordinate system.

cartesian_to_xy() converts from the canvas coordinate system to the data coordinate system. The data points are assigned to the nearest inner spiral loops (if the point is located inside a certain spiral loop, the distance is zero).

Examples

x = runif(2)
y = runif(2)
spiral_initialize(xlim = c(0, 1))
spiral_track(ylim = c(0, 1))
spiral_points(x, y)

xy_to_cartesian(x, y)
#>            x        y
#> 1 -0.7985684 1.617187
#> 2 -2.3279481 3.807875
xy_to_polar(x, y)
#>       theta        r
#> 1  8.312658 1.803609
#> 2 27.252262 4.463099

x = runif(100, -4, 4)
y = runif(100, -4, 4)
spiral_initialize(xlim = c(0, 1))
spiral_track(ylim = c(0, 1))
df = cartesian_to_xy(x, y)
# directly draw in the viewport
grid.points(x, y, default.units = "native")
# check whether the converted xy are correct (should overlap to the previous points)
spiral_points(df$x, df$y, pch = 16, gp = gpar(col = 2))