56 lines
1.6 KiB
Python
56 lines
1.6 KiB
Python
|
# -*- coding: utf-8 -*-
|
||
|
"""
|
||
|
Created on Sun Oct 29 21:26:14 2023
|
||
|
|
||
|
@author: Lukas
|
||
|
"""
|
||
|
import drawsvg as draw
|
||
|
import random
|
||
|
|
||
|
# Größe der Zeichenfläche
|
||
|
canvas_width = 400
|
||
|
canvas_height = 400
|
||
|
|
||
|
# Anzahl der zufällig erzeugten Rechtecke
|
||
|
num_rectangles = 100
|
||
|
|
||
|
# Mindestabstand zwischen den Rechtecken
|
||
|
min_spacing = 5
|
||
|
|
||
|
# Erstellen der Zeichenfläche
|
||
|
d = draw.Drawing(canvas_width, canvas_height, origin='center')
|
||
|
|
||
|
# Funktion zum Erzeugen eines zufälligen Rechtecks mit Abstand
|
||
|
def create_random_rectangle(rectangles):
|
||
|
while True:
|
||
|
x = random.uniform(-canvas_width / 2, canvas_width / 2)
|
||
|
y = random.uniform(-canvas_height / 2, canvas_height / 2)
|
||
|
width = random.uniform(10, 50)
|
||
|
height = random.uniform(10, 50)
|
||
|
|
||
|
# Überprüfen, ob das neue Rechteck mit den vorhandenen Rechtecken kollidiert
|
||
|
collides = False
|
||
|
for rect in rectangles:
|
||
|
x1, y1, w1, h1 = x, y, width, height
|
||
|
x2, y2, w2, h2 = rect
|
||
|
|
||
|
if (x1 + w1 + min_spacing < x2 or x2 + w2 + min_spacing < x1 or
|
||
|
y1 + h1 + min_spacing < y2 or y2 + h2 + min_spacing < y1):
|
||
|
continue
|
||
|
collides = True
|
||
|
break
|
||
|
|
||
|
if not collides:
|
||
|
rectangles.append((x, y, width, height))
|
||
|
return draw.Rectangle(x, y, width, height, fill='blue')
|
||
|
|
||
|
# Erzeugen der zufälligen Rechtecke und Hinzufügen zur Zeichenfläche
|
||
|
rectangles = []
|
||
|
for _ in range(num_rectangles):
|
||
|
random_rectangle = create_random_rectangle(rectangles)
|
||
|
d.append(random_rectangle)
|
||
|
|
||
|
# Speichern der SVG-Datei
|
||
|
d.save_svg('random_rectangles_with_spacing.svg')
|
||
|
|