import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Voronoi
# Generate the points
num_points = 21 # Number of points around the central point
R = 10 # Maximum radius for generating points around the center
# Generate random angles and radii
theta = np.random.uniform(0, 2 * np.pi, num_points)
r = R * np.sqrt(np.random.uniform(0, 1, num_points)) # Square root for uniform distribution in circle
# Convert polar coordinates to Cartesian coordinates
x = r * np.cos(theta)
y = r * np.sin(theta)
other_points = np.column_stack((x, y))
# Define the central point at (0, 0)
central_point = np.array([[0, 0]])
# Combine central point with other points
points = np.vstack((central_point, other_points))
# Create the Voronoi diagram
vor = Voronoi(points)
# Find indices of points whose regions are adjacent to the central point
ridge_points = vor.ridge_points # Indices of points between which each Voronoi ridge lies
# Initialize a list to store indices of adjacent points
Adj_indices = []
for pair in ridge_points:
if 0 in pair: # If the central point (index 0) is part of the ridge
neighbor = pair[0] if pair[1] == 0 else pair[1]
Adj_indices.append(neighbor)
# Remove duplicates and the central point itself if included
Adj_indices = np.unique(Adj_indices)
Adj_indices = Adj_indices[Adj_indices != 0] # Exclude central point
# Create the set Adj_P containing only points adjacent to the central point
Adj_P = points[Adj_indices.astype(int)]
# Plot the Voronoi diagram without infinite edges
fig, ax = plt.subplots()
# Plot all finite edges in black
for simplex in vor.ridge_vertices:
simplex = np.asarray(simplex)
if np.all(simplex >= 0): # Finite ridge (no vertex at infinity)
ax.plot(vor.vertices[simplex, 0], vor.vertices[simplex, 1], 'k-')
# Plot the central point in red
ax.plot(points[0, 0], points[0, 1], 'ro', label='Central Point')
# Plot adjacent points in blue
ax.plot(points[Adj_indices.astype(int), 0], points[Adj_indices.astype(int), 1], 'bo', label='Adjacent Points')
# Plot other points in black
other_indices = np.setdiff1d(np.arange(len(points)), np.hstack(([0], Adj_indices)))
ax.plot(points[other_indices, 0], points[other_indices, 1], 'ko', label='Other Points')
# Set equal aspect ratio and display the legend
ax.set_aspect('equal')
ax.legend()
plt.show()
This code generates 22 points where the first point is at the center (0,0), and the remaining 21 points are randomly distributed around it. It constructs a Voronoi diagram, identifies which points have regions adjacent to the central point, and plots the diagram with:
- The central point in red.
- Adjacent points in blue.
- Other points in black.
- All edges in black, excluding edges that continue to infinity.
The set Adj_P contains all points adjacent to the central point's region, excluding the central point itself.