Skip to content

Instantly share code, notes, and snippets.

@loukesio
Created August 20, 2023 20:12
Show Gist options
  • Select an option

  • Save loukesio/d9fa7030bee1844458329c654376fa3e to your computer and use it in GitHub Desktop.

Select an option

Save loukesio/d9fa7030bee1844458329c654376fa3e to your computer and use it in GitHub Desktop.
# Load the Rcpp library (assuming you've already loaded it earlier)
library(Rcpp)
library(showtext)
library(ggplot2)
library(ggforce)
library(ggfx)
## Loading Google fonts (https://fonts.google.com/)
font_add_google("Exo 2", "Exo")
showtext_auto()
# The C++ code
cppFunction('
DataFrame generate_lines_within_circle(double center_x, double center_y,
double radius, int num_lines) {
// RNGScope is a helper class that sets up the R random number generator
RNGScope scope;
NumericVector start_angles = runif(num_lines, 0, 2 * M_PI);
NumericVector end_angles = start_angles + runif(num_lines, 0, 2 * M_PI);
NumericVector start_x(num_lines);
NumericVector start_y(num_lines);
NumericVector end_x(num_lines);
NumericVector end_y(num_lines);
for(int i = 0; i < num_lines; ++i) {
start_x[i] = center_x + radius * cos(start_angles[i]);
start_y[i] = center_y + radius * sin(start_angles[i]);
end_x[i] = center_x + radius * cos(end_angles[i]);
end_y[i] = center_y + radius * sin(end_angles[i]);
}
return DataFrame::create(Named("start_x") = start_x,
Named("start_y") = start_y,
Named("end_x") = end_x,
Named("end_y") = end_y);
}
')
# Circle parameters
center_x <- 0.5
center_y <- 0.5
radius <- 0.4
# Number of lines to generate within the circle
num_lines <- 1000
# Generate random lines within the circle using the C++ function
lines_within_circle <- generate_lines_within_circle(center_x, center_y, radius, num_lines)
lines_within_circle
ggplot() +
with_outer_glow(geom_circle(aes(x0 = center_x, y0 = center_y, r = radius), fill = NA, color = "#333333"),colour="white",sigma=,expand=0.5) +
purrr::pmap(
.l = lines_within_circle,
.f = function(start_x, start_y, end_x, end_y) {
geom_segment(
aes(x = start_x, y = start_y, xend = end_x, yend = end_y),
color = 'gray',
linewidth = 0.1
)
}
) +
coord_fixed() +
labs(title="Moon") +
theme_void() +
theme(legend.position = "none",
plot.title=element_text(family="Exo", size=28, hjust=0.5, color="#8d96a3"),
panel.background = element_rect(fill = "#333333", color=NA),
plot.background = element_rect(fill = "#333333", color=NA),
panel.grid = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment