Search code examples
r

How to smooth an arc


I am trying to plot an arc of circle using three points, and I got this image enter image description here

But, as you see the arc is not really smooth, as you see the "zigzags" within the path. How can we remove them?

Here is part of my code:

library(ggplot2)
plot_arc <- function(C, r, P1, P2, P3) {
  t <- seq(atan2(P1[2] - C[2], P1[1] - C[1]), atan2(P2[2] - C[2], P2[1] - C[1]), length.out = 100000)
  x <- C[1] + r * cos(t)
  y <- C[2] + r * sin(t)
  data <- data.frame(x = x, y = y)
  
  p <- ggplot() +
    geom_path(data = data, aes(x = x, y = y), color = "blue", size=1.) +
    geom_point(aes(x = P1[1], y = P1[2]), color = "red", size = 3) +
    geom_point(aes(x = P2[1], y = P2[2]), color = "green", size = 3) +
    geom_point(aes(x = P3[1], y = P3[2]), color = "yellow", size = 3) +
    coord_fixed() 
 
  print(p) 
}

plot_arc(C_optimal, r_optimal, P1, P2, P3)

Solution

  • It looks zigzag on the screen due to the poor screen resolution. If you save the plot to a file, specifying a high dpi, then you'll get a much smoother arc. Similarly, if you save the plot to a pdf file.

    plot_arc(C_optimal, r_optimal, P1, P2, P3)
    ggsave("arc.jpeg", dpi=500)
    

    enter image description here