Skip to contents

Computes the coordinates of a spring from (x, y) to (xend, yend), in the same units as the coordinates. This is the geometry used by geom_spring(), which however computes it in mm on the screen. Use create_spring() to draw a spring in data coordinates, e.g. with ggplot2::geom_path() and ggplot2::coord_equal().

Usage

create_spring(
  x,
  y,
  xend,
  yend,
  diameter = 1,
  coils = 10,
  n = 50,
  shape = c("zigzag", "wave", "coil")
)

Arguments

x, y

Starting coordinates.

xend, yend

Ending coordinates.

diameter

Diameter of the coils, in the units of x and y.

coils

Number of coils (turns) in the spring.

n

Number of points used to draw each coil.

shape

How the spring is drawn: "zigzag", "wave" or "coil". See geom_spring().

Value

A data frame with columns x and y. It has zero rows when the spring has zero length.

Examples

spring <- create_spring(x = 4, y = 2, xend = 10, yend = 6,
                        diameter = 1, coils = 8)

library(ggplot2)
ggplot(spring, aes(x, y)) +
  geom_path() +
  coord_equal()