Skip to content

Instantly share code, notes, and snippets.

@Mohammad-Reihani
Last active July 24, 2024 08:51
Show Gist options
  • Save Mohammad-Reihani/12bc2a76872d47546e9c4f9515322b02 to your computer and use it in GitHub Desktop.
Save Mohammad-Reihani/12bc2a76872d47546e9c4f9515322b02 to your computer and use it in GitHub Desktop.
A Python script to generate and save grid points within a specified area, with configurable spacing and minimum distance from walls
import json
def generate_grid_points(x_length_cm, y_length_cm, points_in_x, points_in_y, min_dist_x_cm, min_dist_y_cm):
# Generate points along x and y axes with minimum distance from the walls
x_points = [int(round(i * (x_length_cm / (points_in_x - 1)))) for i in range(points_in_x)]
y_points = [int(round(i * (y_length_cm / (points_in_y - 1)))) for i in range(points_in_y)]
# Filter out points that are too close to the walls
x_points = [x for x in x_points if min_dist_x_cm <= x <= x_length_cm - min_dist_x_cm]
y_points = [y for y in y_points if min_dist_y_cm <= y <= y_length_cm - min_dist_y_cm]
# Create the grid points
grid_points = [[x, y] for x in x_points for y in y_points]
return grid_points
def save_grid_points_to_file(grid_points, filename):
with open(filename, 'w') as file:
json.dump(grid_points, file)
# Example usage
x_length_cm = 1000 # 10 meters
y_length_cm = 1000 # 10 meters
points_in_x = 5
points_in_y = 5
min_dist_x_cm = 10 # 0.1 meter
min_dist_y_cm = 10 # 0.1 meter
grid_points = generate_grid_points(x_length_cm, y_length_cm, points_in_x, points_in_y, min_dist_x_cm, min_dist_y_cm)
save_grid_points_to_file(grid_points, 'grid_points.json')
# Print the array to be copy-pasted into a Python script
with open('grid_points.json', 'r') as file:
print(file.read())
#Rest of the code
#sample usage
data_points_array = [[100, 100], [100, 200], [100, 300], [100, 400], [100, 500], [100, 600], [100, 700], [100, 800], [100, 900], [100, 1000], [100, 1100], [100, 1200], [100, 1300], [100, 1400], [100, 1500], [100, 1600], [100, 1700], [200, 100], [200, 200], [200, 300], [200, 400], [200, 500], [200, 600], [200, 700], [200, 800], [200, 900], [200, 1000], [200, 1100], [200, 1200], [200, 1300], [200, 1400], [200, 1500], [200, 1600], [200, 1700], [300, 100], [300, 200], [300, 300], [300, 400], [300, 500], [300, 600], [300, 700], [300, 800], [300, 900], [300, 1000], [300, 1100], [300, 1200], [300, 1300], [300, 1400], [300, 1500], [300, 1600], [300, 1700], [400, 100], [400, 200], [400, 300], [400, 400], [400, 500], [400, 600], [400, 700], [400, 800], [400, 900], [400, 1000], [400, 1100], [400, 1200], [400, 1300], [400, 1400], [400, 1500], [400, 1600], [400, 1700], [500, 100], [500, 200], [500, 300], [500, 400], [500, 500], [500, 600], [500, 700], [500, 800], [500, 900], [500, 1000], [500, 1100], [500, 1200], [500, 1300], [500, 1400], [500, 1500], [500, 1600], [500, 1700], [600, 100], [600, 200], [600, 300], [600, 400], [600, 500], [600, 600], [600, 700], [600, 800], [600, 900], [600, 1000], [600, 1100], [600, 1200], [600, 1300], [600, 1400], [600, 1500], [600, 1600], [600, 1700], [700, 100], [700, 200], [700, 300], [700, 400], [700, 500], [700, 600], [700, 700], [700, 800], [700, 900], [700, 1000], [700, 1100], [700, 1200], [700, 1300], [700, 1400], [700, 1500], [700, 1600], [700, 1700], [800, 100], [800, 200], [800, 300], [800, 400], [800, 500], [800, 600], [800, 700], [800, 800], [800, 900], [800, 1000], [800, 1100], [800, 1200], [800, 1300], [800, 1400], [800, 1500], [800, 1600], [800, 1700], [900, 100], [900, 200], [900, 300], [900, 400], [900, 500], [900, 600], [900, 700], [900, 800], [900, 900], [900, 1000], [900, 1100], [900, 1200], [900, 1300], [900, 1400], [900, 1500], [900, 1600], [900, 1700], [1000, 100], [1000, 200], [1000, 300], [1000, 400], [1000, 500], [1000, 600], [1000, 700], [1000, 800], [1000, 900], [1000, 1000], [1000, 1100], [1000, 1200], [1000, 1300], [1000, 1400], [1000, 1500], [1000, 1600], [1000, 1700], [1100, 100], [1100, 200], [1100, 300], [1100, 400], [1100, 500], [1100, 600], [1100, 700], [1100, 800], [1100, 900], [1100, 1000], [1100, 1100], [1100, 1200], [1100, 1300], [1100, 1400], [1100, 1500], [1100, 1600], [1100, 1700], [1200, 100], [1200, 200], [1200, 300], [1200, 400], [1200, 500], [1200, 600], [1200, 700], [1200, 800], [1200, 900], [1200, 1000], [1200, 1100], [1200, 1200], [1200, 1300], [1200, 1400], [1200, 1500], [1200, 1600], [1200, 1700], [1300, 100], [1300, 200], [1300, 300], [1300, 400], [1300, 500], [1300, 600], [1300, 700], [1300, 800], [1300, 900], [1300, 1000], [1300, 1100], [1300, 1200], [1300, 1300], [1300, 1400], [1300, 1500], [1300, 1600], [1300, 1700], [1400, 100], [1400, 200], [1400, 300], [1400, 400], [1400, 500], [1400, 600], [1400, 700], [1400, 800], [1400, 900], [1400, 1000], [1400, 1100], [1400, 1200], [1400, 1300], [1400, 1400], [1400, 1500], [1400, 1600], [1400, 1700], [1500, 100], [1500, 200], [1500, 300], [1500, 400], [1500, 500], [1500, 600], [1500, 700], [1500, 800], [1500, 900], [1500, 1000], [1500, 1100], [1500, 1200], [1500, 1300], [1500, 1400], [1500, 1500], [1500, 1600], [1500, 1700], [1600, 100], [1600, 200], [1600, 300], [1600, 400], [1600, 500], [1600, 600], [1600, 700], [1600, 800], [1600, 900], [1600, 1000], [1600, 1100], [1600, 1200], [1600, 1300], [1600, 1400], [1600, 1500], [1600, 1600], [1600, 1700], [1700, 100], [1700, 200], [1700, 300], [1700, 400], [1700, 500], [1700, 600], [1700, 700], [1700, 800], [1700, 900], [1700, 1000], [1700, 1100], [1700, 1200], [1700, 1300], [1700, 1400], [1700, 1500], [1700, 1600], [1700, 1700], [1800, 100], [1800, 200], [1800, 300], [1800, 400], [1800, 500], [1800, 600], [1800, 700], [1800, 800], [1800, 900], [1800, 1000], [1800, 1100], [1800, 1200], [1800, 1300], [1800, 1400], [1800, 1500], [1800, 1600], [1800, 1700], [1900, 100], [1900, 200], [1900, 300], [1900, 400], [1900, 500], [1900, 600], [1900, 700], [1900, 800], [1900, 900], [1900, 1000], [1900, 1100], [1900, 1200], [1900, 1300], [1900, 1400], [1900, 1500], [1900, 1600], [1900, 1700], [2000, 100], [2000, 200], [2000, 300], [2000, 400], [2000, 500], [2000, 600], [2000, 700], [2000, 800], [2000, 900], [2000, 1000], [2000, 1100], [2000, 1200], [2000, 1300], [2000, 1400], [2000, 1500], [2000, 1600], [2000, 1700], [2100, 100], [2100, 200], [2100, 300], [2100, 400], [2100, 500], [2100, 600], [2100, 700], [2100, 800], [2100, 900], [2100, 1000], [2100, 1100], [2100, 1200], [2100, 1300], [2100, 1400], [2100, 1500], [2100, 1600], [2100, 1700], [2200, 100], [2200, 200], [2200, 300], [2200, 400], [2200, 500], [2200, 600], [2200, 700], [2200, 800], [2200, 900], [2200, 1000], [2200, 1100], [2200, 1200], [2200, 1300], [2200, 1400], [2200, 1500], [2200, 1600], [2200, 1700], [2300, 100], [2300, 200], [2300, 300], [2300, 400], [2300, 500], [2300, 600], [2300, 700], [2300, 800], [2300, 900], [2300, 1000], [2300, 1100], [2300, 1200], [2300, 1300], [2300, 1400], [2300, 1500], [2300, 1600], [2300, 1700], [2400, 100], [2400, 200], [2400, 300], [2400, 400], [2400, 500], [2400, 600], [2400, 700], [2400, 800], [2400, 900], [2400, 1000], [2400, 1100], [2400, 1200], [2400, 1300], [2400, 1400], [2400, 1500], [2400, 1600], [2400, 1700], [2500, 100], [2500, 200], [2500, 300], [2500, 400], [2500, 500], [2500, 600], [2500, 700], [2500, 800], [2500, 900], [2500, 1000], [2500, 1100], [2500, 1200], [2500, 1300], [2500, 1400], [2500, 1500], [2500, 1600], [2500, 1700], [2600, 100], [2600, 200], [2600, 300], [2600, 400], [2600, 500], [2600, 600], [2600, 700], [2600, 800], [2600, 900], [2600, 1000], [2600, 1100], [2600, 1200], [2600, 1300], [2600, 1400], [2600, 1500], [2600, 1600], [2600, 1700], [2700, 100], [2700, 200], [2700, 300], [2700, 400], [2700, 500], [2700, 600], [2700, 700], [2700, 800], [2700, 900], [2700, 1000], [2700, 1100], [2700, 1200], [2700, 1300], [2700, 1400], [2700, 1500], [2700, 1600], [2700, 1700], [2800, 100], [2800, 200], [2800, 300], [2800, 400], [2800, 500], [2800, 600], [2800, 700], [2800, 800], [2800, 900], [2800, 1000], [2800, 1100], [2800, 1200], [2800, 1300], [2800, 1400], [2800, 1500], [2800, 1600], [2800, 1700], [2900, 100], [2900, 200], [2900, 300], [2900, 400], [2900, 500], [2900, 600], [2900, 700], [2900, 800], [2900, 900], [2900, 1000], [2900, 1100], [2900, 1200], [2900, 1300], [2900, 1400], [2900, 1500], [2900, 1600], [2900, 1700], [3000, 100], [3000, 200], [3000, 300], [3000, 400], [3000, 500], [3000, 600], [3000, 700], [3000, 800], [3000, 900], [3000, 1000], [3000, 1100], [3000, 1200], [3000, 1300], [3000, 1400], [3000, 1500], [3000, 1600], [3000, 1700], [3100, 100], [3100, 200], [3100, 300], [3100, 400], [3100, 500], [3100, 600], [3100, 700], [3100, 800], [3100, 900], [3100, 1000], [3100, 1100], [3100, 1200], [3100, 1300], [3100, 1400], [3100, 1500], [3100, 1600], [3100, 1700], [3200, 100], [3200, 200], [3200, 300], [3200, 400], [3200, 500], [3200, 600], [3200, 700], [3200, 800], [3200, 900], [3200, 1000], [3200, 1100], [3200, 1200], [3200, 1300], [3200, 1400], [3200, 1500], [3200, 1600], [3200, 1700], [3300, 100], [3300, 200], [3300, 300], [3300, 400], [3300, 500], [3300, 600], [3300, 700], [3300, 800], [3300, 900], [3300, 1000], [3300, 1100], [3300, 1200], [3300, 1300], [3300, 1400], [3300, 1500], [3300, 1600], [3300, 1700], [3400, 100], [3400, 200], [3400, 300], [3400, 400], [3400, 500], [3400, 600], [3400, 700], [3400, 800], [3400, 900], [3400, 1000], [3400, 1100], [3400, 1200], [3400, 1300], [3400, 1400], [3400, 1500], [3400, 1600], [3400, 1700], [3500, 100], [3500, 200], [3500, 300], [3500, 400], [3500, 500], [3500, 600], [3500, 700], [3500, 800], [3500, 900], [3500, 1000], [3500, 1100], [3500, 1200], [3500, 1300], [3500, 1400], [3500, 1500], [3500, 1600], [3500, 1700], [3600, 100], [3600, 200], [3600, 300], [3600, 400], [3600, 500], [3600, 600], [3600, 700], [3600, 800], [3600, 900], [3600, 1000], [3600, 1100], [3600, 1200], [3600, 1300], [3600, 1400], [3600, 1500], [3600, 1600], [3600, 1700], [3700, 100], [3700, 200], [3700, 300], [3700, 400], [3700, 500], [3700, 600], [3700, 700], [3700, 800], [3700, 900], [3700, 1000], [3700, 1100], [3700, 1200], [3700, 1300], [3700, 1400], [3700, 1500], [3700, 1600], [3700, 1700], [3800, 100], [3800, 200], [3800, 300], [3800, 400], [3800, 500], [3800, 600], [3800, 700], [3800, 800], [3800, 900], [3800, 1000], [3800, 1100], [3800, 1200], [3800, 1300], [3800, 1400], [3800, 1500], [3800, 1600], [3800, 1700], [3900, 100], [3900, 200], [3900, 300], [3900, 400], [3900, 500], [3900, 600], [3900, 700], [3900, 800], [3900, 900], [3900, 1000], [3900, 1100], [3900, 1200], [3900, 1300], [3900, 1400], [3900, 1500], [3900, 1600], [3900, 1700]]
data_points = np.array(data_points_array)
#rest of the code
@Mohammad-Reihani
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment