Skip to content

Instantly share code, notes, and snippets.

@tbbooher
Created January 5, 2024 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbbooher/4d8b3ef8f528891fddf6236ca94baf16 to your computer and use it in GitHub Desktop.
Save tbbooher/4d8b3ef8f528891fddf6236ca94baf16 to your computer and use it in GitHub Desktop.
# Load required libraries
library(ggplot2)
library(ggforce)
library(gridExtra)
plot_laser_points <- function() {
plots <- vector("list", length = 9)
circle_radius <- 18 # Fixed circle radius of 18 mm
max_distance_from_center <- 10
for (i in 1:9) {
# Set the seed for reproducibility
set.seed(i+3)
repeat {
# Generate random points within 5mm of the circle center
x1 <- runif(1, -max_distance_from_center, max_distance_from_center)
y1 <- runif(1, -max_distance_from_center, max_distance_from_center)
x2 <- runif(1, -max_distance_from_center, max_distance_from_center)
y2 <- runif(1, -max_distance_from_center, max_distance_from_center)
# Calculate Δd and d
Δd <- 300
d <- 50
# Avoid division by zero
if (d == 0) next
deltaX <- x2 - x1;
deltaY <- y2 - y1;
x_true <- x1 - (deltaX * d) / Δd;
y_true <- y1 - (deltaY * d) / Δd;
# Check if the true point is within the circle
if (sqrt(x_true^2 + y_true^2) <= circle_radius) {
break
}
}
# Data frame for points
points_df <- data.frame(x = c(x1, x2, x_true), y = c(y1, y2, y_true),
type = c('Near', 'Far', 'True'))
# Create plot
plots[[i]] <- ggplot() +
geom_circle(aes(x0 = 0, y0 = 0, r = circle_radius), linetype = "solid", color = "blue") +
geom_point(data = points_df, aes(x = x, y = y, color = type), size = 4) +
scale_color_manual(values = c('Near' = 'blue', 'Far' = 'black', 'True' = 'red')) +
theme_minimal() +
coord_fixed() +
xlim(c(-circle_radius, circle_radius)) +
ylim(c(-circle_radius, circle_radius)) +
theme(legend.position = "none",
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
plot.title = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank())
}
# Arrange plots in a grid
do.call(grid.arrange, c(plots, ncol = 3))
}
# Call the function
plot_laser_points()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment