# Load required libraries
pacman::p_load(pacman, ggplot2, dplyr, ggforce)

# Function to create a pentagon (approximation of apple's cross-section)
create_pentagon <- function(radius = 0.5, center_x = 0, center_y = 0) {  
  angles <- seq(0, 2 * pi, length.out = 6) # 5 points + 1 to close
  data.frame(
    x = center_x + radius * cos(angles),
    y = center_y + radius * sin(angles)
  )
}

# Create the outer circle (apple shape)
apple_circle <- data.frame(
  x = 0,
  y = 0,
  radius = 1.5
)

# Create the pentagon (seed pattern inside the apple)
pentagon <- create_pentagon(radius = 0.5)  

# Extract the coordinates of the 5 points of the pentagon to place seeds
seeds <- pentagon %>%
  slice(1:5) %>%
  mutate(radius = 0.08)  # Small radius for the seeds

# Plot the apple cross-section with seeds at pentagram points
ggplot() +
  # Draw the apple as a circle
  geom_circle(data = apple_circle, aes(x0 = x, y0 = y, r = radius), 
              color = "#99cc00", fill = "#ffffff", linewidth = 1) +
  # Draw the pentagon inside the apple
  geom_polygon(data = pentagon, aes(x = x, y = y), 
               fill = "#ccffcc", color = "#000000", size = 1) +
  # Draw seeds at the points of the pentagram
  geom_circle(data = seeds, aes(x0 = x, y0 = y, r = radius), 
              fill = "#000000", color = NA) +  # Seeds as black circles
  coord_fixed() +  # Equal scaling for both axes
  theme_void() +  # Clean the plot
  ggtitle("Apple Cross-Section with Seeds at Pentagram Points")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

# Function to create the Flower of Life 
flower_of_life <- function(radius = 1, n_layers = 5) {
  # Initialise an empty data frame to store the circles' positions
  circles <- data.frame(x0 = numeric(0), y0 = numeric(0))
  
  # Loop to create concentric layers of circles
  for (i in 0:(n_layers - 1)) {
    num_circles <- 6 * i
    if (num_circles == 0) {
      # First central circle
      circles <- rbind(circles, data.frame(x0 = 0, y0 = 0))
    } else {
      # Create outer circles in the hexagonal grid for each layer
      for (j in 0:(num_circles - 1)) {
        angle <- 2 * pi * j / num_circles
        x_shift <- i * radius * cos(angle)
        y_shift <- i * radius * sin(angle)
        circles <- rbind(circles, data.frame(x0 = x_shift, y0 = y_shift))
      }
    }
  }
  return(circles)
}

# Generate flower of life data with more circles
flower_data <- flower_of_life(radius = 1, n_layers = 10)  # Increase n_layers for more complexity

# Plot the Flower of Life
ggplot(flower_data) +
  geom_circle(aes(x0 = x0, y0 = y0, r = 1), 
              color = "#000000", fill = NA, size = 0.7) +
  coord_fixed() + # Equal scaling for both axes
  theme_void() + # Clean the plot
  ggtitle("Flower of Life")