Created
December 29, 2024 00:22
-
-
Save taesiri/37298a12d8de98fe99965d829bbdb774 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def generate_grid_svg(size=10, cell_size=50, stroke_width=2): | |
""" | |
Generate an SVG with a grid of random vertical or horizontal lines. | |
Args: | |
size (int): Number of cells in both dimensions (creates size x size grid) | |
cell_size (int): Size of each cell in pixels | |
stroke_width (int): Width of the lines in pixels | |
Returns: | |
tuple: (svg_content, stats_dict) | |
""" | |
# Calculate total dimensions | |
total_size = size * cell_size | |
# Initialize counters | |
vertical_count = 0 | |
horizontal_count = 0 | |
row_stats = {i: {'vertical': 0, 'horizontal': 0} for i in range(size)} | |
col_stats = {i: {'vertical': 0, 'horizontal': 0} for i in range(size)} | |
# Start SVG content | |
svg = f'''<?xml version="1.0" encoding="UTF-8"?> | |
<svg xmlns="http://www.w3.org/2000/svg" width="{total_size}" height="{total_size}" viewBox="0 0 {total_size} {total_size}"> | |
<!-- Grid background --> | |
<rect width="100%" height="100%" fill="white"/> | |
<!-- Grid lines -->''' | |
# Add grid lines | |
for i in range(size + 1): | |
x = i * cell_size | |
svg += f''' | |
<line x1="{x}" y1="0" x2="{x}" y2="{total_size}" stroke="lightgray" stroke-width="1"/> | |
<line x1="0" y1="{x}" x2="{total_size}" y2="{x}" stroke="lightgray" stroke-width="1"/>''' | |
# Add random lines in each cell | |
for row in range(size): | |
for col in range(size): | |
x = col * cell_size | |
y = row * cell_size | |
# Randomly choose vertical or horizontal line | |
is_vertical = random.choice([True, False]) | |
if is_vertical: | |
vertical_count += 1 | |
row_stats[row]['vertical'] += 1 | |
col_stats[col]['vertical'] += 1 | |
# Draw vertical line in the middle of the cell | |
x1 = x + cell_size/2 | |
y1 = y + cell_size * 0.3 | |
y2 = y + cell_size * 0.7 | |
svg += f''' | |
<line x1="{x1}" y1="{y1}" x2="{x1}" y2="{y2}" stroke="black" stroke-width="{stroke_width}"/>''' | |
else: | |
horizontal_count += 1 | |
row_stats[row]['horizontal'] += 1 | |
col_stats[col]['horizontal'] += 1 | |
# Draw horizontal line in the middle of the cell | |
x1 = x + cell_size * 0.3 | |
x2 = x + cell_size * 0.7 | |
y1 = y + cell_size/2 | |
svg += f''' | |
<line x1="{x1}" y1="{y1}" x2="{x2}" y2="{y1}" stroke="black" stroke-width="{stroke_width}"/>''' | |
# Close SVG tag | |
svg += ''' | |
</svg>''' | |
stats = { | |
'total_vertical': vertical_count, | |
'total_horizontal': horizontal_count, | |
'row_stats': row_stats, | |
'col_stats': col_stats | |
} | |
return svg, stats | |
# Example usage | |
if __name__ == "__main__": | |
# Generate SVG | |
svg_content, stats = generate_grid_svg(size=10, cell_size=50, stroke_width=2) | |
# Print overall statistics | |
print(f"Overall Statistics:") | |
print(f"Total vertical lines: {stats['total_vertical']}") | |
print(f"Total horizontal lines: {stats['total_horizontal']}") | |
print(f"Total lines: {stats['total_vertical'] + stats['total_horizontal']}") | |
print("\nRow Statistics:") | |
for row in range(len(stats['row_stats'])): | |
row_data = stats['row_stats'][row] | |
print(f"Row {row}: {row_data['vertical']} vertical, {row_data['horizontal']} horizontal") | |
print("\nColumn Statistics:") | |
for col in range(len(stats['col_stats'])): | |
col_data = stats['col_stats'][col] | |
print(f"Column {col}: {col_data['vertical']} vertical, {col_data['horizontal']} horizontal") | |
# Save to file | |
with open("random_grid.svg", "w") as f: | |
f.write(svg_content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment